Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added custom TMDB api key #668

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ volumes:
| DATABASE_PASSWORD | Database password |
| DATABASE_DATABASE | Database name |
| IGDB_CLIENT_ID | IGDB API key, needed for game lookup |
| IGDB_CLIENT_SECRET | IGDB secret |
| IGDB_CLIENT_SECRET | IGDB secret
| TMDB_API_KEY | Custom TMDB API key |
| PUID | UserID |
| PGID | GroupID |
| TZ | Timezone, for example `Europe/London`, see [full list](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) |
Expand Down
8 changes: 8 additions & 0 deletions server/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
],
"nullable": true
},
"tmdbApiKey": {
"type": "string",
"nullable": true
},
"igdbClientId": {
"type": "string",
"nullable": true
Expand Down Expand Up @@ -170,6 +174,10 @@
],
"nullable": true
},
"tmdbApiKey": {
"type": "string",
"nullable": true
},
"igdbClientId": {
"type": "string",
"nullable": true
Expand Down
8 changes: 7 additions & 1 deletion server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
tmdbLang,
TmdbLang,
} from 'src/entity/configuration';
import { validateTmdbApiKey } from './utils';

/**
* Logger depends on LOGS_PATH, it cannot be used here.
Expand Down Expand Up @@ -61,6 +62,7 @@ export class Config {
static readonly DEMO = process.env.DEMO ? Boolean(process.env.DEMO) : false;
static readonly IGDB_CLIENT_ID = process.env.IGDB_CLIENT_ID;
static readonly IGDB_CLIENT_SECRET = process.env.IGDB_CLIENT_SECRET;
static readonly TMDB_API_KEY = process.env.TMDB_API_KEY || '779734046efc1e6127485c54d3b29627';

static readonly HOSTNAME = process.env.HOSTNAME || '127.0.0.1';
static readonly PORT = Number(process.env.PORT) || 7481;
Expand All @@ -71,7 +73,7 @@ export class Config {
static readonly AUDIBLE_LANG =
process.env.AUDIBLE_LANG?.toLowerCase() as AudibleCountryCode;

static validate() {
static async validate() {
if (this.SERVER_LANG && !serverLang.includes(this.SERVER_LANG)) {
throw new Error(
`SERVER_LANG should be one of: ${serverLang}, received: ${this.SERVER_LANG}`
Expand All @@ -89,6 +91,10 @@ export class Config {
`AUDIBLE_LANG should be one of: ${audibleLang}, received: ${this.AUDIBLE_LANG}`
);
}
const isTmdbApiKeyValid: boolean = await validateTmdbApiKey(this.TMDB_API_KEY)
if(isTmdbApiKeyValid !== true){
throw new Error("TMDB_API_KEY is Invalid")
}
}

static migrate() {
Expand Down
1 change: 1 addition & 0 deletions server/src/entity/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type Configuration = {
tmdbLang?: TmdbLang;
audibleLang?: AudibleCountryCode;
serverLang?: ServerLang;
tmdbApiKey?: string;
igdbClientId?: string;
igdbClientSecret?: string;
};
Expand Down
1 change: 1 addition & 0 deletions server/src/generated/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ router.patch(
serverLang: {
oneOf: [{ $ref: '#/definitions/ServerLang' }, { type: 'null' }],
},
tmdbApiKey: { type: ['string', 'null'] },
igdbClientId: { type: ['string', 'null'] },
igdbClientSecret: { type: ['string', 'null'] },
},
Expand Down
22 changes: 10 additions & 12 deletions server/src/metadata/provider/tmdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { MediaItemForProvider, ExternalIds } from 'src/entity/mediaItem';
import { MetadataProvider } from 'src/metadata/metadataProvider';
import { GlobalConfiguration } from 'src/repository/globalSettings';

const TMDB_API_KEY = '779734046efc1e6127485c54d3b29627';

type PosterSize =
| 'w92'
| 'w154'
Expand Down Expand Up @@ -59,7 +57,7 @@ export class TMDbMovie extends TMDb {
'https://api.themoviedb.org/3/search/movie',
{
params: {
api_key: TMDB_API_KEY,
api_key: GlobalConfiguration.configuration.tmdbApiKey,
query: query,
language: GlobalConfiguration.configuration.tmdbLang,
},
Expand All @@ -76,7 +74,7 @@ export class TMDbMovie extends TMDb {
`https://api.themoviedb.org/3/movie/${mediaItem.tmdbId}`,
{
params: {
api_key: TMDB_API_KEY,
api_key: GlobalConfiguration.configuration.tmdbApiKey,
language: GlobalConfiguration.configuration.tmdbLang,
},
}
Expand All @@ -91,7 +89,7 @@ export class TMDbMovie extends TMDb {
async findByImdbId(imdbId: string): Promise<MediaItemForProvider> {
const res = await axios.get(`https://api.themoviedb.org/3/find/${imdbId}`, {
params: {
api_key: TMDB_API_KEY,
api_key: GlobalConfiguration.configuration.tmdbApiKey,
external_source: 'imdb_id',
language: GlobalConfiguration.configuration.tmdbLang,
},
Expand Down Expand Up @@ -133,7 +131,7 @@ export class TMDbTv extends TMDb {
'https://api.themoviedb.org/3/search/tv',
{
params: {
api_key: TMDB_API_KEY,
api_key: GlobalConfiguration.configuration.tmdbApiKey,
query: query,
language: GlobalConfiguration.configuration.tmdbLang,
},
Expand All @@ -151,7 +149,7 @@ export class TMDbTv extends TMDb {
`https://api.themoviedb.org/3/tv/${mediaItem.tmdbId}`,
{
params: {
api_key: TMDB_API_KEY,
api_key: GlobalConfiguration.configuration.tmdbApiKey,
append_to_response: 'external_ids',
language: GlobalConfiguration.configuration.tmdbLang,
},
Expand All @@ -166,7 +164,7 @@ export class TMDbTv extends TMDb {
`https://api.themoviedb.org/3/tv/${mediaItem.tmdbId}/season/${season.seasonNumber}`,
{
params: {
api_key: TMDB_API_KEY,
api_key: GlobalConfiguration.configuration.tmdbApiKey,
append_to_response: 'external_ids',
language: GlobalConfiguration.configuration.tmdbLang,
},
Expand All @@ -188,7 +186,7 @@ export class TMDbTv extends TMDb {
async findByImdbId(imdbId: string): Promise<MediaItemForProvider> {
const res = await axios.get(`https://api.themoviedb.org/3/find/${imdbId}`, {
params: {
api_key: TMDB_API_KEY,
api_key: GlobalConfiguration.configuration.tmdbApiKey,
external_source: 'imdb_id',
language: GlobalConfiguration.configuration.tmdbLang,
},
Expand All @@ -208,7 +206,7 @@ export class TMDbTv extends TMDb {
async findByTvdbId(tvdbId: number): Promise<MediaItemForProvider> {
const res = await axios.get(`https://api.themoviedb.org/3/find/${tvdbId}`, {
params: {
api_key: TMDB_API_KEY,
api_key: GlobalConfiguration.configuration.tmdbApiKey,
external_source: 'tvdb_id',
language: GlobalConfiguration.configuration.tmdbLang,
},
Expand All @@ -234,7 +232,7 @@ export class TMDbTv extends TMDb {
`https://api.themoviedb.org/3/find/${episodeImdbId}`,
{
params: {
api_key: TMDB_API_KEY,
api_key: GlobalConfiguration.configuration.tmdbApiKey,
external_source: 'imdb_id',
language: GlobalConfiguration.configuration.tmdbLang,
},
Expand All @@ -258,7 +256,7 @@ export class TMDbTv extends TMDb {
`https://api.themoviedb.org/3/find/${episodeTvdbId}`,
{
params: {
api_key: TMDB_API_KEY,
api_key: GlobalConfiguration.configuration.tmdbApiKey,
external_source: 'tvdb_id',
language: GlobalConfiguration.configuration.tmdbLang,
},
Expand Down
9 changes: 7 additions & 2 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@
demo?: boolean;
};
export class Application {
#server: Server;

Check warning on line 210 in server/src/server.ts

View check run for this annotation

codefactor.io / CodeFactor

server/src/server.ts#L210

'#server' is defined but never used. (no-unused-private-class-members)
#config: ApplicationConfig;
#sessionKey: string;

Check warning on line 212 in server/src/server.ts

View check run for this annotation

codefactor.io / CodeFactor

server/src/server.ts#L212

'#sessionKey' is defined but never used. (no-unused-private-class-members)

constructor(config: ApplicationConfig) {
this.#config = config;
Expand All @@ -217,7 +217,7 @@

async initialize() {
Config.migrate();
Config.validate();
await Config.validate();
setupI18n(this.#config.serverLang);
logger.init();
Database.init();
Expand Down Expand Up @@ -288,6 +288,7 @@
audibleLang: AudibleCountryCode;
igdbClientId?: string;
igdbClientSecret?: string;
tmdbApiKey: string;
demo?: boolean;
}) => {
const {
Expand All @@ -296,10 +297,11 @@
audibleLang,
igdbClientId,
igdbClientSecret,
tmdbApiKey,
demo,
} = args;
Config.migrate();
Config.validate();
await Config.validate();
logger.init();
Database.init();
await Database.runMigrations();
Expand All @@ -319,6 +321,7 @@
audibleLang: audibleLang || 'us',
igdbClientId: igdbClientId,
igdbClientSecret: igdbClientSecret,
tmdbApiKey: tmdbApiKey
});
} else {
await configurationRepository.update({
Expand All @@ -327,6 +330,7 @@
audibleLang: audibleLang || configuration.audibleLang,
igdbClientId: igdbClientId || configuration.igdbClientId,
igdbClientSecret: igdbClientSecret || configuration.igdbClientSecret,
tmdbApiKey: tmdbApiKey || configuration.tmdbApiKey
});
}

Expand Down Expand Up @@ -415,6 +419,7 @@
audibleLang: Config.AUDIBLE_LANG,
igdbClientId: Config.IGDB_CLIENT_ID,
igdbClientSecret: Config.IGDB_CLIENT_SECRET,
tmdbApiKey : Config.TMDB_API_KEY,
demo: Config.DEMO,
});
server = new Server({
Expand Down
14 changes: 14 additions & 0 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,50 +53,50 @@
.toFile(imageSmallPath);
};

export const updateAsset = async (args: {
type: ImageType;
mediaItem?: MediaItemBase;
season?: TvSeason;
}) => {
const { mediaItem, season, type } = args;

const oldPosterId = season
? type === 'poster' && season.posterId
: type === 'poster' && mediaItem && !season
? mediaItem.posterId
: type === 'backdrop' && mediaItem && !season
? mediaItem.backdropId
: undefined;

const newImageId = getImageId();

if (type === 'poster' && season) {
await Database.knex('season')
.update('posterId', newImageId)
.where('id', season.id);
} else if (type === 'poster' && mediaItem && !season) {
await Database.knex('mediaItem')
.update('posterId', newImageId)
.where('id', mediaItem.id);
} else if (type === 'backdrop' && mediaItem && !season) {
await Database.knex('mediaItem')
.update('backdropId', newImageId)
.where('id', mediaItem.id);
}

if (oldPosterId) {
const imagePath = `/img/original/${oldPosterId}`;
if (await pathExists(imagePath)) {
await rm(imagePath);
}

const smallImagePath = `/img/small/${oldPosterId}`;
if (await pathExists(smallImagePath)) {
await rm(smallImagePath);
}
}
};

Check notice on line 99 in server/src/utils.ts

View check run for this annotation

codefactor.io / CodeFactor

server/src/utils.ts#L56-L99

Complex Method
export const catchAndLogError = async (fn: () => Promise<void> | void) => {
try {
await fn();
Expand Down Expand Up @@ -147,3 +147,17 @@
};

export const getImageId = customAlphabet('1234567890abcdef', 32);

export const validateTmdbApiKey = async (tmdbApiKey: string) => {
const apiKeyStatus = await axios.get("https://api.themoviedb.org/3/authentication", {
params: {
api_key: tmdbApiKey,
},
}).then((res) => {
return res.data
}).catch((error) => {
logger.error(error)
return {"success" : false}
});
return apiKeyStatus?.success
}
Loading