Skip to content

Commit

Permalink
small fix for type name consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
mProjectsCode committed May 13, 2022
1 parent b3ff9d1 commit 5bdb9bd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
31 changes: 23 additions & 8 deletions src/api/apis/MALAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {SeriesModel} from '../../models/SeriesModel';

export class MALAPI extends APIModel {
plugin: MediaDbPlugin;
typeMappings: Map<string, string>;

constructor(plugin: MediaDbPlugin) {
super();
Expand All @@ -15,6 +16,11 @@ export class MALAPI extends APIModel {
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'];
this.typeMappings = new Map<string, string>();
this.typeMappings.set('movie', 'movie');
this.typeMappings.set('special', 'special');
this.typeMappings.set('tv', 'series');
this.typeMappings.set('ova', 'ova');
}

async searchByTitle(title: string): Promise<MediaTypeModel[]> {
Expand All @@ -34,18 +40,22 @@ export class MALAPI extends APIModel {
let ret: MediaTypeModel[] = [];

for (const result of data.data) {
if (result.type.toLowerCase() === 'movie' || result.type.toLowerCase() === 'special') {
const type = this.typeMappings.get(result.type.toLowerCase());
if (type === undefined) {
continue;
}
if (type === 'movie' || type === 'special') {
ret.push(new MovieModel({
type: result.type,
type: type,
title: result.title,
englishTitle: result.title_english ?? result.title,
year: result.year ?? result.aired?.prop?.from?.year ?? '',
dataSource: this.apiName,
id: result.mal_id,
} as MovieModel));
} else if (result.type.toLowerCase() === 'tv' || result.type.toLowerCase() === 'ova') {
} else if (type === 'series' || type === 'ova') {
ret.push(new SeriesModel({
type: result.type,
type: type,
title: result.title,
englishTitle: result.title_english ?? result.title,
year: result.year ?? result.aired?.prop?.from?.year ?? '',
Expand All @@ -71,9 +81,14 @@ export class MALAPI extends APIModel {
console.log(data);
const result = data.data;

if (result.type.toLowerCase() === 'movie' || result.type.toLowerCase() === 'special') {
const type = this.typeMappings.get(result.type.toLowerCase());
if (type === undefined) {
throw Error(`${result.type.toLowerCase()} is an unsupported type.`);
}

if (type === 'movie' || type === 'special') {
const model = new MovieModel({
type: result.type,
type: type,
title: result.title,
englishTitle: result.title_english ?? result.title,
year: result.year ?? result.aired?.prop?.from?.year ?? '',
Expand All @@ -96,9 +111,9 @@ export class MALAPI extends APIModel {
} as MovieModel);

return model;
} else if (result.type.toLowerCase() === 'tv' || result.type.toLowerCase() === 'ova') {
} else if (type === 'series' || type === 'ova') {
const model = new SeriesModel({
type: result.type,
type: type,
title: result.title,
englishTitle: result.title_english ?? result.title,
year: result.year ?? result.aired?.prop?.from?.year ?? '',
Expand Down
26 changes: 20 additions & 6 deletions src/api/apis/OMDbAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {GameModel} from '../../models/GameModel';

export class OMDbAPI extends APIModel {
plugin: MediaDbPlugin;
typeMappings: Map<string, string>;

constructor(plugin: MediaDbPlugin) {
super();
Expand All @@ -16,6 +17,10 @@ export class OMDbAPI extends APIModel {
this.apiDescription = 'A free API for Movies, Series and Games.';
this.apiUrl = 'http://www.omdbapi.com/';
this.types = ['movie', 'series'];
this.typeMappings = new Map<string, string>();
this.typeMappings.set('movie', 'movie');
this.typeMappings.set('series', 'series');
this.typeMappings.set('game', 'game');
}

async searchByTitle(title: string): Promise<MediaTypeModel[]> {
Expand Down Expand Up @@ -46,6 +51,10 @@ export class OMDbAPI extends APIModel {
let ret: MediaTypeModel[] = [];

for (const result of data.Search) {
const type = this.typeMappings.get(result.type.toLowerCase());
if (type === undefined) {
continue;
}
if (result.Type === 'movie') {
ret.push(new MovieModel({
type: 'movie',
Expand Down Expand Up @@ -99,9 +108,14 @@ export class OMDbAPI extends APIModel {
throw Error(`Received error from ${this.apiName}: ${result.Error}`);
}

if (result.Type === 'movie') {
const type = this.typeMappings.get(result.type.toLowerCase());
if (type === undefined) {
throw Error(`${result.type.toLowerCase()} is an unsupported type.`);
}

if (type === 'movie') {
const model = new MovieModel({
type: 'movie',
type: type,
title: result.Title,
englishTitle: result.Title,
year: result.Year,
Expand All @@ -124,9 +138,9 @@ export class OMDbAPI extends APIModel {
} as MovieModel);

return model;
} else if (result.Type === 'series') {
} else if (type === 'series') {
const model = new SeriesModel({
type: 'series',
type: type,
title: result.Title,
englishTitle: result.Title,
year: result.Year,
Expand All @@ -152,9 +166,9 @@ export class OMDbAPI extends APIModel {
} as SeriesModel);

return model;
} else if (result.Type === 'game') {
} else if (type === 'game') {
const model = new GameModel({
type: 'game',
type: type,
title: result.Title,
englishTitle: result.Title,
year: result.Year,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function getFileName(item: MediaTypeModel) {
}

export function replaceIllegalFileNameCharactersInString(string: string) {
return string.replace(/[\\,#%&{}/*<>$"@.]*/g, '').replace(/:+/g, ' -');
return string.replace(/[\\,#%&{}/*<>$"@.?]*/g, '').replace(/:+/g, ' -');
}

export function replaceTags(template: string, mediaTypeModel: MediaTypeModel): string {
Expand Down

0 comments on commit 5bdb9bd

Please sign in to comment.