Skip to content

Commit 526f9d8

Browse files
committed
Split TMDB Movie & TMDB Series
1 parent e823341 commit 526f9d8

File tree

4 files changed

+135
-130
lines changed

4 files changed

+135
-130
lines changed

src/api/apis/TMDBMovieAPI.ts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { Notice, renderResults } from 'obsidian';
2+
import type MediaDbPlugin from '../../main';
3+
import type { MediaTypeModel } from '../../models/MediaTypeModel';
4+
import { MovieModel } from '../../models/MovieModel';
5+
import { MediaType } from '../../utils/MediaType';
6+
import { APIModel } from '../APIModel';
7+
8+
export class TMDBMovieAPI extends APIModel {
9+
plugin: MediaDbPlugin;
10+
typeMappings: Map<string, string>;
11+
apiDateFormat: string = 'YYYY-MM-DD';
12+
13+
constructor(plugin: MediaDbPlugin) {
14+
super();
15+
16+
this.plugin = plugin;
17+
this.apiName = 'TMDBMovieAPI';
18+
this.apiDescription = 'A community built Movie DB.';
19+
this.apiUrl = 'https://www.themoviedb.org/';
20+
this.types = [MediaType.Movie];
21+
this.typeMappings = new Map<string, string>();
22+
this.typeMappings.set('movie', 'movie');
23+
}
24+
25+
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
26+
console.log(`MDB | api "${this.apiName}" queried by Title`);
27+
28+
if (!this.plugin.settings.TMDBKey) {
29+
throw new Error(`MDB | API key for ${this.apiName} missing.`);
30+
}
31+
32+
const searchUrl = `https://api.themoviedb.org/3/search/movie?api_key=${this.plugin.settings.TMDBKey}&query=${encodeURIComponent(title)}&include_adult=${this.plugin.settings.sfwFilter ? 'false' : 'true'}`;
33+
const fetchData = await fetch(searchUrl);
34+
35+
if (fetchData.status === 401) {
36+
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
37+
}
38+
if (fetchData.status !== 200) {
39+
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
40+
}
41+
42+
const data = await fetchData.json();
43+
44+
if (data.total_results === 0) {
45+
if (data.Error === 'Movie not found!') {
46+
return [];
47+
}
48+
49+
throw Error(`MDB | Received error from ${this.apiName}: \n${JSON.stringify(data, undefined, 4)}`);
50+
}
51+
if (!data.results) {
52+
return [];
53+
}
54+
55+
// console.debug(data.results);
56+
57+
const ret: MediaTypeModel[] = [];
58+
59+
for (const result of data.results) {
60+
ret.push(
61+
new MovieModel({
62+
type: 'movie',
63+
title: result.original_title,
64+
englishTitle: result.title,
65+
year: result.release_date ? new Date(result.release_date).getFullYear().toString() : 'unknown',
66+
dataSource: this.apiName,
67+
id: result.id,
68+
}),
69+
);
70+
}
71+
72+
return ret;
73+
}
74+
75+
async getById(id: string): Promise<MediaTypeModel> {
76+
console.log(`MDB | api "${this.apiName}" queried by ID`);
77+
78+
if (!this.plugin.settings.TMDBKey) {
79+
throw Error(`MDB | API key for ${this.apiName} missing.`);
80+
}
81+
82+
const searchUrl = `https://api.themoviedb.org/3/movie/${encodeURIComponent(id)}?api_key=${this.plugin.settings.TMDBKey}&append_to_response=credits`;
83+
const fetchData = await fetch(searchUrl);
84+
85+
if (fetchData.status === 401) {
86+
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
87+
}
88+
if (fetchData.status !== 200) {
89+
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
90+
}
91+
92+
const result = await fetchData.json();
93+
// console.debug(result);
94+
95+
return new MovieModel({
96+
type: 'movie',
97+
title: result.title,
98+
englishTitle: result.title,
99+
year: result.release_date ? new Date(result.release_date).getFullYear().toString() : 'unknown',
100+
premiere: this.plugin.dateFormatter.format(result.release_date, this.apiDateFormat) ?? 'unknown',
101+
dataSource: this.apiName,
102+
url: `https://www.themoviedb.org/movie/${result.id}`,
103+
id: result.id,
104+
105+
plot: result.overview ?? '',
106+
genres: result.genres.map((g: any) => g.name) ?? [],
107+
writer: result.credits.crew.filter((c: any) => c.job === 'Screenplay').map((c: any) => c.name) ?? [],
108+
director: result.credits.crew.filter((c: any) => c.job === 'Director').map((c: any) => c.name) ?? [],
109+
studio: result.production_companies.map((s: any) => s.name) ?? [],
110+
111+
duration: result.runtime ?? 'unknown',
112+
onlineRating: result.vote_average,
113+
actors: result.credits.cast.map((c: any) => c.name).slice(0, 5) ?? [],
114+
image: `https://image.tmdb.org/t/p/w780${result.poster_path}`,
115+
116+
released:['Released'].includes(result.status),
117+
streamingServices: [],
118+
119+
userData: {
120+
watched: false,
121+
lastWatched: '',
122+
personalRating: 0,
123+
},
124+
});
125+
126+
}
127+
128+
getDisabledMediaTypes(): MediaType[] {
129+
return this.plugin.settings.TMDBMovieAPI_disabledMediaTypes as MediaType[];
130+
}
131+
}
Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Notice, renderResults } from 'obsidian';
22
import type MediaDbPlugin from '../../main';
33
import type { MediaTypeModel } from '../../models/MediaTypeModel';
4-
import { MovieModel } from '../../models/MovieModel';
54
import { SeriesModel } from '../../models/SeriesModel';
65
import { MediaType } from '../../utils/MediaType';
76
import { APIModel } from '../APIModel';
@@ -131,128 +130,3 @@ export class TMDBSeriesAPI extends APIModel {
131130
return this.plugin.settings.TMDBSeriesAPI_disabledMediaTypes as MediaType[];
132131
}
133132
}
134-
135-
export class TMDBMovieAPI extends APIModel {
136-
plugin: MediaDbPlugin;
137-
typeMappings: Map<string, string>;
138-
apiDateFormat: string = 'YYYY-MM-DD';
139-
140-
constructor(plugin: MediaDbPlugin) {
141-
super();
142-
143-
this.plugin = plugin;
144-
this.apiName = 'TMDBMovieAPI';
145-
this.apiDescription = 'A community built Movie DB.';
146-
this.apiUrl = 'https://www.themoviedb.org/';
147-
this.types = [MediaType.Movie];
148-
this.typeMappings = new Map<string, string>();
149-
this.typeMappings.set('movie', 'movie');
150-
}
151-
152-
async searchByTitle(title: string): Promise<MediaTypeModel[]> {
153-
console.log(`MDB | api "${this.apiName}" queried by Title`);
154-
155-
if (!this.plugin.settings.TMDBKey) {
156-
throw new Error(`MDB | API key for ${this.apiName} missing.`);
157-
}
158-
159-
const searchUrl = `https://api.themoviedb.org/3/search/movie?api_key=${this.plugin.settings.TMDBKey}&query=${encodeURIComponent(title)}&include_adult=${this.plugin.settings.sfwFilter ? 'false' : 'true'}`;
160-
const fetchData = await fetch(searchUrl);
161-
162-
if (fetchData.status === 401) {
163-
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
164-
}
165-
if (fetchData.status !== 200) {
166-
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
167-
}
168-
169-
const data = await fetchData.json();
170-
171-
if (data.total_results === 0) {
172-
if (data.Error === 'Movie not found!') {
173-
return [];
174-
}
175-
176-
throw Error(`MDB | Received error from ${this.apiName}: \n${JSON.stringify(data, undefined, 4)}`);
177-
}
178-
if (!data.results) {
179-
return [];
180-
}
181-
182-
// console.debug(data.results);
183-
184-
const ret: MediaTypeModel[] = [];
185-
186-
for (const result of data.results) {
187-
ret.push(
188-
new MovieModel({
189-
type: 'movie',
190-
title: result.original_title,
191-
englishTitle: result.title,
192-
year: result.release_date ? new Date(result.release_date).getFullYear().toString() : 'unknown',
193-
dataSource: this.apiName,
194-
id: result.id,
195-
}),
196-
);
197-
}
198-
199-
return ret;
200-
}
201-
202-
async getById(id: string): Promise<MediaTypeModel> {
203-
console.log(`MDB | api "${this.apiName}" queried by ID`);
204-
205-
if (!this.plugin.settings.TMDBKey) {
206-
throw Error(`MDB | API key for ${this.apiName} missing.`);
207-
}
208-
209-
const searchUrl = `https://api.themoviedb.org/3/movie/${encodeURIComponent(id)}?api_key=${this.plugin.settings.TMDBKey}&append_to_response=credits`;
210-
const fetchData = await fetch(searchUrl);
211-
212-
if (fetchData.status === 401) {
213-
throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`);
214-
}
215-
if (fetchData.status !== 200) {
216-
throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`);
217-
}
218-
219-
const result = await fetchData.json();
220-
// console.debug(result);
221-
222-
return new MovieModel({
223-
type: 'movie',
224-
title: result.title,
225-
englishTitle: result.title,
226-
year: result.release_date ? new Date(result.release_date).getFullYear().toString() : 'unknown',
227-
premiere: this.plugin.dateFormatter.format(result.release_date, this.apiDateFormat) ?? 'unknown',
228-
dataSource: this.apiName,
229-
url: `https://www.themoviedb.org/movie/${result.id}`,
230-
id: result.id,
231-
232-
plot: result.overview ?? '',
233-
genres: result.genres.map((g: any) => g.name) ?? [],
234-
writer: result.credits.crew.filter((c: any) => c.job === 'Screenplay').map((c: any) => c.name) ?? [],
235-
director: result.credits.crew.filter((c: any) => c.job === 'Director').map((c: any) => c.name) ?? [],
236-
studio: result.production_companies.map((s: any) => s.name) ?? [],
237-
238-
duration: result.runtime ?? 'unknown',
239-
onlineRating: result.vote_average,
240-
actors: result.credits.cast.map((c: any) => c.name).slice(0, 5) ?? [],
241-
image: `https://image.tmdb.org/t/p/w780${result.poster_path}`,
242-
243-
released:['Released'].includes(result.status),
244-
streamingServices: [],
245-
246-
userData: {
247-
watched: false,
248-
lastWatched: '',
249-
personalRating: 0,
250-
},
251-
});
252-
253-
}
254-
255-
getDisabledMediaTypes(): MediaType[] {
256-
return this.plugin.settings.TMDBMovieAPI_disabledMediaTypes as MediaType[];
257-
}
258-
}

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { MusicBrainzAPI } from './api/apis/MusicBrainzAPI';
1111
import { OMDbAPI } from './api/apis/OMDbAPI';
1212
import { OpenLibraryAPI } from './api/apis/OpenLibraryAPI';
1313
import { SteamAPI } from './api/apis/SteamAPI';
14-
import { TMDBSeriesAPI } from './api/apis/TMDBAPI';
15-
import { TMDBMovieAPI } from './api/apis/TMDBAPI';
14+
import { TMDBSeriesAPI } from './api/apis/TMDBSeriesAPI';
15+
import { TMDBMovieAPI } from './api/apis/TMDBMovieAPI';
1616
import { WikipediaAPI } from './api/apis/WikipediaAPI';
1717
import { ComicVineAPI } from './api/apis/ComicVineAPI';
1818
import { MediaDbFolderImportModal } from './modals/MediaDbFolderImportModal';

src/settings/Settings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ const DEFAULT_SETTINGS: MediaDbPluginSettings = {
9090
useDefaultFrontMatter: true,
9191
enableTemplaterIntegration: false,
9292
OMDbAPI_disabledMediaTypes: [],
93-
TMDBSeriesAPI_disabledMediaTypes: [],
94-
TMDBMovieAPI_disabledMediaTypes: [],
93+
TMDBSeriesAPI_disabledMediaTypes: [],
94+
TMDBMovieAPI_disabledMediaTypes: [],
9595
MALAPI_disabledMediaTypes: [],
9696
MALAPIManga_disabledMediaTypes: [],
9797
ComicVineAPI_disabledMediaTypes: [],

0 commit comments

Comments
 (0)