added basic MAL api
This commit is contained in:
parent
30d07f79f3
commit
413a855e07
6 changed files with 114 additions and 7 deletions
79
src/api/apis/MALAPI.ts
Normal file
79
src/api/apis/MALAPI.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import {getFileName} from './utils/Utils';
|
|||
import {OMDbAPI} from './api/apis/OMDbAPI';
|
||||
import {MediaDbAdvancedSearchModal} from './modals/MediaDbAdvancedSearchModal';
|
||||
import {MediaDbSearchResultModal} from './modals/MediaDbSearchResultModal';
|
||||
import {MALAPI} from './api/apis/MALAPI';
|
||||
|
||||
export default class MediaDbPlugin extends Plugin {
|
||||
settings: MediaDbPluginSettings;
|
||||
|
|
@ -36,6 +37,7 @@ export default class MediaDbPlugin extends Plugin {
|
|||
// register APIs
|
||||
this.apiManager.registerAPI(new TestAPI());
|
||||
this.apiManager.registerAPI(new OMDbAPI(this));
|
||||
this.apiManager.registerAPI(new MALAPI(this));
|
||||
}
|
||||
|
||||
async createMediaDbNote(): Promise<void> {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
export abstract class MediaTypeModel {
|
||||
type: string;
|
||||
title: string;
|
||||
premiere: string;
|
||||
year: string;
|
||||
dataSource: string;
|
||||
id: string;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import {stringifyYaml} from 'obsidian';
|
|||
export class MovieModel extends MediaTypeModel {
|
||||
type: string;
|
||||
title: string;
|
||||
premiere: string;
|
||||
year: string;
|
||||
dataSource: string;
|
||||
id: string;
|
||||
|
||||
|
|
@ -16,6 +16,7 @@ export class MovieModel extends MediaTypeModel {
|
|||
image: string;
|
||||
|
||||
released: boolean;
|
||||
premiere: string;
|
||||
|
||||
watched: boolean;
|
||||
lastWatched: string;
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ export function sleep(ms: number) {
|
|||
}
|
||||
|
||||
export function getFileName(item: MediaTypeModel) {
|
||||
return replaceIllegalFileNameCharactersInString(item.premiere ? `${item.title} (${item.premiere})` : `${item.title}`);
|
||||
return replaceIllegalFileNameCharactersInString(item.year ? `${item.title} (${item.year})` : `${item.title}`);
|
||||
}
|
||||
|
||||
export function replaceIllegalFileNameCharactersInString(string: string) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue