fix some issues; migrate to bun'

This commit is contained in:
Moritz Jung 2024-04-11 22:30:55 +02:00
parent cf3d5c6d6f
commit 54293ac3a1
56 changed files with 1360 additions and 764 deletions

View file

@ -21,8 +21,12 @@ export class APIManager {
for (const api of this.apis) {
if (apisToQuery.contains(api.apiName)) {
const apiRes = await api.searchByTitle(query);
res = res.concat(apiRes);
try {
const apiRes = await api.searchByTitle(query);
res = res.concat(apiRes);
} catch (e) {
console.warn(e);
}
}
}

View file

@ -1,6 +1,5 @@
import { MediaTypeModel } from '../models/MediaTypeModel';
import { MediaType } from '../utils/MediaType';
import { MediaDbPluginSettings } from 'src/settings/Settings';
import MediaDbPlugin from '../main';
export abstract class APIModel {
@ -20,7 +19,10 @@ export abstract class APIModel {
abstract getById(id: string): Promise<MediaTypeModel>;
hasType(type: MediaType): boolean {
if (this.types.contains(type) && (Boolean((this.plugin.settings.apiToggle as any)?.[this.apiName]?.[type] as MediaDbPluginSettings) === true || (this.plugin.settings.apiToggle as any)?.[this.apiName]?.[type] as MediaDbPluginSettings === undefined)) {
if (
this.types.contains(type) &&
(Boolean((this.plugin.settings.apiToggle as any)?.[this.apiName]?.[type]) === true || (this.plugin.settings.apiToggle as any)?.[this.apiName]?.[type] === undefined)
) {
return true;
}
}

View file

@ -1,64 +0,0 @@
import { APIModel } from '../APIModel';
import { MediaTypeModel } from '../../models/MediaTypeModel';
import MediaDbPlugin from '../../main';
// WIP
export class LocGovAPI extends APIModel {
plugin: MediaDbPlugin;
typeMappings: Map<string, string>;
constructor(plugin: MediaDbPlugin) {
super();
this.plugin = plugin;
this.apiName = 'loc.gov API';
this.apiDescription = 'A free API for the Library of Congress collections.';
this.apiUrl = 'https://libraryofcongress.github.io/data-exploration/index.html';
this.types = [];
this.typeMappings = new Map<string, string>();
// this.typeMappings.set('movie', 'movie');
}
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
console.log(`MDB | api "${this.apiName}" queried by Title`);
const searchUrl = `https://www.loc.gov/search/?q=${encodeURIComponent(title)}&fo=json&c=20`;
const fetchData = await fetch(searchUrl);
console.debug(fetchData);
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
}
const data = await fetchData.json();
console.debug(data);
let ret: MediaTypeModel[] = [];
throw new Error('MDB | Under construction, API implementation not finished');
// return ret;
}
async getById(id: string): Promise<MediaTypeModel> {
console.log(`MDB | api "${this.apiName}" queried by ID`);
const searchUrl = `https://www.loc.gov/item/${encodeURIComponent(id)}/?fo=json`;
const fetchData = await fetch(searchUrl);
if (fetchData.status !== 200) {
throw Error(`MDB | Received status code ${fetchData.status} from an API.`);
}
const data = await fetchData.json();
console.debug(data);
const result = data.data;
const type = this.typeMappings.get(result.type.toLowerCase());
if (type === undefined) {
throw Error(`${result.type.toLowerCase()} is an unsupported type.`);
}
throw new Error('MDB | Under construction, API implementation not finished');
// return;
}
}

View file

@ -41,7 +41,13 @@ export class SteamAPI extends APIModel {
const filteredData = [];
for (const app of data.applist.apps) {
if (app.name.toLowerCase().includes(title.toLowerCase())) {
if (
app.name
.normalize('NFD')
.replace(/\p{Diacritic}/gu, '')
.toLowerCase()
.includes(title.toLowerCase())
) {
filteredData.push(app);
}
if (filteredData.length > 20) {