Added support for giant bomb api and modified the readme

Added a note about mobygames per #164
This commit is contained in:
ltctceplrm 2024-09-22 01:26:02 +02:00
parent 265bf10426
commit ee19791dba
4 changed files with 131 additions and 2 deletions

View file

@ -0,0 +1,109 @@
import { APIModel } from '../APIModel';
import { MediaTypeModel } from '../../models/MediaTypeModel';
import MediaDbPlugin from '../../main';
import { GameModel } from '../../models/GameModel';
import { requestUrl } from 'obsidian';
import { MediaType } from '../../utils/MediaType';
export class GiantBombAPI extends APIModel {
plugin: MediaDbPlugin;
apiDateFormat: string = 'YYYY-MM-DD';
constructor(plugin: MediaDbPlugin) {
super();
this.plugin = plugin;
this.apiName = 'GiantBombAPI';
this.apiDescription = 'A free API for games.';
this.apiUrl = 'https://www.giantbomb.com/api';
this.types = [MediaType.Game];
}
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
console.log(`MDB | api "${this.apiName}" queried by Title`);
if (!this.plugin.settings.GiantBombKey) {
throw Error(`MDB | API key for ${this.apiName} missing.`);
}
const searchUrl = `${this.apiUrl}/games?api_key=${this.plugin.settings.GiantBombKey}&filter=name:${encodeURIComponent(title)}&format=json`;
const fetchData = await requestUrl({
url: searchUrl,
});
// console.debug(fetchData);
if (fetchData.status === 401) {
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
}
if (fetchData.status === 429) {
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 ${this.apiName}.`);
}
const data = await fetchData.json;
// console.debug(data);
const ret: MediaTypeModel[] = [];
for (const result of data.results) {
ret.push(
new GameModel({
type: MediaType.Game,
title: result.name,
englishTitle: result.name,
year: new Date(result.original_release_date).getFullYear().toString(),
dataSource: this.apiName,
id: result.guid,
} as GameModel),
);
}
return ret;
}
async getById(id: string): Promise<MediaTypeModel> {
console.log(`MDB | api "${this.apiName}" queried by ID`);
if (!this.plugin.settings.GiantBombKey) {
throw Error(`MDB | API key for ${this.apiName} missing.`);
}
const searchUrl = `${this.apiUrl}/game/${encodeURIComponent(id)}/?api_key=${this.plugin.settings.GiantBombKey}&format=json`;
const fetchData = await requestUrl({
url: searchUrl,
});
console.debug(fetchData);
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
}
const data = await fetchData.json;
// console.debug(data);
const result = data.results;
return new GameModel({
type: MediaType.Game,
title: result.name,
englishTitle: result.name,
year: new Date(result.original_release_date).getFullYear().toString(),
dataSource: this.apiName,
url: result.site_detail_url,
id: result.guid,
developers: result.developers?.map((x: any) => x.name) ?? [],
publishers: result.publishers?.map((x: any) => x.name) ?? [],
genres: result.genres?.map((x: any) => x.name) ?? [],
onlineRating: 0,
image: result.image?.super_url ?? '',
released: true,
releaseDate: result.original_release_date ?? 'unknown',
userData: {
played: false,
personalRating: 0,
},
} as GameModel);
}
}

View file

@ -21,6 +21,7 @@ import { SteamAPI } from './api/apis/SteamAPI';
import { BoardGameGeekAPI } from './api/apis/BoardGameGeekAPI';
import { OpenLibraryAPI } from './api/apis/OpenLibraryAPI';
import { MobyGamesAPI } from './api/apis/MobyGamesAPI';
import { GiantBombAPI } from './api/apis/GiantBombAPI';
import { PropertyMapper } from './settings/PropertyMapper';
import { MediaDbFolderImportModal } from './modals/MediaDbFolderImportModal';
import { PropertyMapping, PropertyMappingModel } from './settings/PropertyMapping';
@ -58,6 +59,7 @@ export default class MediaDbPlugin extends Plugin {
this.apiManager.registerAPI(new BoardGameGeekAPI(this));
this.apiManager.registerAPI(new OpenLibraryAPI(this));
this.apiManager.registerAPI(new MobyGamesAPI(this));
this.apiManager.registerAPI(new GiantBombAPI(this));
// this.apiManager.registerAPI(new LocGovAPI(this)); // TODO: parse data
this.mediaTypeManager = new MediaTypeManager();

View file

@ -12,6 +12,7 @@ import { fragWithHTML } from '../utils/Utils';
export interface MediaDbPluginSettings {
OMDbKey: string;
MobyGamesKey: string;
GiantBombKey: string;
sfwFilter: boolean;
templates: boolean;
customDateFormat: string;
@ -78,6 +79,7 @@ export interface MediaDbPluginSettings {
const DEFAULT_SETTINGS: MediaDbPluginSettings = {
OMDbKey: '',
MobyGamesKey: '',
GiantBombKey: '',
sfwFilter: true,
templates: true,
customDateFormat: 'L',
@ -191,7 +193,7 @@ export class MediaDbSettingTab extends PluginSettingTab {
});
});
new Setting(containerEl)
new Setting(containerEl)
.setName('Moby Games key')
.setDesc('API key for "www.mobygames.com".')
.addText(cb => {
@ -203,6 +205,18 @@ export class MediaDbSettingTab extends PluginSettingTab {
});
});
new Setting(containerEl)
.setName('Giant Bomb Key')
.setDesc('API key for "www.giantbomb.com".')
.addText(cb => {
cb.setPlaceholder('API key')
.setValue(this.plugin.settings.GiantBombKey)
.onChange(data => {
this.plugin.settings.GiantBombKey = data;
void this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName('SFW filter')
.setDesc('Only shows SFW results for APIs that offer filtering.')