Skip to content

Commit f5b9020

Browse files
committed
fix #70 #71 #68 #57
1 parent e489634 commit f5b9020

File tree

16 files changed

+225
-64
lines changed

16 files changed

+225
-64
lines changed

src/api/APIModel.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { MediaTypeModel } from '../models/MediaTypeModel';
2+
import { MediaType } from '../utils/MediaType';
23

34
export abstract class APIModel {
45
apiName: string;
56
apiUrl: string;
67
apiDescription: string;
7-
types: string[];
8+
types: MediaType[];
89

910
/**
1011
* This function should query the api and return a list of matches. The matches should be caped at 20.
@@ -15,12 +16,11 @@ export abstract class APIModel {
1516

1617
abstract getById(id: string): Promise<MediaTypeModel>;
1718

18-
hasType(type: string): boolean {
19+
hasType(type: MediaType): boolean {
1920
return this.types.contains(type);
2021
}
2122

22-
// for future use (https://github.com/mProjectsCode/obsidian-media-db-plugin/issues/5)
23-
hasTypeOverlap(types: string[]): boolean {
23+
hasTypeOverlap(types: MediaType[]): boolean {
2424
for (const type of types) {
2525
if (this.hasType(type)) {
2626
return true;

src/api/apis/BoardGameGeekAPI.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { APIModel } from '../APIModel';
22
import { MediaTypeModel } from '../../models/MediaTypeModel';
33
import MediaDbPlugin from '../../main';
44
import { BoardGameModel } from 'src/models/BoardGameModel';
5-
import { debugLog } from '../../utils/Utils';
65
import { requestUrl } from 'obsidian';
6+
import { MediaType } from '../../utils/MediaType';
77

88
export class BoardGameGeekAPI extends APIModel {
99
plugin: MediaDbPlugin;
@@ -15,7 +15,7 @@ export class BoardGameGeekAPI extends APIModel {
1515
this.apiName = 'BoardGameGeekAPI';
1616
this.apiDescription = 'A free API for BoardGameGeek things.';
1717
this.apiUrl = 'https://api.geekdo.com/xmlapi';
18-
this.types = ['boardgames'];
18+
this.types = [MediaType.BoardGame];
1919
}
2020

2121
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
@@ -33,13 +33,13 @@ export class BoardGameGeekAPI extends APIModel {
3333
const data = fetchData.text;
3434
const response = new window.DOMParser().parseFromString(data, 'text/xml');
3535

36-
debugLog(response);
36+
console.debug(response);
3737

3838
let ret: MediaTypeModel[] = [];
3939

4040
for (const boardgame of Array.from(response.querySelectorAll('boardgame'))) {
4141
const id = boardgame.attributes.getNamedItem('objectid')!.value;
42-
const title = boardgame.querySelector('name')!.textContent!;
42+
const title = boardgame.querySelector('name[primary=true]')?.textContent ?? boardgame.querySelector('name')!.textContent!;
4343
const year = boardgame.querySelector('yearpublished')?.textContent ?? '';
4444

4545
ret.push(
@@ -70,17 +70,22 @@ export class BoardGameGeekAPI extends APIModel {
7070

7171
const data = fetchData.text;
7272
const response = new window.DOMParser().parseFromString(data, 'text/xml');
73-
debugLog(response);
73+
console.debug(response);
7474

7575
const boardgame = response.querySelector('boardgame')!;
76-
const title = boardgame.querySelector('name')!.textContent!;
76+
const title = boardgame.querySelector('name[primary=true]')!.textContent!;
7777
const year = boardgame.querySelector('yearpublished')?.textContent ?? '';
7878
const image = boardgame.querySelector('image')?.textContent ?? undefined;
79-
const onlineRating = Number.parseFloat(boardgame.querySelector('statistics ratings average')?.textContent ?? '');
79+
const onlineRating = Number.parseFloat(boardgame.querySelector('statistics ratings average')?.textContent ?? '0');
8080
const genres = Array.from(boardgame.querySelectorAll('boardgamecategory')).map(n => n!.textContent!);
81+
const complexityRating = Number.parseFloat(boardgame.querySelector('averageweight')?.textContent ?? '0');
82+
const minPlayers = Number.parseFloat(boardgame.querySelector('minplayers')?.textContent ?? '0');
83+
const maxPlayers = Number.parseFloat(boardgame.querySelector('maxplayers')?.textContent ?? '0');
84+
const playtime = (boardgame.querySelector('playingtime')?.textContent ?? 'unknown') + ' minutes';
85+
const publishers = Array.from(boardgame.querySelectorAll('boardgamepublisher')).map(n => n!.textContent!);
8186

8287
const model = new BoardGameModel({
83-
title,
88+
title: title,
8489
englishTitle: title,
8590
year: year === '0' ? '' : year,
8691
dataSource: this.apiName,
@@ -89,7 +94,13 @@ export class BoardGameGeekAPI extends APIModel {
8994

9095
genres: genres,
9196
onlineRating: onlineRating,
97+
complexityRating: complexityRating,
98+
minPlayers: minPlayers,
99+
maxPlayers: maxPlayers,
100+
playtime: playtime,
101+
publishers: publishers,
92102
image: image,
103+
93104
released: true,
94105

95106
userData: {

src/api/apis/LocGovAPI.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ export class LocGovAPI extends APIModel {
2525

2626
const searchUrl = `https://www.loc.gov/search/?q=${encodeURIComponent(title)}&fo=json&c=20`;
2727
const fetchData = await fetch(searchUrl);
28-
debugLog(fetchData);
28+
console.debug(fetchData);
2929

3030
if (fetchData.status !== 200) {
3131
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
3232
}
3333

3434
const data = await fetchData.json();
35-
debugLog(data);
35+
console.debug(data);
3636
let ret: MediaTypeModel[] = [];
3737

3838
throw new Error('MDB | Under construction, API implementation not finished');
@@ -50,7 +50,7 @@ export class LocGovAPI extends APIModel {
5050
}
5151

5252
const data = await fetchData.json();
53-
debugLog(data);
53+
console.debug(data);
5454
const result = data.data;
5555

5656
const type = this.typeMappings.get(result.type.toLowerCase());

src/api/apis/MALAPI.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { MediaTypeModel } from '../../models/MediaTypeModel';
33
import { MovieModel } from '../../models/MovieModel';
44
import MediaDbPlugin from '../../main';
55
import { SeriesModel } from '../../models/SeriesModel';
6-
import { debugLog } from '../../utils/Utils';
6+
import { MediaType } from '../../utils/MediaType';
77

88
export class MALAPI extends APIModel {
99
plugin: MediaDbPlugin;
@@ -16,7 +16,7 @@ export class MALAPI extends APIModel {
1616
this.apiName = 'MALAPI';
1717
this.apiDescription = 'A free API for Anime. Some results may take a long time to load.';
1818
this.apiUrl = 'https://jikan.moe/';
19-
this.types = ['movie', 'series', 'anime'];
19+
this.types = [MediaType.Movie, MediaType.Series];
2020
this.typeMappings = new Map<string, string>();
2121
this.typeMappings.set('movie', 'movie');
2222
this.typeMappings.set('special', 'special');
@@ -30,13 +30,13 @@ export class MALAPI extends APIModel {
3030
const searchUrl = `https://api.jikan.moe/v4/anime?q=${encodeURIComponent(title)}&limit=20${this.plugin.settings.sfwFilter ? '&sfw' : ''}`;
3131

3232
const fetchData = await fetch(searchUrl);
33-
debugLog(fetchData);
33+
console.debug(fetchData);
3434
if (fetchData.status !== 200) {
3535
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
3636
}
3737
const data = await fetchData.json();
3838

39-
debugLog(data);
39+
console.debug(data);
4040

4141
let ret: MediaTypeModel[] = [];
4242

@@ -85,15 +85,15 @@ export class MALAPI extends APIModel {
8585
async getById(id: string): Promise<MediaTypeModel> {
8686
console.log(`MDB | api "${this.apiName}" queried by ID`);
8787

88-
const searchUrl = `https://api.jikan.moe/v4/anime/${encodeURIComponent(id)}`;
88+
const searchUrl = `https://api.jikan.moe/v4/anime/${encodeURIComponent(id)}/full`;
8989
const fetchData = await fetch(searchUrl);
9090

9191
if (fetchData.status !== 200) {
9292
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
9393
}
9494

9595
const data = await fetchData.json();
96-
debugLog(data);
96+
console.debug(data);
9797
const result = data.data;
9898

9999
const type = this.typeMappings.get(result.type?.toLowerCase());
@@ -111,10 +111,12 @@ export class MALAPI extends APIModel {
111111
producer: result.studios?.map((x: any) => x.name).join(', ') ?? 'unknown',
112112
duration: result.duration ?? 'unknown',
113113
onlineRating: result.score ?? 0,
114+
actors: [],
114115
image: result.images?.jpg?.image_url ?? '',
115116

116117
released: true,
117118
premiere: new Date(result.aired?.from).toLocaleDateString() ?? 'unknown',
119+
streamingServices: result.streaming?.map((x: any) => x.name) ?? [],
118120

119121
userData: {
120122
watched: false,
@@ -140,10 +142,12 @@ export class MALAPI extends APIModel {
140142
producer: result.studios?.map((x: any) => x.name).join(', ') ?? 'unknown',
141143
duration: result.duration ?? 'unknown',
142144
onlineRating: result.score ?? 0,
145+
actors: [],
143146
image: result.images?.jpg?.image_url ?? '',
144147

145148
released: true,
146149
premiere: new Date(result.aired?.from).toLocaleDateString() ?? 'unknown',
150+
streamingServices: result.streaming?.map((x: any) => x.name) ?? [],
147151

148152
userData: {
149153
watched: false,
@@ -168,6 +172,7 @@ export class MALAPI extends APIModel {
168172
episodes: result.episodes,
169173
duration: result.duration ?? 'unknown',
170174
onlineRating: result.score ?? 0,
175+
streamingServices: result.streaming?.map((x: any) => x.name) ?? [],
171176
image: result.images?.jpg?.image_url ?? '',
172177

173178
released: true,

src/api/apis/MusicBrainzAPI.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { MediaTypeModel } from '../../models/MediaTypeModel';
33
import MediaDbPlugin from '../../main';
44
import { requestUrl } from 'obsidian';
55
import { MusicReleaseModel } from '../../models/MusicReleaseModel';
6-
import { contactEmail, debugLog, mediaDbVersion, pluginName } from '../../utils/Utils';
6+
import { contactEmail, mediaDbVersion, pluginName } from '../../utils/Utils';
7+
import { MediaType } from '../../utils/MediaType';
78

89
export class MusicBrainzAPI extends APIModel {
910
plugin: MediaDbPlugin;
@@ -15,7 +16,7 @@ export class MusicBrainzAPI extends APIModel {
1516
this.apiName = 'MusicBrainz API';
1617
this.apiDescription = 'Free API for music albums.';
1718
this.apiUrl = 'https://musicbrainz.org/';
18-
this.types = ['music'];
19+
this.types = [MediaType.MusicRelease];
1920
}
2021

2122
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
@@ -30,14 +31,14 @@ export class MusicBrainzAPI extends APIModel {
3031
},
3132
});
3233

33-
debugLog(fetchData);
34+
console.debug(fetchData);
3435

3536
if (fetchData.status !== 200) {
3637
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
3738
}
3839

3940
const data = await fetchData.json;
40-
debugLog(data);
41+
console.debug(data);
4142
let ret: MediaTypeModel[] = [];
4243

4344
for (const result of data['release-groups']) {
@@ -76,7 +77,7 @@ export class MusicBrainzAPI extends APIModel {
7677
}
7778

7879
const data = await fetchData.json;
79-
debugLog(data);
80+
console.debug(data);
8081
const result = data;
8182

8283
const model = new MusicReleaseModel({

src/api/apis/OMDbAPI.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { MovieModel } from '../../models/MovieModel';
44
import MediaDbPlugin from '../../main';
55
import { SeriesModel } from '../../models/SeriesModel';
66
import { GameModel } from '../../models/GameModel';
7-
import { debugLog } from '../../utils/Utils';
7+
import { MediaType } from '../../utils/MediaType';
88

99
export class OMDbAPI extends APIModel {
1010
plugin: MediaDbPlugin;
@@ -17,7 +17,7 @@ export class OMDbAPI extends APIModel {
1717
this.apiName = 'OMDbAPI';
1818
this.apiDescription = 'A free API for Movies, Series and Games.';
1919
this.apiUrl = 'http://www.omdbapi.com/';
20-
this.types = ['movie', 'series'];
20+
this.types = [MediaType.Movie, MediaType.Series, MediaType.Game];
2121
this.typeMappings = new Map<string, string>();
2222
this.typeMappings.set('movie', 'movie');
2323
this.typeMappings.set('series', 'series');
@@ -46,7 +46,7 @@ export class OMDbAPI extends APIModel {
4646
return [];
4747
}
4848

49-
debugLog(data.Search);
49+
console.debug(data.Search);
5050

5151
let ret: MediaTypeModel[] = [];
5252

@@ -108,7 +108,7 @@ export class OMDbAPI extends APIModel {
108108
}
109109

110110
const result = await fetchData.json();
111-
debugLog(result);
111+
console.debug(result);
112112

113113
if (result.Response === 'False') {
114114
throw Error(`MDB | Received error from ${this.apiName}: ${result.Error}`);
@@ -133,9 +133,11 @@ export class OMDbAPI extends APIModel {
133133
producer: result.Director ?? 'unknown',
134134
duration: result.Runtime ?? 'unknown',
135135
onlineRating: Number.parseFloat(result.imdbRating ?? 0),
136+
actors: result.Actors?.split(', ') ?? [],
136137
image: result.Poster ?? '',
137138

138139
released: true,
140+
streamingServices: [],
139141
premiere: new Date(result.Released).toLocaleDateString() ?? 'unknown',
140142

141143
userData: {
@@ -161,9 +163,11 @@ export class OMDbAPI extends APIModel {
161163
episodes: 0,
162164
duration: result.Runtime ?? 'unknown',
163165
onlineRating: Number.parseFloat(result.imdbRating ?? 0),
166+
actors: result.Actors?.split(', ') ?? [],
164167
image: result.Poster ?? '',
165168

166169
released: true,
170+
streamingServices: [],
167171
airing: false,
168172
airedFrom: new Date(result.Released).toLocaleDateString() ?? 'unknown',
169173
airedTo: 'unknown',

src/api/apis/SteamAPI.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { APIModel } from '../APIModel';
22
import { MediaTypeModel } from '../../models/MediaTypeModel';
33
import MediaDbPlugin from '../../main';
44
import { GameModel } from '../../models/GameModel';
5-
import { debugLog } from '../../utils/Utils';
65
import { requestUrl } from 'obsidian';
76
import { MediaType } from '../../utils/MediaType';
87

@@ -17,7 +16,7 @@ export class SteamAPI extends APIModel {
1716
this.apiName = 'SteamAPI';
1817
this.apiDescription = 'A free API for all Steam games.';
1918
this.apiUrl = 'http://www.steampowered.com/';
20-
this.types = ['games'];
19+
this.types = [MediaType.Game];
2120
this.typeMappings = new Map<string, string>();
2221
this.typeMappings.set('game', 'game');
2322
}
@@ -36,7 +35,7 @@ export class SteamAPI extends APIModel {
3635

3736
const data = await fetchData.json;
3837

39-
debugLog(data);
38+
console.debug(data);
4039

4140
let filteredData = [];
4241

@@ -79,7 +78,7 @@ export class SteamAPI extends APIModel {
7978
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
8079
}
8180

82-
debugLog(await fetchData.json);
81+
console.debug(await fetchData.json);
8382

8483
let result: any;
8584
for (const [key, value] of Object.entries(await fetchData.json)) {
@@ -94,7 +93,7 @@ export class SteamAPI extends APIModel {
9493
throw Error(`MDB | API returned invalid data.`);
9594
}
9695

97-
debugLog(result);
96+
console.debug(result);
9897

9998
const model = new GameModel({
10099
type: MediaType.Game,

0 commit comments

Comments
 (0)