Skip to content

Commit e75bb5b

Browse files
committed
Added support for Moby Games API
1 parent 73a8a62 commit e75bb5b

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

src/api/apis/MobyGamesAPI.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { APIModel } from '../APIModel';
2+
import { MediaTypeModel } from '../../models/MediaTypeModel';
3+
import MediaDbPlugin from '../../main';
4+
import { GameModel } from '../../models/GameModel';
5+
import { requestUrl } from 'obsidian';
6+
import { MediaType } from '../../utils/MediaType';
7+
8+
export class MobyGamesAPI extends APIModel {
9+
plugin: MediaDbPlugin;
10+
11+
constructor(plugin: MediaDbPlugin) {
12+
super();
13+
14+
this.plugin = plugin;
15+
this.apiName = 'MobyGamesAPI';
16+
this.apiDescription = 'A free API for games.';
17+
this.apiUrl = 'https://api.mobygames.com/v1';
18+
this.types = [MediaType.Game];
19+
}
20+
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
21+
console.log(`MDB | api "${this.apiName}" queried by Title`);
22+
23+
const searchUrl = `${this.apiUrl}/games?title=${encodeURIComponent(title)}&api_key=${this.plugin.settings.MobyGamesKey}`;
24+
const fetchData = await requestUrl({
25+
url: searchUrl,
26+
});
27+
28+
console.debug(fetchData);
29+
30+
if (fetchData.status === 401) {
31+
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
32+
}
33+
if (fetchData.status === 429) {
34+
throw Error(`MDB | Too many requests for ${this.apiName}, you've exceeded your API quota.`);
35+
}
36+
if (fetchData.status !== 200) {
37+
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
38+
}
39+
40+
const data = await fetchData.json;
41+
console.debug(data);
42+
const ret: MediaTypeModel[] = [];
43+
for (const result of data.games) {
44+
ret.push(
45+
new GameModel({
46+
type: MediaType.Game,
47+
title: result.title,
48+
englishTitle: result.title,
49+
year: result.platforms[0].first_release_date ?? '',
50+
dataSource: this.apiName,
51+
id: result.game_id,
52+
53+
} as GameModel),
54+
);
55+
}
56+
57+
return ret;
58+
}
59+
60+
async getById(id: string): Promise<MediaTypeModel> {
61+
console.log(`MDB | api "${this.apiName}" queried by ID`);
62+
63+
const searchUrl = `${this.apiUrl}/games?id=${encodeURIComponent(id)}&api_key=${this.plugin.settings.MobyGamesKey}`;
64+
const fetchData = await requestUrl({
65+
url: searchUrl,
66+
});
67+
console.debug(fetchData);
68+
69+
if (fetchData.status !== 200) {
70+
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
71+
}
72+
73+
const data = await fetchData.json;
74+
console.debug(data);
75+
const result = data.games[0];
76+
77+
const model = new GameModel({
78+
type: MediaType.Game,
79+
title: result.title,
80+
englishTitle: result.title,
81+
year: result.platforms[0].first_release_date,
82+
dataSource: this.apiName,
83+
url: `https://www.mobygames.com/game/${result.game_id}`,
84+
id: result.game_id,
85+
developers: result['developers'],
86+
publishers: result['publishers'],
87+
genres: result.genres?.map((x: any) => x.genre_name) ?? [],
88+
onlineRating: result.moby_score,
89+
image: result.sample_cover.image ?? '',
90+
91+
released: true,
92+
releaseDate: result.platforms[0].first_release_date ?? 'unknown',
93+
94+
userData: {
95+
played: false,
96+
97+
personalRating: 0,
98+
},
99+
} as GameModel);
100+
101+
return model;
102+
}
103+
}

src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { MEDIA_TYPES, MediaTypeManager } from './utils/MediaTypeManager';
2020
import { SteamAPI } from './api/apis/SteamAPI';
2121
import { BoardGameGeekAPI } from './api/apis/BoardGameGeekAPI';
2222
import { OpenLibraryAPI } from './api/apis/OpenLibraryAPI';
23+
import { MobyGamesAPI } from './api/apis/MobyGamesAPI';
2324
import { PropertyMapper } from './settings/PropertyMapper';
2425
import { YAMLConverter } from './utils/YAMLConverter';
2526
import { MediaDbFolderImportModal } from './modals/MediaDbFolderImportModal';
@@ -48,6 +49,7 @@ export default class MediaDbPlugin extends Plugin {
4849
this.apiManager.registerAPI(new SteamAPI(this));
4950
this.apiManager.registerAPI(new BoardGameGeekAPI(this));
5051
this.apiManager.registerAPI(new OpenLibraryAPI(this));
52+
this.apiManager.registerAPI(new MobyGamesAPI(this));
5153
// this.apiManager.registerAPI(new LocGovAPI(this)); // TODO: parse data
5254

5355
this.mediaTypeManager = new MediaTypeManager();

src/settings/Settings.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { fragWithHTML } from '../utils/Utils';
1111

1212
export interface MediaDbPluginSettings {
1313
OMDbKey: string;
14+
MobyGamesKey: string;
1415
sfwFilter: boolean;
1516
useCustomYamlStringifier: boolean;
1617
templates: boolean;
@@ -60,6 +61,7 @@ export interface MediaDbPluginSettings {
6061

6162
const DEFAULT_SETTINGS: MediaDbPluginSettings = {
6263
OMDbKey: '',
64+
MobyGamesKey: '',
6365
sfwFilter: true,
6466
useCustomYamlStringifier: true,
6567
templates: true,
@@ -160,6 +162,18 @@ export class MediaDbSettingTab extends PluginSettingTab {
160162
});
161163
});
162164

165+
new Setting(containerEl)
166+
.setName('Moby Games key')
167+
.setDesc('API key for "www.mobygames.com".')
168+
.addText(cb => {
169+
cb.setPlaceholder('API key')
170+
.setValue(this.plugin.settings.MobyGamesKey)
171+
.onChange(data => {
172+
this.plugin.settings.MobyGamesKey = data;
173+
this.plugin.saveSettings();
174+
});
175+
});
176+
163177
new Setting(containerEl)
164178
.setName('SFW filter')
165179
.setDesc('Only shows SFW results for APIs that offer filtering.')

0 commit comments

Comments
 (0)