|
| 1 | +import {APIModel} from '../APIModel'; |
| 2 | +import {MediaTypeModel} from '../../models/MediaTypeModel'; |
| 3 | +import MediaDbPlugin from '../../main'; |
| 4 | +import {BoardGameModel} from 'src/models/BoardGameModel'; |
| 5 | +import {debugLog} from '../../utils/Utils'; |
| 6 | +import {requestUrl} from 'obsidian'; |
| 7 | +import {MediaType} from '../../utils/MediaType'; |
| 8 | + |
| 9 | +export class BoardGameGeekAPI extends APIModel { |
| 10 | + plugin: MediaDbPlugin; |
| 11 | + |
| 12 | + constructor(plugin: MediaDbPlugin) { |
| 13 | + super(); |
| 14 | + |
| 15 | + this.plugin = plugin; |
| 16 | + this.apiName = 'BoardGameGeekAPI'; |
| 17 | + this.apiDescription = 'A free API for BoardGameGeek things.'; |
| 18 | + this.apiUrl = 'https://api.geekdo.com/xmlapi'; |
| 19 | + this.types = ['boardgames']; |
| 20 | + } |
| 21 | + |
| 22 | + async searchByTitle(title: string): Promise<MediaTypeModel[]> { |
| 23 | + console.log(`MDB | api "${this.apiName}" queried by Title`); |
| 24 | + |
| 25 | + const searchUrl = `${this.apiUrl}/search?search=${encodeURIComponent(title)}`; |
| 26 | + const fetchData = await requestUrl({ |
| 27 | + url: searchUrl, |
| 28 | + }); |
| 29 | + |
| 30 | + if (fetchData.status !== 200) { |
| 31 | + throw Error(`MDB | Received status code ${fetchData.status} from an API.`); |
| 32 | + } |
| 33 | + |
| 34 | + const data = fetchData.text; |
| 35 | + const response = new window.DOMParser().parseFromString(data, "text/xml") |
| 36 | + |
| 37 | + debugLog(response); |
| 38 | + |
| 39 | + let ret: MediaTypeModel[] = []; |
| 40 | + |
| 41 | + for (const boardgame of Array.from(response.querySelectorAll("boardgame"))) { |
| 42 | + const id = boardgame.attributes.getNamedItem("objectid")!.value; |
| 43 | + const title = boardgame.querySelector("name")!.textContent!; |
| 44 | + const year = boardgame.querySelector("yearpublished")?.textContent ?? ""; |
| 45 | + |
| 46 | + ret.push(new BoardGameModel({ |
| 47 | + dataSource: this.apiName, |
| 48 | + id, |
| 49 | + title, |
| 50 | + englishTitle: title, |
| 51 | + year, |
| 52 | + } as BoardGameModel)); |
| 53 | + } |
| 54 | + |
| 55 | + return ret; |
| 56 | + } |
| 57 | + |
| 58 | + async getById(id: string): Promise<MediaTypeModel> { |
| 59 | + console.log(`MDB | api "${this.apiName}" queried by ID`); |
| 60 | + |
| 61 | + const searchUrl = `${this.apiUrl}/boardgame/${encodeURIComponent(id)}?stats=1`; |
| 62 | + const fetchData = await requestUrl({ |
| 63 | + url: searchUrl, |
| 64 | + }); |
| 65 | + |
| 66 | + if (fetchData.status !== 200) { |
| 67 | + throw Error(`MDB | Received status code ${fetchData.status} from an API.`); |
| 68 | + } |
| 69 | + |
| 70 | + const data = fetchData.text; |
| 71 | + const response = new window.DOMParser().parseFromString(data, "text/xml") |
| 72 | + debugLog(response); |
| 73 | + |
| 74 | + const boardgame = response.querySelector("boardgame")!; |
| 75 | + const title = boardgame.querySelector("name")!.textContent!; |
| 76 | + const year = boardgame.querySelector("yearpublished")?.textContent ?? ""; |
| 77 | + const image = boardgame.querySelector("image")?.textContent ?? undefined; |
| 78 | + const onlineRating = Number.parseFloat(boardgame.querySelector("statistics ratings average")?.textContent ?? ""); |
| 79 | + const genres = Array.from(boardgame.querySelectorAll("boardgamecategory")).map(n => n!.textContent!); |
| 80 | + |
| 81 | + const model = new BoardGameModel({ |
| 82 | + type: MediaType.BoardGame, |
| 83 | + title, |
| 84 | + englishTitle: title, |
| 85 | + year: year === "0" ? "" : year, |
| 86 | + dataSource: this.apiName, |
| 87 | + url: `https://boardgamegeek.com/boardgame/${id}`, |
| 88 | + id, |
| 89 | + |
| 90 | + genres, |
| 91 | + onlineRating, |
| 92 | + image, |
| 93 | + released: true, |
| 94 | + |
| 95 | + userData: { |
| 96 | + played: false, |
| 97 | + personalRating: 0, |
| 98 | + }, |
| 99 | + } as BoardGameModel); |
| 100 | + |
| 101 | + return model; |
| 102 | + |
| 103 | + } |
| 104 | +} |
0 commit comments