Skip to content

Commit 75f3a59

Browse files
Merge pull request #131 from ltctceplrm/master
Added support for Moby Games API
2 parents 69ffd44 + d2d6d34 commit 75f3a59

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Now you select the result you want and the plugin will cast it's magic and creat
119119
| [Wikipedia](https://en.wikipedia.org/wiki/Main_Page) | The Wikipedia API allows access to all Wikipedia articles. | wiki articles | No | None | No |
120120
| [Steam](https://store.steampowered.com/) | The Steam API offers information on all steam games. | games | No | 10000 per day | No |
121121
| [Open Library](https://openlibrary.org) | The OpenLibrary API offers metadata for books | books | No | Cover access is rate-limited when not using CoverID or OLID by max 100 requests/IP every 5 minutes. This plugin uses OLID so there shouldn't be a rate limit. | No |
122+
| [Moby Games](https://www.mobygames.com) | The Moby Games API offers metadata for games for all platforms | games | Yes, by making an account [here](https://www.mobygames.com/user/register/) | API requests are limited to 360 per hour (one every ten seconds). In addition, requests should be made no more frequently than one per second. | No |
122123
123124
#### Notes
124125
@@ -152,6 +153,10 @@ Now you select the result you want and the plugin will cast it's magic and creat
152153
- The ID you need is the "work" ID and not the "book" ID, it needs to start with `/works/`. You can find this ID in the URL
153154
- e.g. for "Fantastic Mr. Fox" the URL looks like this `https://openlibrary.org/works/OL45804W` so the ID is `/works/OL45804W`
154155
- This URL is located near the top of the page above the title, see `An edition of Fantastic Mr Fox (1970) `
156+
- [Moby Games](https://www.mobygames.com)
157+
- you can find this ID in the URL
158+
- e.g. for "Bioshock 2" the URL looks like this `https://www.mobygames.com/game/45089/bioshock-2/` so the ID is `45089`
159+
155160
156161
### Problems, unexpected behavior or improvement suggestions?
157162

src/api/apis/MobyGamesAPI.ts

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

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)