Plugin review changes round 2
This commit is contained in:
parent
8b4a50647d
commit
889d51712e
9 changed files with 52 additions and 24 deletions
|
|
@ -25,9 +25,13 @@ export class APIManager {
|
|||
}
|
||||
|
||||
async queryDetailedInfo(item: MediaTypeModel): Promise<MediaTypeModel> {
|
||||
return await this.queryDetailedInfoById(item.id, item.dataSource);
|
||||
}
|
||||
|
||||
async queryDetailedInfoById(id: string, dataSource: string): Promise<MediaTypeModel> {
|
||||
for (const api of this.apis) {
|
||||
if (api.apiName === item.dataSource) {
|
||||
return api.getById(item);
|
||||
if (api.apiName === dataSource) {
|
||||
return api.getById(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ export abstract class APIModel {
|
|||
*/
|
||||
abstract searchByTitle(title: string): Promise<MediaTypeModel[]>;
|
||||
|
||||
abstract getById(item: MediaTypeModel): Promise<MediaTypeModel>;
|
||||
abstract getById(id: string): Promise<MediaTypeModel>;
|
||||
|
||||
hasType(type: string): boolean {
|
||||
return this.types.contains(type);
|
||||
|
|
|
|||
|
|
@ -40,10 +40,10 @@ export class LocGovAPI extends APIModel {
|
|||
// return ret;
|
||||
}
|
||||
|
||||
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
|
||||
async getById(id: string): Promise<MediaTypeModel> {
|
||||
console.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
|
||||
const searchUrl = `https://www.loc.gov/item/${item.id}/?fo=json`;
|
||||
const searchUrl = `https://www.loc.gov/item/${encodeURIComponent(id)}/?fo=json`;
|
||||
const fetchData = await fetch(searchUrl);
|
||||
if (fetchData.status !== 200) {
|
||||
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
|
||||
|
|
|
|||
|
|
@ -69,10 +69,10 @@ export class MALAPI extends APIModel {
|
|||
return ret;
|
||||
}
|
||||
|
||||
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
|
||||
async getById(id: string): Promise<MediaTypeModel> {
|
||||
console.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
|
||||
const searchUrl = `https://api.jikan.moe/v4/anime/${item.id}`;
|
||||
const searchUrl = `https://api.jikan.moe/v4/anime/${encodeURIComponent(id)}`;
|
||||
const fetchData = await fetch(searchUrl);
|
||||
|
||||
if (fetchData.status !== 200) {
|
||||
|
|
|
|||
|
|
@ -58,14 +58,14 @@ export class MusicBrainzAPI extends APIModel {
|
|||
return ret;
|
||||
}
|
||||
|
||||
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
|
||||
async getById(id: string): Promise<MediaTypeModel> {
|
||||
console.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
|
||||
const searchUrl = `https://musicbrainz.org/ws/2/release-group/${encodeURIComponent(item.id)}?inc=releases+artists+tags+ratings+genres&fmt=json`;
|
||||
const searchUrl = `https://musicbrainz.org/ws/2/release-group/${encodeURIComponent(id)}?inc=releases+artists+tags+ratings+genres&fmt=json`;
|
||||
const fetchData = await requestUrl({
|
||||
url: searchUrl,
|
||||
headers: {
|
||||
'User-Agent': `${pluginName}/0.1.7 (${contactEmail})`,
|
||||
'User-Agent': `${pluginName}/${mediaDbVersion} (${contactEmail})`,
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -88,10 +88,10 @@ export class OMDbAPI extends APIModel {
|
|||
return ret;
|
||||
}
|
||||
|
||||
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
|
||||
async getById(id: string): Promise<MediaTypeModel> {
|
||||
console.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
|
||||
const searchUrl = `http://www.omdbapi.com/?i=${item.id}&apikey=${this.plugin.settings.OMDbKey}`;
|
||||
const searchUrl = `http://www.omdbapi.com/?i=${encodeURIComponent(id)}&apikey=${this.plugin.settings.OMDbKey}`;
|
||||
const fetchData = await fetch(searchUrl);
|
||||
|
||||
if (fetchData.status === 401) {
|
||||
|
|
|
|||
|
|
@ -65,10 +65,10 @@ export class SteamAPI extends APIModel {
|
|||
return ret;
|
||||
}
|
||||
|
||||
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
|
||||
async getById(id: string): Promise<MediaTypeModel> {
|
||||
console.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
|
||||
const searchUrl = `http://store.steampowered.com/api/appdetails?appids=${item.id}`;
|
||||
const searchUrl = `http://store.steampowered.com/api/appdetails?appids=${encodeURIComponent(id)}`;
|
||||
const fetchData = await requestUrl({
|
||||
url: searchUrl,
|
||||
});
|
||||
|
|
@ -77,7 +77,17 @@ export class SteamAPI extends APIModel {
|
|||
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
|
||||
}
|
||||
|
||||
const result = (await fetchData.json)[item.id].data;
|
||||
debugLog(await fetchData.json);
|
||||
|
||||
let result;
|
||||
for (const [key, value] of Object.entries(await fetchData.json)) {
|
||||
if (key == id) {
|
||||
result = value.data;
|
||||
}
|
||||
}
|
||||
if (!result) {
|
||||
throw Error(`MDB | API returned invalid data.`);
|
||||
}
|
||||
|
||||
debugLog(result);
|
||||
|
||||
|
|
|
|||
|
|
@ -46,10 +46,10 @@ export class WikipediaAPI extends APIModel {
|
|||
return ret;
|
||||
}
|
||||
|
||||
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
|
||||
async getById(id: string): Promise<MediaTypeModel> {
|
||||
console.log(`MDB | api "${this.apiName}" queried by ID`);
|
||||
|
||||
const searchUrl = `https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=${item.id}&inprop=url&format=json&origin=*`;
|
||||
const searchUrl = `https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=${encodeURIComponent(id)}&inprop=url&format=json&origin=*`;
|
||||
const fetchData = await fetch(searchUrl);
|
||||
|
||||
if (fetchData.status !== 200) {
|
||||
|
|
|
|||
28
src/main.ts
28
src/main.ts
|
|
@ -43,7 +43,15 @@ export default class MediaDbPlugin extends Plugin {
|
|||
this.addCommand({
|
||||
id: 'update-media-db-note',
|
||||
name: 'Update the open note, if it is a Media DB entry.',
|
||||
callback: () => this.updateActiveNote(),
|
||||
checkCallback: (checking: boolean) => {
|
||||
if (!this.app.workspace.getActiveFile()) {
|
||||
return false;
|
||||
}
|
||||
if (!checking) {
|
||||
this.updateActiveNote()
|
||||
}
|
||||
return true;
|
||||
},
|
||||
});
|
||||
|
||||
// register the settings tab
|
||||
|
|
@ -88,7 +96,11 @@ export default class MediaDbPlugin extends Plugin {
|
|||
const fileName = replaceIllegalFileNameCharactersInString(this.mediaTypeManager.getFileName(mediaTypeModel));
|
||||
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${fileName}.md`;
|
||||
|
||||
await this.app.vault.delete(this.app.vault.getAbstractFileByPath(filePath));
|
||||
const file = this.app.vault.getAbstractFileByPath(filePath);
|
||||
if (file) {
|
||||
await this.app.vault.delete(file);
|
||||
}
|
||||
|
||||
const targetFile = await this.app.vault.create(filePath, fileContent);
|
||||
|
||||
// open file
|
||||
|
|
@ -127,10 +139,12 @@ export default class MediaDbPlugin extends Plugin {
|
|||
}
|
||||
|
||||
async updateActiveNote() {
|
||||
const activeLeaf: TFile = this.app.workspace.getActiveFile();
|
||||
if (!activeLeaf.name) return;
|
||||
const activeFile: TFile = this.app.workspace.getActiveFile();
|
||||
if (!activeFile) {
|
||||
throw new Error('MDB | there is no active note');
|
||||
}
|
||||
|
||||
let metadata: FrontMatterCache = this.app.metadataCache.getFileCache(activeLeaf).frontmatter;
|
||||
let metadata: FrontMatterCache = this.app.metadataCache.getFileCache(activeFile).frontmatter;
|
||||
|
||||
if (!metadata?.type || !metadata?.dataSource || !metadata?.id) {
|
||||
throw new Error('MDB | active note is not a Media DB entry or is missing metadata');
|
||||
|
|
@ -139,7 +153,7 @@ export default class MediaDbPlugin extends Plugin {
|
|||
delete metadata.position; // remove unnecessary data from the FrontMatterCache
|
||||
let oldMediaTypeModel = this.mediaTypeManager.createMediaTypeModelFromMediaType(metadata, metadata.type);
|
||||
|
||||
let newMediaTypeModel = await this.apiManager.queryDetailedInfo({dataSource: metadata.dataSource, id: metadata.id} as MediaTypeModel);
|
||||
let newMediaTypeModel = await this.apiManager.queryDetailedInfoById(metadata.id, metadata.dataSource);
|
||||
if (!newMediaTypeModel) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -147,7 +161,7 @@ export default class MediaDbPlugin extends Plugin {
|
|||
newMediaTypeModel = Object.assign(oldMediaTypeModel, newMediaTypeModel.getWithOutUserData());
|
||||
|
||||
console.log('MDB | deleting old entry');
|
||||
await this.app.vault.delete(activeLeaf);
|
||||
await this.app.vault.delete(activeFile);
|
||||
await this.createMediaDbNoteFromModel(newMediaTypeModel);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue