Merge pull request #47 from mProjectsCode/master
changes for release 0.3.2
This commit is contained in:
commit
7ae51676a2
18 changed files with 230 additions and 5 deletions
104
src/api/apis/BoardGameGeekAPI.ts
Normal file
104
src/api/apis/BoardGameGeekAPI.ts
Normal 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;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -100,7 +100,7 @@ export class SteamAPI extends APIModel {
|
|||
englishTitle: result.name,
|
||||
year: (new Date(result.release_date.date)).getFullYear().toString(),
|
||||
dataSource: this.apiName,
|
||||
url: `https://store.steampowered.com/app/${result.id}`,
|
||||
url: `https://store.steampowered.com/app/${result.steam_appid}`,
|
||||
id: result.steam_appid,
|
||||
|
||||
genres: result.genres?.map((x: any) => x.description) ?? [],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue