Merge pull request #42 from CedricFinance/add-board-game-geek

Add BoardGameGeekAPI
This commit is contained in:
Moritz Jung 2022-08-29 13:08:27 +02:00 committed by GitHub
commit a0a2ef38bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 188 additions and 1 deletions

View file

@ -0,0 +1,104 @@
import {APIModel} from '../APIModel';
import {MediaTypeModel} from '../../models/MediaTypeModel';
import MediaDbPlugin from '../../main';
import {BoardGameModel} from 'src/models/BoardGameModel';
import {debugLog} from '../../utils/Utils';
import {requestUrl} from 'obsidian';
import {MediaType} from '../../utils/MediaType';
export class BoardGameGeekAPI extends APIModel {
plugin: MediaDbPlugin;
constructor(plugin: MediaDbPlugin) {
super();
this.plugin = plugin;
this.apiName = 'BoardGameGeekAPI';
this.apiDescription = 'A free API for BoardGameGeek things.';
this.apiUrl = 'https://api.geekdo.com/xmlapi';
this.types = ['boardgames'];
}
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
console.log(`MDB | api "${this.apiName}" queried by Title`);
const searchUrl = `${this.apiUrl}/search?search=${encodeURIComponent(title)}`;
const fetchData = await requestUrl({
url: searchUrl,
});
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
}
const data = fetchData.text;
const response = new window.DOMParser().parseFromString(data, "text/xml")
debugLog(response);
let ret: MediaTypeModel[] = [];
for (const boardgame of Array.from(response.querySelectorAll("boardgame"))) {
const id = boardgame.attributes.getNamedItem("objectid")!.value;
const title = boardgame.querySelector("name")!.textContent!;
const year = boardgame.querySelector("yearpublished")?.textContent ?? "";
ret.push(new BoardGameModel({
dataSource: this.apiName,
id,
title,
englishTitle: title,
year,
} as BoardGameModel));
}
return ret;
}
async getById(id: string): Promise<MediaTypeModel> {
console.log(`MDB | api "${this.apiName}" queried by ID`);
const searchUrl = `${this.apiUrl}/boardgame/${encodeURIComponent(id)}?stats=1`;
const fetchData = await requestUrl({
url: searchUrl,
});
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
}
const data = fetchData.text;
const response = new window.DOMParser().parseFromString(data, "text/xml")
debugLog(response);
const boardgame = response.querySelector("boardgame")!;
const title = boardgame.querySelector("name")!.textContent!;
const year = boardgame.querySelector("yearpublished")?.textContent ?? "";
const image = boardgame.querySelector("image")?.textContent ?? undefined;
const onlineRating = Number.parseFloat(boardgame.querySelector("statistics ratings average")?.textContent ?? "");
const genres = Array.from(boardgame.querySelectorAll("boardgamecategory")).map(n => n!.textContent!);
const model = new BoardGameModel({
type: MediaType.BoardGame,
title,
englishTitle: title,
year: year === "0" ? "" : year,
dataSource: this.apiName,
url: `https://boardgamegeek.com/boardgame/${id}`,
id,
genres,
onlineRating,
image,
released: true,
userData: {
played: false,
personalRating: 0,
},
} as BoardGameModel);
return model;
}
}