|
| 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 | +} |
0 commit comments