Skip to content

Commit 889d517

Browse files
committed
Plugin review changes round 2
1 parent 8b4a506 commit 889d517

File tree

9 files changed

+52
-24
lines changed

9 files changed

+52
-24
lines changed

src/api/APIManager.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ export class APIManager {
2525
}
2626

2727
async queryDetailedInfo(item: MediaTypeModel): Promise<MediaTypeModel> {
28+
return await this.queryDetailedInfoById(item.id, item.dataSource);
29+
}
30+
31+
async queryDetailedInfoById(id: string, dataSource: string): Promise<MediaTypeModel> {
2832
for (const api of this.apis) {
29-
if (api.apiName === item.dataSource) {
30-
return api.getById(item);
33+
if (api.apiName === dataSource) {
34+
return api.getById(id);
3135
}
3236
}
3337
}

src/api/APIModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export abstract class APIModel {
1313
*/
1414
abstract searchByTitle(title: string): Promise<MediaTypeModel[]>;
1515

16-
abstract getById(item: MediaTypeModel): Promise<MediaTypeModel>;
16+
abstract getById(id: string): Promise<MediaTypeModel>;
1717

1818
hasType(type: string): boolean {
1919
return this.types.contains(type);

src/api/apis/LocGovAPI.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ export class LocGovAPI extends APIModel {
4040
// return ret;
4141
}
4242

43-
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
43+
async getById(id: string): Promise<MediaTypeModel> {
4444
console.log(`MDB | api "${this.apiName}" queried by ID`);
4545

46-
const searchUrl = `https://www.loc.gov/item/${item.id}/?fo=json`;
46+
const searchUrl = `https://www.loc.gov/item/${encodeURIComponent(id)}/?fo=json`;
4747
const fetchData = await fetch(searchUrl);
4848
if (fetchData.status !== 200) {
4949
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);

src/api/apis/MALAPI.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ export class MALAPI extends APIModel {
6969
return ret;
7070
}
7171

72-
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
72+
async getById(id: string): Promise<MediaTypeModel> {
7373
console.log(`MDB | api "${this.apiName}" queried by ID`);
7474

75-
const searchUrl = `https://api.jikan.moe/v4/anime/${item.id}`;
75+
const searchUrl = `https://api.jikan.moe/v4/anime/${encodeURIComponent(id)}`;
7676
const fetchData = await fetch(searchUrl);
7777

7878
if (fetchData.status !== 200) {

src/api/apis/MusicBrainzAPI.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ export class MusicBrainzAPI extends APIModel {
5858
return ret;
5959
}
6060

61-
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
61+
async getById(id: string): Promise<MediaTypeModel> {
6262
console.log(`MDB | api "${this.apiName}" queried by ID`);
6363

64-
const searchUrl = `https://musicbrainz.org/ws/2/release-group/${encodeURIComponent(item.id)}?inc=releases+artists+tags+ratings+genres&fmt=json`;
64+
const searchUrl = `https://musicbrainz.org/ws/2/release-group/${encodeURIComponent(id)}?inc=releases+artists+tags+ratings+genres&fmt=json`;
6565
const fetchData = await requestUrl({
6666
url: searchUrl,
6767
headers: {
68-
'User-Agent': `${pluginName}/0.1.7 (${contactEmail})`,
68+
'User-Agent': `${pluginName}/${mediaDbVersion} (${contactEmail})`,
6969
},
7070
});
7171

src/api/apis/OMDbAPI.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ export class OMDbAPI extends APIModel {
8888
return ret;
8989
}
9090

91-
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
91+
async getById(id: string): Promise<MediaTypeModel> {
9292
console.log(`MDB | api "${this.apiName}" queried by ID`);
9393

94-
const searchUrl = `http://www.omdbapi.com/?i=${item.id}&apikey=${this.plugin.settings.OMDbKey}`;
94+
const searchUrl = `http://www.omdbapi.com/?i=${encodeURIComponent(id)}&apikey=${this.plugin.settings.OMDbKey}`;
9595
const fetchData = await fetch(searchUrl);
9696

9797
if (fetchData.status === 401) {

src/api/apis/SteamAPI.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ export class SteamAPI extends APIModel {
6565
return ret;
6666
}
6767

68-
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
68+
async getById(id: string): Promise<MediaTypeModel> {
6969
console.log(`MDB | api "${this.apiName}" queried by ID`);
7070

71-
const searchUrl = `http://store.steampowered.com/api/appdetails?appids=${item.id}`;
71+
const searchUrl = `http://store.steampowered.com/api/appdetails?appids=${encodeURIComponent(id)}`;
7272
const fetchData = await requestUrl({
7373
url: searchUrl,
7474
});
@@ -77,7 +77,17 @@ export class SteamAPI extends APIModel {
7777
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
7878
}
7979

80-
const result = (await fetchData.json)[item.id].data;
80+
debugLog(await fetchData.json);
81+
82+
let result;
83+
for (const [key, value] of Object.entries(await fetchData.json)) {
84+
if (key == id) {
85+
result = value.data;
86+
}
87+
}
88+
if (!result) {
89+
throw Error(`MDB | API returned invalid data.`);
90+
}
8191

8292
debugLog(result);
8393

src/api/apis/WikipediaAPI.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ export class WikipediaAPI extends APIModel {
4646
return ret;
4747
}
4848

49-
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
49+
async getById(id: string): Promise<MediaTypeModel> {
5050
console.log(`MDB | api "${this.apiName}" queried by ID`);
5151

52-
const searchUrl = `https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=${item.id}&inprop=url&format=json&origin=*`;
52+
const searchUrl = `https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=${encodeURIComponent(id)}&inprop=url&format=json&origin=*`;
5353
const fetchData = await fetch(searchUrl);
5454

5555
if (fetchData.status !== 200) {

src/main.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ export default class MediaDbPlugin extends Plugin {
4343
this.addCommand({
4444
id: 'update-media-db-note',
4545
name: 'Update the open note, if it is a Media DB entry.',
46-
callback: () => this.updateActiveNote(),
46+
checkCallback: (checking: boolean) => {
47+
if (!this.app.workspace.getActiveFile()) {
48+
return false;
49+
}
50+
if (!checking) {
51+
this.updateActiveNote()
52+
}
53+
return true;
54+
},
4755
});
4856

4957
// register the settings tab
@@ -88,7 +96,11 @@ export default class MediaDbPlugin extends Plugin {
8896
const fileName = replaceIllegalFileNameCharactersInString(this.mediaTypeManager.getFileName(mediaTypeModel));
8997
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${fileName}.md`;
9098

91-
await this.app.vault.delete(this.app.vault.getAbstractFileByPath(filePath));
99+
const file = this.app.vault.getAbstractFileByPath(filePath);
100+
if (file) {
101+
await this.app.vault.delete(file);
102+
}
103+
92104
const targetFile = await this.app.vault.create(filePath, fileContent);
93105

94106
// open file
@@ -127,10 +139,12 @@ export default class MediaDbPlugin extends Plugin {
127139
}
128140

129141
async updateActiveNote() {
130-
const activeLeaf: TFile = this.app.workspace.getActiveFile();
131-
if (!activeLeaf.name) return;
142+
const activeFile: TFile = this.app.workspace.getActiveFile();
143+
if (!activeFile) {
144+
throw new Error('MDB | there is no active note');
145+
}
132146

133-
let metadata: FrontMatterCache = this.app.metadataCache.getFileCache(activeLeaf).frontmatter;
147+
let metadata: FrontMatterCache = this.app.metadataCache.getFileCache(activeFile).frontmatter;
134148

135149
if (!metadata?.type || !metadata?.dataSource || !metadata?.id) {
136150
throw new Error('MDB | active note is not a Media DB entry or is missing metadata');
@@ -139,15 +153,15 @@ export default class MediaDbPlugin extends Plugin {
139153
delete metadata.position; // remove unnecessary data from the FrontMatterCache
140154
let oldMediaTypeModel = this.mediaTypeManager.createMediaTypeModelFromMediaType(metadata, metadata.type);
141155

142-
let newMediaTypeModel = await this.apiManager.queryDetailedInfo({dataSource: metadata.dataSource, id: metadata.id} as MediaTypeModel);
156+
let newMediaTypeModel = await this.apiManager.queryDetailedInfoById(metadata.id, metadata.dataSource);
143157
if (!newMediaTypeModel) {
144158
return;
145159
}
146160

147161
newMediaTypeModel = Object.assign(oldMediaTypeModel, newMediaTypeModel.getWithOutUserData());
148162

149163
console.log('MDB | deleting old entry');
150-
await this.app.vault.delete(activeLeaf);
164+
await this.app.vault.delete(activeFile);
151165
await this.createMediaDbNoteFromModel(newMediaTypeModel);
152166
}
153167

0 commit comments

Comments
 (0)