added basic MAL api

This commit is contained in:
mProjectsCode 2022-05-06 11:36:22 +02:00
parent 30d07f79f3
commit 413a855e07
6 changed files with 114 additions and 7 deletions

79
src/api/apis/MALAPI.ts Normal file
View file

@ -0,0 +1,79 @@
import {APIModel} from '../APIModel';
import {MediaTypeModel} from '../../models/MediaTypeModel';
import {MovieModel} from '../../models/MovieModel';
import MediaDbPlugin from '../../main';
// @ts-ignore
import Jikanjs from '@mateoaranda/jikanjs';
export class MALAPI extends APIModel {
plugin: MediaDbPlugin;
constructor(plugin: MediaDbPlugin) {
super();
this.plugin = plugin;
this.apiName = 'MALAPI';
this.apiDescription = 'A free API for Anime.';
this.apiUrl = 'https://jikan.moe/';
this.types = ['movie', 'series', 'anime'];
}
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
console.log(`MDB | api "${this.apiName}" queried`);
const searchUrl = `https://api.jikan.moe/v4/anime?q=${title}&limit=20`;
const fetchData = await fetch(searchUrl);
console.log(fetchData);
if (fetchData.status !== 200) {
throw Error(`Received status code ${fetchData.status} from an API.`);
}
const data = await fetchData.json();
console.log(data);
let ret: MediaTypeModel[] = [];
for (const value of data.data) {
// if (value.Type === 'movie') {
ret.push(new MovieModel({
type: value.type,
title: value.title,
year: value.year,
dataSource: this.apiName,
id: value.mal_id,
} as MovieModel));
// }
}
return ret;
}
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
const searchUrl = `https://api.jikan.moe/v4/anime/${item.id}`;
const fetchData = await fetch(searchUrl);
if (fetchData.status !== 200) {
throw Error(`Received status code ${fetchData.status} from an API.`);
}
const data = await fetchData.json();
console.log(data);
const result = data.data;
// if (data.Type === 'movie') {
const model = new MovieModel({
type: result.type,
title: result.title,
year: result.year,
dataSource: this.apiName,
id: result.mal_id,
} as MovieModel);
return model;
// }
// return;
}
}

View file

@ -37,7 +37,13 @@ export class OMDbAPI extends APIModel {
for (const value of data.Search) {
if (value.Type === 'movie') {
ret.push(new MovieModel({title: value.Title, id: value.imdbID, dataSource: this.apiName, type: 'movie', premiere: value.Year} as MovieModel));
ret.push(new MovieModel({
type: 'movie',
title: value.Title,
year: value.Year,
dataSource: this.apiName,
id: value.imdbID,
} as MovieModel));
}
}
@ -54,10 +60,29 @@ export class OMDbAPI extends APIModel {
}
const data = await fetchData.json();
if (data.Type === 'movie') {
const model = new MovieModel({title: data.Title, id: data.imdbID, dataSource: this.apiName, type: 'movie', premiere: data.Year} as MovieModel);
console.log(data);
// TODO: mapping
if (data.Type === 'movie') {
const model = new MovieModel({
type: 'movie',
title: data.Title,
year: data.Year,
dataSource: this.apiName,
id: data.imdbID,
genres: data.Genre?.split(', ') ?? [],
producer: data.Director ?? 'unknown',
duration: data.Runtime ?? 'unknown',
onlineRating: Number.parseFloat(data.imdbRating ?? 0),
image: data.Poster ?? '',
released: true,
premiere: data.Released ?? 'unknown',
watched: false,
lastWatched: '',
personalRating: 0,
} as MovieModel);
return model;
}