diff --git a/src/api/apis/MALAPI.ts b/src/api/apis/MALAPI.ts index a58f414..ab96625 100644 --- a/src/api/apis/MALAPI.ts +++ b/src/api/apis/MALAPI.ts @@ -13,7 +13,7 @@ export class MALAPI extends APIModel { this.plugin = plugin; this.apiName = 'MALAPI'; - this.apiDescription = 'A free API for Anime.'; + this.apiDescription = 'A free API for Anime. Some results may take a long time to load.'; this.apiUrl = 'https://jikan.moe/'; this.types = ['movie', 'series', 'anime']; } @@ -62,18 +62,31 @@ export class MALAPI extends APIModel { console.log(data); const result = data.data; - // if (data.Type === 'movie') { + if (result.type === 'Movie') { const model = new MovieModel({ type: result.type, title: result.title, - year: result.year, + year: result.year ?? result.aired?.prop?.from?.year ?? '', dataSource: this.apiName, id: result.mal_id, + + genres: result.genres?.map((x: any) => x.name) ?? [], + producer: result.studios?.map((x: any) => x.name).join(', ') ?? 'unknown', + duration: result.duration ?? 'unknown', + onlineRating: result.score ?? 0, + image: result.images?.jpg?.image_url ?? '', + + released: true, + premiere: result.aired?.string ?? 'unknown', + + watched: false, + lastWatched: '', + personalRating: 0, } as MovieModel); return model; - // } + } - // return; + return; } } diff --git a/src/api/apis/OMDbAPI.ts b/src/api/apis/OMDbAPI.ts index f8b8045..ad36fcc 100644 --- a/src/api/apis/OMDbAPI.ts +++ b/src/api/apis/OMDbAPI.ts @@ -22,6 +22,10 @@ export class OMDbAPI extends APIModel { const searchUrl = `http://www.omdbapi.com/?s=${title}&apikey=${this.plugin.settings.OMDbKey}`; const fetchData = await fetch(searchUrl); + + if (fetchData.status === 401) { + throw Error(`Authentication for ${this.apiName} failed. Check the API key.`); + } if (fetchData.status !== 200) { throw Error(`Received status code ${fetchData.status} from an API.`); } @@ -55,6 +59,9 @@ export class OMDbAPI extends APIModel { const searchUrl = `http://www.omdbapi.com/?i=${item.id}&apikey=${this.plugin.settings.OMDbKey}`; const fetchData = await fetch(searchUrl); + if (fetchData.status === 401) { + throw Error(`Authentication for ${this.apiName} failed. Check the API key.`); + } if (fetchData.status !== 200) { throw Error(`Received status code ${fetchData.status} from an API.`); } diff --git a/src/modals/MediaDbAdvancedSearchModal.ts b/src/modals/MediaDbAdvancedSearchModal.ts index 2955182..8186f37 100644 --- a/src/modals/MediaDbAdvancedSearchModal.ts +++ b/src/modals/MediaDbAdvancedSearchModal.ts @@ -1,4 +1,4 @@ -import {App, ButtonComponent, Component, Modal, Setting, TextComponent, ToggleComponent} from 'obsidian'; +import {App, ButtonComponent, Component, Modal, Notice, Setting, TextComponent, ToggleComponent} from 'obsidian'; import {MediaTypeModel} from '../models/MediaTypeModel'; import {APIManager} from '../api/APIManager'; @@ -16,7 +16,7 @@ export class MediaDbAdvancedSearchModal extends Modal { this.onSubmit = onSubmit; this.selectedApis = []; for (const api of this.apiManager.apis) { - this.selectedApis[api.apiName] = true; + this.selectedApis[api.apiName] = false; } } @@ -30,7 +30,20 @@ export class MediaDbAdvancedSearchModal extends Modal { console.log(this.selectedApis); - if (!this.query) { + if (!this.query || this.query.length < 5) { + new Notice("MDB: Query to short"); + return; + } + + let selectedAPICount = 0; + for (const api in this.selectedApis) { + if (this.selectedApis[api]) { + selectedAPICount += 1; + } + } + + if (selectedAPICount === 0) { + new Notice("MDB: No API selected"); return; } diff --git a/src/settings/Settings.ts b/src/settings/Settings.ts index 835da6f..4b7701b 100644 --- a/src/settings/Settings.ts +++ b/src/settings/Settings.ts @@ -6,11 +6,13 @@ import {FolderSuggest} from './suggesters/FolderSuggester'; export interface MediaDbPluginSettings { folder: string, + sfwFilter: boolean, OMDbKey: string, } export const DEFAULT_SETTINGS: MediaDbPluginSettings = { folder: '', + sfwFilter: true, OMDbKey: '', }; @@ -53,6 +55,17 @@ export class MediaDbSettingTab extends PluginSettingTab { this.plugin.saveSettings(); }); }); + + new Setting(containerEl) + .setName('SFW filter') + .setDesc('Only shows SFW results for APIs that offer filtering.') + .addToggle(cb => { + cb.setValue(this.plugin.settings.sfwFilter) + .onChange(data => { + this.plugin.settings.sfwFilter = data; + this.plugin.saveSettings(); + }); + }); } } diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 625842a..4e277c9 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -13,5 +13,5 @@ export function getFileName(item: MediaTypeModel) { } export function replaceIllegalFileNameCharactersInString(string: string) { - return string.replace(/[\\,#%&{}/*<>$":@.]*/g, ''); + return string.replace(/[\\,#%&{}/*<>$"@.]*/g, '').replace(/:+/g, ' -'); }