|
| 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 { SeriesModel } from '../../models/SeriesModel'; |
| 6 | +import { MediaType } from '../../utils/MediaType'; |
| 7 | +import { APIModel } from '../APIModel'; |
| 8 | + |
| 9 | +export class TMDBSeriesAPI extends APIModel { |
| 10 | + plugin: MediaDbPlugin; |
| 11 | + typeMappings: Map<string, string>; |
| 12 | + apiDateFormat: string = 'YYYY-MM-DD'; |
| 13 | + |
| 14 | + constructor(plugin: MediaDbPlugin) { |
| 15 | + super(); |
| 16 | + |
| 17 | + this.plugin = plugin; |
| 18 | + this.apiName = 'TMDBSeriesAPI'; |
| 19 | + this.apiDescription = 'A community built Series DB.'; |
| 20 | + this.apiUrl = 'https://www.themoviedb.org/'; |
| 21 | + this.types = [MediaType.Series]; |
| 22 | + this.typeMappings = new Map<string, string>(); |
| 23 | + this.typeMappings.set('tv', 'series'); |
| 24 | + } |
| 25 | + |
| 26 | + async searchByTitle(title: string): Promise<MediaTypeModel[]> { |
| 27 | + console.log(`MDB | api "${this.apiName}" queried by Title`); |
| 28 | + |
| 29 | + if (!this.plugin.settings.TMDBKey) { |
| 30 | + throw new Error(`MDB | API key for ${this.apiName} missing.`); |
| 31 | + } |
| 32 | + |
| 33 | + const searchUrl = `https://api.themoviedb.org/3/search/tv?api_key=${this.plugin.settings.TMDBKey}&query=${encodeURIComponent(title)}&include_adult=${this.plugin.settings.sfwFilter ? 'false' : 'true'}`; |
| 34 | + const fetchData = await fetch(searchUrl); |
| 35 | + |
| 36 | + if (fetchData.status === 401) { |
| 37 | + throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`); |
| 38 | + } |
| 39 | + if (fetchData.status !== 200) { |
| 40 | + throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`); |
| 41 | + } |
| 42 | + |
| 43 | + const data = await fetchData.json(); |
| 44 | + |
| 45 | + if (data.total_results === 0) { |
| 46 | + if (data.Error === 'Series not found!') { |
| 47 | + return []; |
| 48 | + } |
| 49 | + |
| 50 | + throw Error(`MDB | Received error from ${this.apiName}: \n${JSON.stringify(data, undefined, 4)}`); |
| 51 | + } |
| 52 | + if (!data.results) { |
| 53 | + return []; |
| 54 | + } |
| 55 | + |
| 56 | + // console.debug(data.results); |
| 57 | + |
| 58 | + const ret: MediaTypeModel[] = []; |
| 59 | + |
| 60 | + for (const result of data.results) { |
| 61 | + ret.push( |
| 62 | + new SeriesModel({ |
| 63 | + type: 'series', |
| 64 | + title: result.original_name, |
| 65 | + englishTitle: result.name, |
| 66 | + year: result.first_air_date ? new Date(result.first_air_date).getFullYear().toString() : 'unknown', |
| 67 | + dataSource: this.apiName, |
| 68 | + id: result.id, |
| 69 | + }), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + return ret; |
| 74 | + } |
| 75 | + |
| 76 | + async getById(id: string): Promise<MediaTypeModel> { |
| 77 | + console.log(`MDB | api "${this.apiName}" queried by ID`); |
| 78 | + |
| 79 | + if (!this.plugin.settings.TMDBKey) { |
| 80 | + throw Error(`MDB | API key for ${this.apiName} missing.`); |
| 81 | + } |
| 82 | + |
| 83 | + const searchUrl = `https://api.themoviedb.org/3/tv/${encodeURIComponent(id)}?api_key=${this.plugin.settings.TMDBKey}&append_to_response=credits`; |
| 84 | + const fetchData = await fetch(searchUrl); |
| 85 | + |
| 86 | + if (fetchData.status === 401) { |
| 87 | + throw Error(`MDB | Authentication for ${this.apiName} failed. Check the API key.`); |
| 88 | + } |
| 89 | + if (fetchData.status !== 200) { |
| 90 | + throw Error(`MDB | Received status code ${fetchData.status} from ${this.apiName}.`); |
| 91 | + } |
| 92 | + |
| 93 | + const result = await fetchData.json(); |
| 94 | + // console.debug(result); |
| 95 | + |
| 96 | + return new SeriesModel({ |
| 97 | + type: 'series', |
| 98 | + title: result.original_name, |
| 99 | + englishTitle: result.name, |
| 100 | + year: result.first_air_date ? new Date(result.first_air_date).getFullYear().toString() : 'unknown', |
| 101 | + dataSource: this.apiName, |
| 102 | + url: `https://www.themoviedb.org/tv/${result.id}`, |
| 103 | + id: result.id, |
| 104 | + |
| 105 | + plot: result.overview ?? '', |
| 106 | + genres: result.genres.map((g: any) => g.name) ?? [], |
| 107 | + writer: result.created_by.map((c: any) => c.name) ?? [], |
| 108 | + studio: result.production_companies.map((s: any) => s.name) ?? [], |
| 109 | + episodes: result.number_of_episodes, |
| 110 | + duration: result.episode_run_time[0] ?? 'unknown', |
| 111 | + onlineRating: result.vote_average, |
| 112 | + actors: result.credits.cast.map((c: any) => c.name).slice(0, 5) ?? [], |
| 113 | + image: `https://image.tmdb.org/t/p/w780${result.poster_path}`, |
| 114 | + |
| 115 | + released:['Returning Series','Cancelled','Ended'].includes(result.status), |
| 116 | + streamingServices: [], |
| 117 | + airing: ['Returning Series'].includes(result.status), |
| 118 | + airedFrom: this.plugin.dateFormatter.format(result.first_air_date, this.apiDateFormat) ?? 'unknown', |
| 119 | + airedTo: ['Returning Series'].includes(result.status) ? 'unknown' : this.plugin.dateFormatter.format(result.last_air_date, this.apiDateFormat) ?? 'unknown', |
| 120 | + |
| 121 | + userData: { |
| 122 | + watched: false, |
| 123 | + lastWatched: '', |
| 124 | + personalRating: 0, |
| 125 | + }, |
| 126 | + }); |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + getDisabledMediaTypes(): MediaType[] { |
| 131 | + return this.plugin.settings.TMDBSeriesAPI_disabledMediaTypes as MediaType[]; |
| 132 | + } |
| 133 | +} |
| 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 | +} |
0 commit comments