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