Config fixes and janky OMDb implementation
This commit is contained in:
parent
4405eb4a34
commit
cc2962220e
4 changed files with 86 additions and 1 deletions
66
src/api/apis/OMDbAPI.ts
Normal file
66
src/api/apis/OMDbAPI.ts
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
import {APIModel} from '../APIModel';
|
||||
import {MediaTypeModel} from '../../models/MediaTypeModel';
|
||||
import {MovieModel} from '../../models/MovieModel';
|
||||
import MediaDbPlugin from '../../main';
|
||||
|
||||
export class OMDbAPI extends APIModel {
|
||||
plugin: MediaDbPlugin;
|
||||
|
||||
constructor(plugin: MediaDbPlugin) {
|
||||
super();
|
||||
|
||||
this.plugin = plugin;
|
||||
this.apiName = 'OMDbAPI';
|
||||
this.apiUrl = 'http://www.omdbapi.com/';
|
||||
this.types = ['movie', 'series'];
|
||||
}
|
||||
|
||||
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
|
||||
console.log(`MDB | api "${this.apiName}" queried`);
|
||||
|
||||
const searchUrl = `http://www.omdbapi.com/?s=${title}&apikey=${this.plugin.settings.OMDbKey}`;
|
||||
|
||||
const fetchData = await fetch(searchUrl);
|
||||
if (fetchData.status !== 200) {
|
||||
throw Error(`Received status code ${fetchData.status} from an API.`);
|
||||
}
|
||||
const data = await fetchData.json();
|
||||
|
||||
if (!data.Search) {
|
||||
return [];
|
||||
}
|
||||
|
||||
console.log(data.Search);
|
||||
|
||||
let ret: MediaTypeModel[] = [];
|
||||
|
||||
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'} as MovieModel))
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
async getById(item: MediaTypeModel): Promise<MediaTypeModel> {
|
||||
|
||||
const searchUrl = `http://www.omdbapi.com/?i=${item.id}&apikey=${this.plugin.settings.OMDbKey}`;
|
||||
|
||||
const fetchData = await fetch(searchUrl);
|
||||
if (fetchData.status !== 200) {
|
||||
throw Error(`Received status code ${fetchData.status} from an API.`);
|
||||
}
|
||||
|
||||
const data = await fetchData.json();
|
||||
if (data.Type === 'movie') {
|
||||
const model = new MovieModel({title: data.Title, id: data.imdbID, dataSource: this.apiName, type: 'movie'} as MovieModel);
|
||||
|
||||
// TODO: mapping
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
return ;
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ import {APIManager} from './api/APIManager';
|
|||
import {TestAPI} from './api/apis/TestAPI';
|
||||
import {MediaTypeModel} from './models/MediaTypeModel';
|
||||
import {getFileName} from './utils/Utils';
|
||||
import {OMDbAPI} from './api/apis/OMDbAPI';
|
||||
|
||||
export default class MediaDbPlugin extends Plugin {
|
||||
settings: MediaDbPluginSettings;
|
||||
|
|
@ -33,6 +34,7 @@ export default class MediaDbPlugin extends Plugin {
|
|||
this.apiManager = new APIManager();
|
||||
// register APIs
|
||||
this.apiManager.registerAPI(new TestAPI());
|
||||
this.apiManager.registerAPI(new OMDbAPI(this));
|
||||
}
|
||||
|
||||
async createMediaDbNote(): Promise<void> {
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ export class MediaDbSearchModal extends SuggestModal<MediaTypeModel> {
|
|||
|
||||
// console.log(res)
|
||||
|
||||
// await sleep(1000);
|
||||
await sleep(1000);
|
||||
|
||||
if (this.query === thisQuery) {
|
||||
this.isBusy = false;
|
||||
|
|
|
|||
|
|
@ -6,10 +6,12 @@ import {FolderSuggest} from './suggesters/FolderSuggester';
|
|||
|
||||
export interface MediaDbPluginSettings {
|
||||
folder: string,
|
||||
OMDbKey: string,
|
||||
}
|
||||
|
||||
export const DEFAULT_SETTINGS: MediaDbPluginSettings = {
|
||||
folder: '',
|
||||
OMDbKey: '',
|
||||
};
|
||||
|
||||
export class MediaDbSettingTab extends PluginSettingTab {
|
||||
|
|
@ -17,11 +19,14 @@ export class MediaDbSettingTab extends PluginSettingTab {
|
|||
|
||||
constructor(app: App, plugin: MediaDbPlugin) {
|
||||
super(app, plugin);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
display(): void {
|
||||
const {containerEl} = this;
|
||||
|
||||
containerEl.empty();
|
||||
|
||||
containerEl.createEl('h2', {text: 'Media DB Plugin Settings'});
|
||||
|
||||
new Setting(containerEl)
|
||||
|
|
@ -36,6 +41,18 @@ export class MediaDbSettingTab extends PluginSettingTab {
|
|||
this.plugin.saveSettings();
|
||||
});
|
||||
});
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('OMDb API key')
|
||||
.setDesc('API key for www.omdbapi.com.')
|
||||
.addText(cb => {
|
||||
cb.setPlaceholder('API key')
|
||||
.setValue(this.plugin.settings.OMDbKey)
|
||||
.onChange(data => {
|
||||
this.plugin.settings.OMDbKey = data;
|
||||
this.plugin.saveSettings();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue