series for MAL
This commit is contained in:
parent
c7a75ff93c
commit
ac82168740
8 changed files with 101 additions and 80 deletions
|
|
@ -4,6 +4,7 @@ import {MovieModel} from '../../models/MovieModel';
|
||||||
import MediaDbPlugin from '../../main';
|
import MediaDbPlugin from '../../main';
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import Jikanjs from '@mateoaranda/jikanjs';
|
import Jikanjs from '@mateoaranda/jikanjs';
|
||||||
|
import {SeriesModel} from '../../models/SeriesModel';
|
||||||
|
|
||||||
export class MALAPI extends APIModel {
|
export class MALAPI extends APIModel {
|
||||||
plugin: MediaDbPlugin;
|
plugin: MediaDbPlugin;
|
||||||
|
|
@ -34,16 +35,24 @@ export class MALAPI extends APIModel {
|
||||||
|
|
||||||
let ret: MediaTypeModel[] = [];
|
let ret: MediaTypeModel[] = [];
|
||||||
|
|
||||||
for (const value of data.data) {
|
for (const result of data.data) {
|
||||||
// if (value.Type === 'movie') {
|
if (result.type.toLowerCase() === 'movie') {
|
||||||
ret.push(new MovieModel({
|
ret.push(new MovieModel({
|
||||||
type: value.type,
|
type: result.type,
|
||||||
title: value.title,
|
title: result.title,
|
||||||
year: value.year,
|
year: result.year ?? result.aired?.prop?.from?.year ?? '',
|
||||||
dataSource: this.apiName,
|
dataSource: this.apiName,
|
||||||
id: value.mal_id,
|
id: result.mal_id,
|
||||||
} as MovieModel));
|
} as MovieModel));
|
||||||
// }
|
} else if (result.type.toLowerCase() === 'tv') {
|
||||||
|
ret.push(new SeriesModel({
|
||||||
|
type: result.type,
|
||||||
|
title: result.title,
|
||||||
|
year: result.year ?? result.aired?.prop?.from?.year ?? '',
|
||||||
|
dataSource: this.apiName,
|
||||||
|
id: result.mal_id,
|
||||||
|
} as SeriesModel));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
@ -62,7 +71,7 @@ export class MALAPI extends APIModel {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
const result = data.data;
|
const result = data.data;
|
||||||
|
|
||||||
if (result.type === 'Movie') {
|
if (result.type.toLowerCase() === 'movie') {
|
||||||
const model = new MovieModel({
|
const model = new MovieModel({
|
||||||
type: result.type,
|
type: result.type,
|
||||||
title: result.title,
|
title: result.title,
|
||||||
|
|
@ -84,6 +93,32 @@ export class MALAPI extends APIModel {
|
||||||
personalRating: 0,
|
personalRating: 0,
|
||||||
} as MovieModel);
|
} as MovieModel);
|
||||||
|
|
||||||
|
return model;
|
||||||
|
} else if (result.type.toLowerCase() === 'tv') {
|
||||||
|
const model = new SeriesModel({
|
||||||
|
type: result.type,
|
||||||
|
title: result.title,
|
||||||
|
year: result.year ?? result.aired?.prop?.from?.year ?? '',
|
||||||
|
dataSource: this.apiName,
|
||||||
|
id: result.mal_id,
|
||||||
|
|
||||||
|
genres: result.genres?.map((x: any) => x.name) ?? [],
|
||||||
|
studios: result.studios?.map((x: any) => x.name) ?? [],
|
||||||
|
episodes: result.episodes,
|
||||||
|
duration: result.duration ?? 'unknown',
|
||||||
|
onlineRating: result.score ?? 0,
|
||||||
|
image: result.images?.jpg?.image_url ?? '',
|
||||||
|
|
||||||
|
released: true,
|
||||||
|
airedFrom: (new Date(result.aired?.from)).toLocaleDateString() ?? 'unknown',
|
||||||
|
airedTo: (new Date(result.aired?.to)).toLocaleDateString() ?? 'unknown',
|
||||||
|
airing: result.airing,
|
||||||
|
|
||||||
|
watched: false,
|
||||||
|
lastWatched: '',
|
||||||
|
personalRating: 0,
|
||||||
|
} as SeriesModel);
|
||||||
|
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import {DEFAULT_SETTINGS, MediaDbPluginSettings, MediaDbSettingTab} from './sett
|
||||||
import {APIManager} from './api/APIManager';
|
import {APIManager} from './api/APIManager';
|
||||||
import {TestAPI} from './api/apis/TestAPI';
|
import {TestAPI} from './api/apis/TestAPI';
|
||||||
import {MediaTypeModel} from './models/MediaTypeModel';
|
import {MediaTypeModel} from './models/MediaTypeModel';
|
||||||
import {getFileName} from './utils/Utils';
|
import {replaceIllegalFileNameCharactersInString} from './utils/Utils';
|
||||||
import {OMDbAPI} from './api/apis/OMDbAPI';
|
import {OMDbAPI} from './api/apis/OMDbAPI';
|
||||||
import {MediaDbAdvancedSearchModal} from './modals/MediaDbAdvancedSearchModal';
|
import {MediaDbAdvancedSearchModal} from './modals/MediaDbAdvancedSearchModal';
|
||||||
import {MediaDbSearchResultModal} from './modals/MediaDbSearchResultModal';
|
import {MediaDbSearchResultModal} from './modals/MediaDbSearchResultModal';
|
||||||
|
|
@ -51,9 +51,9 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
|
|
||||||
data.toMetaData();
|
data.toMetaData();
|
||||||
|
|
||||||
const fileContent = `---\n${data.toMetaData()}\n---\n`;
|
const fileContent = `---\n${data.toMetaData()}---\n`;
|
||||||
|
|
||||||
const fileName = getFileName(data);
|
const fileName = replaceIllegalFileNameCharactersInString(data.getFileName());
|
||||||
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${fileName}.md`;
|
const filePath = `${this.settings.folder.replace(/\/$/, '')}/${fileName}.md`;
|
||||||
const targetFile = await this.app.vault.create(filePath, fileContent);
|
const targetFile = await this.app.vault.create(filePath, fileContent);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ export class MediaDbAdvancedSearchModal extends Modal {
|
||||||
console.log(this.selectedApis);
|
console.log(this.selectedApis);
|
||||||
|
|
||||||
if (!this.query || this.query.length < 5) {
|
if (!this.query || this.query.length < 5) {
|
||||||
new Notice("MDB: Query to short");
|
new Notice('MDB: Query to short');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,7 +43,7 @@ export class MediaDbAdvancedSearchModal extends Modal {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (selectedAPICount === 0) {
|
if (selectedAPICount === 0) {
|
||||||
new Notice("MDB: No API selected");
|
new Notice('MDB: No API selected');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,65 +0,0 @@
|
||||||
import {App, SuggestModal} from 'obsidian';
|
|
||||||
import {APIManager} from '../api/APIManager';
|
|
||||||
import {MediaTypeModel} from '../models/MediaTypeModel';
|
|
||||||
|
|
||||||
export class MediaDbSearchModal extends SuggestModal<MediaTypeModel> {
|
|
||||||
query: string;
|
|
||||||
isBusy: boolean;
|
|
||||||
apiManager: APIManager;
|
|
||||||
onChoose: (err: Error, result?: MediaTypeModel) => void;
|
|
||||||
|
|
||||||
constructor(app: App, apiManager: APIManager, onChoose?: (err: Error, result?: MediaTypeModel) => void) {
|
|
||||||
super(app);
|
|
||||||
|
|
||||||
this.apiManager = apiManager;
|
|
||||||
this.onChoose = onChoose;
|
|
||||||
this.isBusy = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
async search(force: boolean = false): Promise<MediaTypeModel[]> {
|
|
||||||
if (!this.query) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.isBusy || force) {
|
|
||||||
this.isBusy = true;
|
|
||||||
const thisQuery: string = this.query;
|
|
||||||
|
|
||||||
console.log('MDB | query started with ' + thisQuery);
|
|
||||||
|
|
||||||
const res = await this.apiManager.query(thisQuery);
|
|
||||||
|
|
||||||
// console.log(res)
|
|
||||||
|
|
||||||
await sleep(1000);
|
|
||||||
|
|
||||||
if (this.query === thisQuery) {
|
|
||||||
this.isBusy = false;
|
|
||||||
|
|
||||||
return res;
|
|
||||||
|
|
||||||
// return [
|
|
||||||
// {title: thisQuery, type: 'movie', data: null} as APIRequestResult,
|
|
||||||
// {title: 'test2', type: 'series', data: {episodes: 24}} as APIRequestResult,
|
|
||||||
// ];
|
|
||||||
} else {
|
|
||||||
return await this.search(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async getSuggestions(query: string): Promise<MediaTypeModel[]> {
|
|
||||||
this.query = query;
|
|
||||||
|
|
||||||
return await this.search();
|
|
||||||
}
|
|
||||||
|
|
||||||
renderSuggestion(item: MediaTypeModel, el: HTMLElement): void {
|
|
||||||
el.createEl('div', {text: item.title});
|
|
||||||
el.createEl('small', {text: item.type});
|
|
||||||
}
|
|
||||||
|
|
||||||
onChooseSuggestion(item: MediaTypeModel, evt: MouseEvent | KeyboardEvent): void {
|
|
||||||
this.onChoose(null, item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -20,8 +20,8 @@ export class MediaDbSearchResultModal extends SuggestModal<MediaTypeModel> {
|
||||||
|
|
||||||
// Renders each suggestion item.
|
// Renders each suggestion item.
|
||||||
renderSuggestion(item: MediaTypeModel, el: HTMLElement) {
|
renderSuggestion(item: MediaTypeModel, el: HTMLElement) {
|
||||||
el.createEl('div', {text: item.premiere ? `${item.title} (${item.premiere})` : `${item.title}`});
|
el.createEl('div', {text: item.getFileName()});
|
||||||
el.createEl('small', {text: `${item.type} from ${item.dataSource}`});
|
el.createEl('small', {text: `${item.type.toUpperCase()} from ${item.dataSource}`});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform action on the selected suggestion.
|
// Perform action on the selected suggestion.
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,6 @@ export abstract class MediaTypeModel {
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
abstract toMetaData(): string;
|
abstract toMetaData(): string;
|
||||||
|
|
||||||
|
abstract getFileName(): string;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,4 +35,8 @@ export class MovieModel extends MediaTypeModel {
|
||||||
return stringifyYaml(this);
|
return stringifyYaml(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getFileName(): string {
|
||||||
|
return this.title + (this.year ? ` (${this.year})` : '');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
45
src/models/SeriesModel.ts
Normal file
45
src/models/SeriesModel.ts
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
import {MediaTypeModel} from './MediaTypeModel';
|
||||||
|
import {stringifyYaml} from 'obsidian';
|
||||||
|
|
||||||
|
|
||||||
|
export class SeriesModel extends MediaTypeModel {
|
||||||
|
type: string;
|
||||||
|
title: string;
|
||||||
|
year: string;
|
||||||
|
dataSource: string;
|
||||||
|
id: string;
|
||||||
|
|
||||||
|
genres: string[];
|
||||||
|
studios: string[];
|
||||||
|
episodes: number;
|
||||||
|
duration: string;
|
||||||
|
onlineRating: number;
|
||||||
|
image: string;
|
||||||
|
|
||||||
|
released: boolean;
|
||||||
|
airing: boolean;
|
||||||
|
airedFrom: string;
|
||||||
|
airedTo: string;
|
||||||
|
|
||||||
|
watched: boolean;
|
||||||
|
lastWatched: string;
|
||||||
|
personalRating: number;
|
||||||
|
|
||||||
|
|
||||||
|
constructor(obj: any = {}) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
Object.assign(this, obj);
|
||||||
|
|
||||||
|
this.type = 'series';
|
||||||
|
}
|
||||||
|
|
||||||
|
toMetaData(): string {
|
||||||
|
return stringifyYaml(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
getFileName(): string {
|
||||||
|
return this.title;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue