small fixes

This commit is contained in:
Moritz Jung 2024-06-08 20:22:41 +02:00
parent 00ba1d85ed
commit e8e262e530
9 changed files with 63 additions and 91 deletions

View file

@ -27,13 +27,13 @@ export class BoardGameGeekAPI extends APIModel {
});
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = fetchData.text;
const response = new window.DOMParser().parseFromString(data, 'text/xml');
console.debug(response);
// console.debug(response);
const ret: MediaTypeModel[] = [];
@ -65,12 +65,12 @@ export class BoardGameGeekAPI extends APIModel {
});
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = fetchData.text;
const response = new window.DOMParser().parseFromString(data, 'text/xml');
console.debug(response);
// console.debug(response);
const boardgame = response.querySelector('boardgame')!;
const title = boardgame.querySelector('name[primary=true]')!.textContent!;
@ -84,7 +84,7 @@ export class BoardGameGeekAPI extends APIModel {
const playtime = (boardgame.querySelector('playingtime')?.textContent ?? 'unknown') + ' minutes';
const publishers = Array.from(boardgame.querySelectorAll('boardgamepublisher')).map(n => n!.textContent!);
const model = new BoardGameModel({
return new BoardGameModel({
title: title,
englishTitle: title,
year: year === '0' ? '' : year,
@ -108,7 +108,5 @@ export class BoardGameGeekAPI extends APIModel {
personalRating: 0,
},
} as BoardGameModel);
return model;
}
}

View file

@ -31,13 +31,13 @@ export class MALAPI extends APIModel {
const searchUrl = `https://api.jikan.moe/v4/anime?q=${encodeURIComponent(title)}&limit=20${this.plugin.settings.sfwFilter ? '&sfw' : ''}`;
const fetchData = await fetch(searchUrl);
console.debug(fetchData);
// console.debug(fetchData);
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json();
console.debug(data);
// console.debug(data);
const ret: MediaTypeModel[] = [];
@ -90,16 +90,16 @@ export class MALAPI extends APIModel {
const fetchData = await fetch(searchUrl);
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json();
console.debug(data);
// console.debug(data);
const result = data.data;
const type = this.typeMappings.get(result.type?.toLowerCase());
if (type === undefined) {
const model = new MovieModel({
return new MovieModel({
subType: '',
title: result.title,
englishTitle: result.title_english ?? result.title,
@ -128,12 +128,10 @@ export class MALAPI extends APIModel {
personalRating: 0,
},
} as MovieModel);
return model;
}
if (type === 'movie' || type === 'special') {
const model = new MovieModel({
return new MovieModel({
subType: type,
title: result.title,
englishTitle: result.title_english ?? result.title,
@ -162,10 +160,8 @@ export class MALAPI extends APIModel {
personalRating: 0,
},
} as MovieModel);
return model;
} else if (type === 'series' || type === 'ova') {
const model = new SeriesModel({
return new SeriesModel({
subType: type,
title: result.title,
englishTitle: result.title_english ?? result.title,
@ -195,8 +191,6 @@ export class MALAPI extends APIModel {
personalRating: 0,
},
} as SeriesModel);
return model;
}
return;

View file

@ -32,13 +32,13 @@ export class MALAPIManga extends APIModel {
const searchUrl = `https://api.jikan.moe/v4/manga?q=${encodeURIComponent(title)}&limit=20${this.plugin.settings.sfwFilter ? '&sfw' : ''}`;
const fetchData = await fetch(searchUrl);
console.debug(fetchData);
// console.debug(fetchData);
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json();
console.debug(data);
// console.debug(data);
const ret: MediaTypeModel[] = [];
@ -87,15 +87,15 @@ export class MALAPIManga extends APIModel {
const fetchData = await fetch(searchUrl);
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json();
console.debug(data);
// console.debug(data);
const result = data.data;
const type = this.typeMappings.get(result.type?.toLowerCase());
const model = new MangaModel({
return new MangaModel({
subType: type,
title: result.title,
englishTitle: result.title_english ?? result.title,
@ -124,7 +124,5 @@ export class MALAPIManga extends APIModel {
personalRating: 0,
},
} as MangaModel);
return model;
}
}

View file

@ -21,8 +21,8 @@ export class MobyGamesAPI extends APIModel {
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
console.log(`MDB | api "${this.apiName}" queried by Title`);
if(!this.plugin.settings.MobyGamesKey) {
throw Error(`MDB | ${this.apiName} API key missing.`);
if (!this.plugin.settings.MobyGamesKey) {
throw Error(`MDB | API key for ${this.apiName} missing.`);
}
const searchUrl = `${this.apiUrl}/games?title=${encodeURIComponent(title)}&api_key=${this.plugin.settings.MobyGamesKey}`;
@ -30,7 +30,7 @@ export class MobyGamesAPI extends APIModel {
url: searchUrl,
});
console.debug(fetchData);
// console.debug(fetchData);
if (fetchData.status === 401) {
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
@ -39,11 +39,11 @@ export class MobyGamesAPI extends APIModel {
throw Error(`MDB | Too many requests for ${this.apiName}, you've exceeded your API quota.`);
}
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json;
console.debug(data);
// console.debug(data);
const ret: MediaTypeModel[] = [];
for (const result of data.games) {
ret.push(
@ -64,8 +64,8 @@ export class MobyGamesAPI extends APIModel {
async getById(id: string): Promise<MediaTypeModel> {
console.log(`MDB | api "${this.apiName}" queried by ID`);
if(!this.plugin.settings.MobyGamesKey) {
throw Error(`MDB | ${this.apiName} API key missing.`);
if (!this.plugin.settings.MobyGamesKey) {
throw Error(`MDB | API key for ${this.apiName} missing.`);
}
const searchUrl = `${this.apiUrl}/games?id=${encodeURIComponent(id)}&api_key=${this.plugin.settings.MobyGamesKey}`;
@ -75,14 +75,14 @@ export class MobyGamesAPI extends APIModel {
console.debug(fetchData);
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json;
console.debug(data);
// console.debug(data);
const result = data.games[0];
const model = new GameModel({
return new GameModel({
type: MediaType.Game,
title: result.title,
englishTitle: result.title,
@ -105,7 +105,5 @@ export class MobyGamesAPI extends APIModel {
personalRating: 0,
},
} as GameModel);
return model;
}
}

View file

@ -31,14 +31,14 @@ export class MusicBrainzAPI extends APIModel {
},
});
console.debug(fetchData);
// console.debug(fetchData);
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json;
console.debug(data);
// console.debug(data);
const ret: MediaTypeModel[] = [];
for (const result of data['release-groups']) {
@ -74,14 +74,12 @@ export class MusicBrainzAPI extends APIModel {
});
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json;
console.debug(data);
const result = data;
const result = await fetchData.json;
const model = new MusicReleaseModel({
return new MusicReleaseModel({
type: 'musicRelease',
title: result.title,
englishTitle: result.title,
@ -100,7 +98,5 @@ export class MusicBrainzAPI extends APIModel {
personalRating: 0,
},
} as MusicReleaseModel);
return model;
}
}

View file

@ -28,8 +28,8 @@ export class OMDbAPI extends APIModel {
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
console.log(`MDB | api "${this.apiName}" queried by Title`);
if(!this.plugin.settings.OMDbKey) {
throw Error(`MDB | ${this.apiName} API key missing.`);
if (!this.plugin.settings.OMDbKey) {
throw Error(`MDB | API key for ${this.apiName} missing.`);
}
const searchUrl = `https://www.omdbapi.com/?s=${encodeURIComponent(title)}&apikey=${this.plugin.settings.OMDbKey}`;
@ -39,7 +39,7 @@ export class OMDbAPI extends APIModel {
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
}
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json();
@ -55,7 +55,7 @@ export class OMDbAPI extends APIModel {
return [];
}
console.debug(data.Search);
// console.debug(data.Search);
const ret: MediaTypeModel[] = [];
@ -106,8 +106,8 @@ export class OMDbAPI extends APIModel {
async getById(id: string): Promise<MediaTypeModel> {
console.log(`MDB | api "${this.apiName}" queried by ID`);
if(!this.plugin.settings.OMDbKey) {
throw Error(`MDB | ${this.apiName} API key missing.`);
if (!this.plugin.settings.OMDbKey) {
throw Error(`MDB | API key for ${this.apiName} missing.`);
}
const searchUrl = `https://www.omdbapi.com/?i=${encodeURIComponent(id)}&apikey=${this.plugin.settings.OMDbKey}`;
@ -117,7 +117,7 @@ export class OMDbAPI extends APIModel {
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
}
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const result = await fetchData.json();
@ -133,7 +133,7 @@ export class OMDbAPI extends APIModel {
}
if (type === 'movie') {
const model = new MovieModel({
return new MovieModel({
type: type,
title: result.Title,
englishTitle: result.Title,
@ -162,10 +162,8 @@ export class OMDbAPI extends APIModel {
personalRating: 0,
},
} as MovieModel);
return model;
} else if (type === 'series') {
const model = new SeriesModel({
return new SeriesModel({
type: type,
title: result.Title,
englishTitle: result.Title,
@ -196,10 +194,8 @@ export class OMDbAPI extends APIModel {
personalRating: 0,
},
} as SeriesModel);
return model;
} else if (type === 'game') {
const model = new GameModel({
return new GameModel({
type: type,
title: result.Title,
englishTitle: result.Title,
@ -222,8 +218,6 @@ export class OMDbAPI extends APIModel {
personalRating: 0,
},
} as GameModel);
return model;
}
return;

View file

@ -23,13 +23,13 @@ export class OpenLibraryAPI extends APIModel {
const searchUrl = `https://openlibrary.org/search.json?title=${encodeURIComponent(title)}`;
const fetchData = await fetch(searchUrl);
console.debug(fetchData);
// console.debug(fetchData);
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json();
console.debug(data);
// console.debug(data);
const ret: MediaTypeModel[] = [];
@ -54,17 +54,17 @@ export class OpenLibraryAPI extends APIModel {
const searchUrl = `https://openlibrary.org/search.json?q=key:${encodeURIComponent(id)}`;
const fetchData = await fetch(searchUrl);
console.debug(fetchData);
// console.debug(fetchData);
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json();
console.debug(data);
// console.debug(data);
const result = data.docs[0];
const model = new BookModel({
return new BookModel({
title: result.title,
year: result.first_publish_year,
dataSource: this.apiName,
@ -88,7 +88,5 @@ export class OpenLibraryAPI extends APIModel {
personalRating: 0,
},
} as BookModel);
return model;
}
}

View file

@ -31,12 +31,12 @@ export class SteamAPI extends APIModel {
});
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json;
console.debug(data);
// console.debug(data);
const ret: MediaTypeModel[] = [];
@ -65,10 +65,10 @@ export class SteamAPI extends APIModel {
});
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
console.debug(await fetchData.json);
// console.debug(await fetchData.json);
let result: any;
for (const [key, value] of Object.entries(await fetchData.json)) {
@ -83,9 +83,9 @@ export class SteamAPI extends APIModel {
throw Error(`MDB | API returned invalid data.`);
}
console.debug(result);
// console.debug(result);
const model = new GameModel({
return new GameModel({
type: MediaType.Game,
title: result.name,
englishTitle: result.name,
@ -108,7 +108,5 @@ export class SteamAPI extends APIModel {
personalRating: 0,
},
} as GameModel);
return model;
}
}

View file

@ -23,10 +23,10 @@ export class WikipediaAPI extends APIModel {
const searchUrl = `https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(title)}&srlimit=20&utf8=&format=json&origin=*`;
const fetchData = await fetch(searchUrl);
console.debug(fetchData);
// console.debug(fetchData);
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json();
@ -56,14 +56,14 @@ export class WikipediaAPI extends APIModel {
const fetchData = await fetch(searchUrl);
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json();
console.debug(data);
// console.debug(data);
const result: any = Object.entries(data?.query?.pages)[0][1];
const model = new WikiModel({
return new WikiModel({
type: 'wiki',
title: result.title,
englishTitle: result.title,
@ -78,7 +78,5 @@ export class WikipediaAPI extends APIModel {
userData: {},
} as WikiModel);
return model;
}
}