44} from "../../../generated/prisma/index.js" ;
55import { BaseMetadataProvider } from "../provider.js" ;
66import logger from "../../../config/logger.js" ;
7+ import { prisma } from "../../../lib/prisma.js" ;
78import type {
89 MediaMetadata ,
910 MediaSearchResult ,
@@ -14,9 +15,8 @@ import type {
1415
1516/**
1617 * TMDB API configuration
17- * Set TMDB_API_KEY environment variable to enable
18+ * Configure via application settings (Settings > Libraries > TMDB API Key)
1819 */
19- const TMDB_API_KEY = process . env . TMDB_API_KEY ;
2020const TMDB_BASE_URL = "https://api.themoviedb.org/3" ;
2121const TMDB_IMAGE_BASE_URL = "https://image.tmdb.org/t/p" ;
2222
@@ -133,11 +133,28 @@ export class TMDBProvider extends BaseMetadataProvider {
133133 readonly source = ExternalIdSource . TMDB ;
134134 readonly supportedMediaTypes = [ MediaType . MOVIE , MediaType . TV_SHOW ] ;
135135
136+ /**
137+ * Get TMDB API key from database settings
138+ */
139+ private async getApiKey ( ) : Promise < string | null > {
140+ try {
141+ const settings = await prisma . settings . findUnique ( {
142+ where : { id : "default" } ,
143+ select : { tmdbApiKey : true } ,
144+ } ) ;
145+ return settings ?. tmdbApiKey || null ;
146+ } catch ( error ) {
147+ logger . error ( "Failed to fetch TMDB API key from database:" , { error } ) ;
148+ return null ;
149+ }
150+ }
151+
136152 /**
137153 * Check if TMDB API key is configured
138154 */
139155 override async isAvailable ( ) : Promise < boolean > {
140- return ! ! TMDB_API_KEY ;
156+ const apiKey = await this . getApiKey ( ) ;
157+ return ! ! apiKey ;
141158 }
142159
143160 /**
@@ -147,14 +164,16 @@ export class TMDBProvider extends BaseMetadataProvider {
147164 endpoint : string ,
148165 params : Record < string , string | number | boolean > = { }
149166 ) : Promise < T | null > {
150- if ( ! TMDB_API_KEY ) {
167+ const apiKey = await this . getApiKey ( ) ;
168+
169+ if ( ! apiKey ) {
151170 logger . warn ( "TMDB API key not configured" ) ;
152171 return null ;
153172 }
154173
155174 try {
156175 const url = new URL ( `${ TMDB_BASE_URL } ${ endpoint } ` ) ;
157- url . searchParams . append ( "api_key" , TMDB_API_KEY ) ;
176+ url . searchParams . append ( "api_key" , apiKey ) ;
158177
159178 for ( const [ key , value ] of Object . entries ( params ) ) {
160179 url . searchParams . append ( key , String ( value ) ) ;
0 commit comments