Steam API

This commit is contained in:
mProjectsCode 2022-05-29 20:01:33 +02:00
parent f6825bbadc
commit 5916ed66eb
3 changed files with 142 additions and 27 deletions

109
src/api/apis/SteamAPI.ts Normal file
View file

@ -0,0 +1,109 @@
import {APIModel} from '../APIModel';
import {MediaTypeModel} from '../../models/MediaTypeModel';
import {MovieModel} from '../../models/MovieModel';
import MediaDbPlugin from '../../main';
import {SeriesModel} from '../../models/SeriesModel';
import {GameModel} from '../../models/GameModel';
import {contactEmail, debugLog, pluginName} from '../../utils/Utils';
import {requestUrl} from 'obsidian';
import {MediaType} from '../../utils/MediaType';
export class SteamAPI extends APIModel {
plugin: MediaDbPlugin;
typeMappings: Map<string, string>;
constructor(plugin: MediaDbPlugin) {
super();
this.plugin = plugin;
this.apiName = 'SteamAPI';
this.apiDescription = 'A free API for all Steam games.';
this.apiUrl = 'http://www.steampowered.com/';
this.types = ['games'];
this.typeMappings = new Map<string, string>();
this.typeMappings.set('game', 'game');
}
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
console.log(`MDB | api "${this.apiName}" queried by Title`);
const searchUrl = `http://api.steampowered.com/ISteamApps/GetAppList/v0002/?format=json`;
const fetchData = await requestUrl({
url: searchUrl,
});
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
}
const data = await fetchData.json;
debugLog(data);
let filteredData = [];
for (const app of data.applist.apps) {
if (app.name.toLowerCase().includes(title.toLowerCase())) {
filteredData.push(app);
}
if (filteredData.length > 20) {
break;
}
}
let ret: MediaTypeModel[] = [];
for (const result of filteredData) {
ret.push(new GameModel({
type: MediaType.Game,
title: result.name,
englishTitle: result.name,
year: '',
dataSource: this.apiName,
id: result.appid,
} as GameModel));
}
return ret;
}
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
console.log(`MDB | api "${this.apiName}" queried by ID`);
const searchUrl = `http://store.steampowered.com/api/appdetails?appids=${item.id}`;
const fetchData = await requestUrl({
url: searchUrl,
});
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
}
const result = (await fetchData.json)[item.id].data;
debugLog(result);
const model = new GameModel({
type: MediaType.Game,
title: result.name,
englishTitle: result.name,
year: (new Date(result.release_date.date)).getFullYear().toString(),
dataSource: this.apiName,
url: `https://store.steampowered.com/app/${result.id}`,
id: result.steam_appid,
genres: result.genres?.map((x: any) => x.description) ?? [],
onlineRating: Number.parseFloat(result.metacritic?.score ?? 0),
image: result.header_image ?? '',
released: !result.release_date?.comming_soon,
releaseDate: (new Date(result.release_date?.date)).toLocaleDateString() ?? 'unknown',
played: false,
personalRating: 0,
} as GameModel);
return model;
}
}