update deps; make apis more type safe
This commit is contained in:
parent
9518ede41c
commit
292dcf5b49
46 changed files with 26911 additions and 550 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import { Notice } from 'obsidian';
|
||||
import createClient from 'openapi-fetch';
|
||||
import { obsidianFetch } from 'src/utils/Utils';
|
||||
import type MediaDbPlugin from '../../main';
|
||||
import { GameModel } from '../../models/GameModel';
|
||||
import type { MediaTypeModel } from '../../models/MediaTypeModel';
|
||||
|
|
@ -6,6 +7,56 @@ import { MovieModel } from '../../models/MovieModel';
|
|||
import { SeriesModel } from '../../models/SeriesModel';
|
||||
import { MediaType } from '../../utils/MediaType';
|
||||
import { APIModel } from '../APIModel';
|
||||
import type { paths } from '../schemas/OMDb';
|
||||
|
||||
type SearchResponse =
|
||||
| {
|
||||
Response: 'True';
|
||||
totalResults: string;
|
||||
Search: {
|
||||
Title: string;
|
||||
Year: string;
|
||||
Poster: string;
|
||||
imdbID: string;
|
||||
Type: string;
|
||||
}[];
|
||||
}
|
||||
| {
|
||||
Response: 'False';
|
||||
Error: string;
|
||||
};
|
||||
|
||||
type IdResponse =
|
||||
| {
|
||||
Response: 'True';
|
||||
Title: string;
|
||||
Year: string;
|
||||
Rated: string;
|
||||
Released: string;
|
||||
Runtime: string;
|
||||
Genre: string;
|
||||
Director: string;
|
||||
Writer: string;
|
||||
Actors: string;
|
||||
Plot: string;
|
||||
Language: string;
|
||||
Country: string;
|
||||
Awards: string;
|
||||
Poster: string;
|
||||
Metascore: string;
|
||||
imdbRating: string;
|
||||
imdbVotes: string;
|
||||
imdbID: string;
|
||||
Type: string;
|
||||
DVD: string;
|
||||
BoxOffice: string;
|
||||
Production: string;
|
||||
Website: string;
|
||||
}
|
||||
| {
|
||||
Response: 'False';
|
||||
Error: string;
|
||||
};
|
||||
|
||||
export class OMDbAPI extends APIModel {
|
||||
plugin: MediaDbPlugin;
|
||||
|
|
@ -33,24 +84,37 @@ export class OMDbAPI extends APIModel {
|
|||
throw new Error(`MDB | API key for ${this.apiName} missing.`);
|
||||
}
|
||||
|
||||
const searchUrl = `https://www.omdbapi.com/?s=${encodeURIComponent(title)}&apikey=${this.plugin.settings.OMDbKey}`;
|
||||
const fetchData = await fetch(searchUrl);
|
||||
const client = createClient<paths>({ baseUrl: 'https://www.omdbapi.com/' });
|
||||
|
||||
if (fetchData.status === 401) {
|
||||
const response = await client.GET('/?s', {
|
||||
params: {
|
||||
query: {
|
||||
s: title,
|
||||
apikey: this.plugin.settings.OMDbKey,
|
||||
},
|
||||
},
|
||||
fetch: obsidianFetch,
|
||||
});
|
||||
|
||||
if (response.response.status === 401) {
|
||||
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
|
||||
}
|
||||
if (fetchData.status !== 200) {
|
||||
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
|
||||
if (response.response.status !== 200) {
|
||||
throw Error(`MDB | Received status code ${response.response.status} from ${this.apiName}.`);
|
||||
}
|
||||
|
||||
const data = await fetchData.json();
|
||||
const data = response.data as SearchResponse | undefined;
|
||||
|
||||
if (!data) {
|
||||
throw Error(`MDB | No data received from ${this.apiName}.`);
|
||||
}
|
||||
|
||||
if (data.Response === 'False') {
|
||||
if (data.Error === 'Movie not found!') {
|
||||
return [];
|
||||
}
|
||||
|
||||
throw Error(`MDB | Received error from ${this.apiName}: \n${JSON.stringify(data, undefined, 4)}`);
|
||||
throw Error(`MDB | Received error from ${this.apiName}: ${data.Error}`);
|
||||
}
|
||||
if (!data.Search) {
|
||||
return [];
|
||||
|
|
@ -111,18 +175,30 @@ export class OMDbAPI extends APIModel {
|
|||
throw Error(`MDB | API key for ${this.apiName} missing.`);
|
||||
}
|
||||
|
||||
const searchUrl = `https://www.omdbapi.com/?i=${encodeURIComponent(id)}&apikey=${this.plugin.settings.OMDbKey}`;
|
||||
const fetchData = await fetch(searchUrl);
|
||||
const client = createClient<paths>({ baseUrl: 'https://www.omdbapi.com/' });
|
||||
|
||||
if (fetchData.status === 401) {
|
||||
const response = await client.GET('/?i', {
|
||||
params: {
|
||||
query: {
|
||||
i: id,
|
||||
apikey: this.plugin.settings.OMDbKey,
|
||||
},
|
||||
},
|
||||
fetch: obsidianFetch,
|
||||
});
|
||||
|
||||
if (response.response.status === 401) {
|
||||
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
|
||||
}
|
||||
if (fetchData.status !== 200) {
|
||||
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
|
||||
if (response.response.status !== 200) {
|
||||
throw Error(`MDB | Received status code ${response.response.status} from ${this.apiName}.`);
|
||||
}
|
||||
|
||||
const result = await fetchData.json();
|
||||
// console.debug(result);
|
||||
const result = response.data as IdResponse | undefined;
|
||||
|
||||
if (!result) {
|
||||
throw Error(`MDB | No data received from ${this.apiName}.`);
|
||||
}
|
||||
|
||||
if (result.Response === 'False') {
|
||||
throw Error(`MDB | Received error from ${this.apiName}: ${result.Error}`);
|
||||
|
|
@ -130,7 +206,7 @@ export class OMDbAPI extends APIModel {
|
|||
|
||||
const type = this.typeMappings.get(result.Type.toLowerCase());
|
||||
if (type === undefined) {
|
||||
throw Error(`${result.type.toLowerCase()} is an unsupported type.`);
|
||||
throw Error(`${result.Type.toLowerCase()} is an unsupported type.`);
|
||||
}
|
||||
|
||||
if (type === 'movie') {
|
||||
|
|
@ -143,19 +219,17 @@ export class OMDbAPI extends APIModel {
|
|||
url: `https://www.imdb.com/title/${result.imdbID}/`,
|
||||
id: result.imdbID,
|
||||
|
||||
plot: result.Plot ?? '',
|
||||
genres: result.Genre?.split(', ') ?? [],
|
||||
director: result.Director?.split(', ') ?? [],
|
||||
writer: result.Writer?.split(', ') ?? [],
|
||||
studio: ['N/A'],
|
||||
duration: result.Runtime ?? 'unknown',
|
||||
plot: result.Plot,
|
||||
genres: result.Genre?.split(', '),
|
||||
director: result.Director?.split(', '),
|
||||
writer: result.Writer?.split(', '),
|
||||
duration: result.Runtime,
|
||||
onlineRating: Number.parseFloat(result.imdbRating ?? 0),
|
||||
actors: result.Actors?.split(', ') ?? [],
|
||||
image: result.Poster ? result.Poster.replace('_SX300', '_SX600') : '',
|
||||
actors: result.Actors?.split(', '),
|
||||
image: result.Poster.replace('_SX300', '_SX600'),
|
||||
|
||||
released: true,
|
||||
streamingServices: [],
|
||||
premiere: this.plugin.dateFormatter.format(result.Released, this.apiDateFormat) ?? 'unknown',
|
||||
premiere: this.plugin.dateFormatter.format(result.Released, this.apiDateFormat),
|
||||
|
||||
userData: {
|
||||
watched: false,
|
||||
|
|
@ -173,21 +247,18 @@ export class OMDbAPI extends APIModel {
|
|||
url: `https://www.imdb.com/title/${result.imdbID}/`,
|
||||
id: result.imdbID,
|
||||
|
||||
plot: result.Plot ?? '',
|
||||
genres: result.Genre?.split(', ') ?? [],
|
||||
writer: result.Writer?.split(', ') ?? [],
|
||||
plot: result.Plot,
|
||||
genres: result.Genre?.split(', '),
|
||||
writer: result.Writer?.split(', '),
|
||||
studio: [],
|
||||
episodes: 0,
|
||||
duration: result.Runtime ?? 'unknown',
|
||||
duration: result.Runtime,
|
||||
onlineRating: Number.parseFloat(result.imdbRating ?? 0),
|
||||
actors: result.Actors?.split(', ') ?? [],
|
||||
image: result.Poster ? result.Poster.replace('_SX300', '_SX600') : '',
|
||||
actors: result.Actors?.split(', '),
|
||||
image: result.Poster.replace('_SX300', '_SX600'),
|
||||
|
||||
released: true,
|
||||
streamingServices: [],
|
||||
airing: false,
|
||||
airedFrom: this.plugin.dateFormatter.format(result.Released, this.apiDateFormat) ?? 'unknown',
|
||||
airedTo: 'unknown',
|
||||
airedFrom: this.plugin.dateFormatter.format(result.Released, this.apiDateFormat),
|
||||
|
||||
userData: {
|
||||
watched: false,
|
||||
|
|
@ -205,14 +276,12 @@ export class OMDbAPI extends APIModel {
|
|||
url: `https://www.imdb.com/title/${result.imdbID}/`,
|
||||
id: result.imdbID,
|
||||
|
||||
developers: [],
|
||||
publishers: [],
|
||||
genres: result.Genre?.split(', ') ?? [],
|
||||
genres: result.Genre?.split(', '),
|
||||
onlineRating: Number.parseFloat(result.imdbRating ?? 0),
|
||||
image: result.Poster ? result.Poster.replace('_SX300', '_SX600') : '',
|
||||
image: result.Poster.replace('_SX300', '_SX600'),
|
||||
|
||||
released: true,
|
||||
releaseDate: this.plugin.dateFormatter.format(result.Released, this.apiDateFormat) ?? 'unknown',
|
||||
releaseDate: this.plugin.dateFormatter.format(result.Released, this.apiDateFormat),
|
||||
|
||||
userData: {
|
||||
played: false,
|
||||
|
|
@ -225,6 +294,6 @@ export class OMDbAPI extends APIModel {
|
|||
}
|
||||
|
||||
getDisabledMediaTypes(): MediaType[] {
|
||||
return this.plugin.settings.OMDbAPI_disabledMediaTypes as MediaType[];
|
||||
return this.plugin.settings.OMDbAPI_disabledMediaTypes;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue