@@ -295,11 +295,12 @@ print(json.dumps({'title': f.get('title',''), 'year': f.get('year','')}))
295295 }
296296}
297297
298- async function searchTMDb ( title , year , language ) {
299- if ( ! TMDB_API_KEY ) return null ;
298+ async function searchTMDb ( title , year , language , apiKey = null ) {
299+ const tmdbKey = apiKey || TMDB_API_KEY ;
300+ if ( ! tmdbKey ) return null ;
300301
301302 const query = qs . escape ( cleanTitle ( title ) ) ;
302- const url = `https://api.themoviedb.org/3/search/movie?api_key=${ TMDB_API_KEY } &query=${ query } &language=${ language } ` ;
303+ const url = `https://api.themoviedb.org/3/search/movie?api_key=${ tmdbKey } &query=${ query } &language=${ language } ` ;
303304
304305 try {
305306 const res = await axios . get ( url ) ;
@@ -330,15 +331,15 @@ async function searchTMDb(title, year, language) {
330331 }
331332}
332333
333- async function getCachedMovie ( title , year , language ) {
334+ async function getCachedMovie ( title , year , language , apiKey = null ) {
334335 const key = safeName ( `${ title } _${ year } _${ language } ` ) . toLowerCase ( ) ;
335336 const file = path . join ( CACHE_DIR , key + '.json' ) ;
336337
337338 if ( fs . existsSync ( file ) ) {
338339 try { return JSON . parse ( fs . readFileSync ( file ) ) ; } catch { }
339340 }
340341
341- const movie = await searchTMDb ( title , year , language ) ;
342+ const movie = await searchTMDb ( title , year , language , apiKey ) ;
342343 if ( movie ) fs . writeFileSync ( file , JSON . stringify ( movie , null , 2 ) ) ;
343344 return movie ;
344345}
@@ -524,9 +525,17 @@ async function getVideoFiles(customMediaConfig = null) {
524525 * Process specific files with progress callback
525526 * @param {string[] } filePaths - Array of file paths to process
526527 * @param {function } progressCallback - Callback for progress updates
528+ * @param {object } options - Configuration options
529+ * @param {Array } options.mediaConfig - Custom media configuration (from configManager)
530+ * @param {Array } options.trackers - Tracker URLs
531+ * @param {string } options.tmdbApiKey - TMDb API key
527532 * @returns {Promise<object> } - Processing statistics
528533 */
529- async function processFiles ( filePaths , progressCallback = null ) {
534+ async function processFiles ( filePaths , progressCallback = null , options = { } ) {
535+ const { mediaConfig : customMediaConfig , trackers : customTrackers , tmdbApiKey : customTmdbApiKey } = options ;
536+ const trackersToUse = customTrackers && customTrackers . length > 0 ? customTrackers : TRACKERS ;
537+ const tmdbKeyToUse = customTmdbApiKey || TMDB_API_KEY ;
538+
530539 const localProcessed = [ ] ;
531540 const localSkipped = [ ] ;
532541 const localTmdbFound = [ ] ;
@@ -559,9 +568,12 @@ async function processFiles(filePaths, progressCallback = null) {
559568 index ++ ;
560569
561570 try {
571+ // Use custom config if provided, otherwise fall back to MEDIA_CONFIG
572+ const mediaConfig = customMediaConfig && customMediaConfig . length > 0 ? customMediaConfig : MEDIA_CONFIG ;
573+
562574 // Find the correct destination base for this file
563575 let destBase = null ;
564- for ( const media of MEDIA_CONFIG ) {
576+ for ( const media of mediaConfig ) {
565577 if ( file . startsWith ( media . source ) ) {
566578 destBase = media . dest ;
567579 break ;
@@ -616,14 +628,14 @@ Generated by torrentify
616628 }
617629
618630 if ( ! fileExistsAndNotEmpty ( torrent ) ) {
619- if ( ! TRACKERS . length ) {
631+ if ( ! trackersToUse . length ) {
620632 log ( `⚠️ Torrent non créé - TRACKERS non configuré` , 'warn' ) ;
621633 } else {
622634 // Remove empty torrent file if exists
623635 if ( fs . existsSync ( torrent ) ) fs . unlinkSync ( torrent ) ;
624636
625637 log ( `🔧 Création du torrent (peut prendre plusieurs minutes)...` ) ;
626- const trackers = TRACKERS . flatMap ( t => [ '-a' , t ] ) ;
638+ const trackers = trackersToUse . flatMap ( t => [ '-a' , t ] ) ;
627639 await execAsync ( 'mktorrent' , [
628640 '-l' , getMktorrentL ( file ) . toString ( ) ,
629641 '-p' ,
@@ -639,9 +651,9 @@ Generated by torrentify
639651 const guess = await runPythonGuessit ( file ) ;
640652
641653 const movie =
642- await getCachedMovie ( guess . title , guess . year , 'en-US' ) ||
643- await getCachedMovie ( guess . title , guess . year , 'fr-FR' ) ||
644- await getCachedMovie ( guess . title , '' , 'en-US' ) ;
654+ await getCachedMovie ( guess . title , guess . year , 'en-US' , tmdbKeyToUse ) ||
655+ await getCachedMovie ( guess . title , guess . year , 'fr-FR' , tmdbKeyToUse ) ||
656+ await getCachedMovie ( guess . title , '' , 'en-US' , tmdbKeyToUse ) ;
645657
646658 if ( movie ?. id ) {
647659 localTmdbFound . push ( file ) ;
@@ -770,18 +782,29 @@ function getMktorrentLForDir(totalSize) {
770782 * @param {object[] } files - Array of file objects in the directory
771783 * @param {function } progressCallback - Callback for progress updates
772784 * @param {string } customTorrentName - Optional custom name for the torrent
785+ * @param {object } options - Configuration options
786+ * @param {Array } options.mediaConfig - Custom media configuration (from configManager)
787+ * @param {Array } options.trackers - Tracker URLs
788+ * @param {string } options.tmdbApiKey - TMDb API key
773789 * @returns {Promise<object> } - Processing result
774790 */
775- async function processDirectory ( dirPath , files , progressCallback = null , customTorrentName = null ) {
791+ async function processDirectory ( dirPath , files , progressCallback = null , customTorrentName = null , options = { } ) {
792+ const { mediaConfig : customMediaConfig , trackers : customTrackers , tmdbApiKey : customTmdbApiKey } = options ;
793+ const trackersToUse = customTrackers && customTrackers . length > 0 ? customTrackers : TRACKERS ;
794+ const tmdbKeyToUse = customTmdbApiKey || TMDB_API_KEY ;
795+
776796 const dirName = path . basename ( dirPath ) ;
777797 // Use custom torrent name if provided, otherwise use directory name
778798 const torrentName = customTorrentName || dirName ;
779799 const safeFolder = safeName ( torrentName ) ;
780800
801+ // Use custom config if provided, otherwise fall back to MEDIA_CONFIG
802+ const mediaConfig = customMediaConfig && customMediaConfig . length > 0 ? customMediaConfig : MEDIA_CONFIG ;
803+
781804 // Find the correct destination base for this directory
782805 let destBase = null ;
783806 let mediaType = null ;
784- for ( const media of MEDIA_CONFIG ) {
807+ for ( const media of mediaConfig ) {
785808 if ( dirPath . startsWith ( media . source ) ) {
786809 destBase = media . dest ;
787810 mediaType = media . type ;
@@ -892,14 +915,14 @@ Generated by torrentify
892915
893916 // 2. Create single torrent for the directory
894917 if ( ! fileExistsAndNotEmpty ( torrent ) ) {
895- if ( ! TRACKERS . length ) {
918+ if ( ! trackersToUse . length ) {
896919 log ( `⚠️ Torrent non créé - TRACKERS non configuré` , 'warn' ) ;
897920 } else {
898921 if ( fs . existsSync ( torrent ) ) fs . unlinkSync ( torrent ) ;
899922
900923 log ( `🔧 Création du torrent pour le répertoire (${ formatSize ( totalSize ) } )...` ) ;
901924 log ( `📛 Nom du torrent: ${ torrentName } ` ) ;
902- const trackers = TRACKERS . flatMap ( t => [ '-a' , t ] ) ;
925+ const trackers = trackersToUse . flatMap ( t => [ '-a' , t ] ) ;
903926
904927 await execAsync ( 'mktorrent' , [
905928 '-l' , getMktorrentLForDir ( totalSize ) . toString ( ) ,
@@ -929,10 +952,10 @@ Generated by torrentify
929952 // Search for TV show (not movie)
930953 let found = null ;
931954 const searchTv = async ( title , year , lang ) => {
932- if ( ! TMDB_API_KEY ) return null ;
955+ if ( ! tmdbKeyToUse ) return null ;
933956
934957 const query = qs . escape ( cleanTitle ( title ) ) ;
935- const url = `https://api.themoviedb.org/3/search/tv?api_key=${ TMDB_API_KEY } &query=${ query } &language=${ lang } ` ;
958+ const url = `https://api.themoviedb.org/3/search/tv?api_key=${ tmdbKeyToUse } &query=${ query } &language=${ lang } ` ;
936959 try {
937960 const res = await axios . get ( url ) ;
938961 if ( ! res . data . results ?. length ) return null ;
@@ -1005,9 +1028,13 @@ Generated by torrentify
10051028 * Process multiple directories
10061029 * @param {object[] } directories - Array of directory objects with path and files
10071030 * @param {function } progressCallback - Callback for progress updates
1031+ * @param {object } options - Configuration options
1032+ * @param {Array } options.mediaConfig - Custom media configuration (from configManager)
1033+ * @param {Array } options.trackers - Tracker URLs
1034+ * @param {string } options.tmdbApiKey - TMDb API key
10081035 * @returns {Promise<object> } - Processing statistics
10091036 */
1010- async function processDirectories ( directories , progressCallback = null ) {
1037+ async function processDirectories ( directories , progressCallback = null , options = { } ) {
10111038 const results = [ ] ;
10121039 const errors = [ ] ;
10131040 const startTime = Date . now ( ) ;
@@ -1035,7 +1062,7 @@ async function processDirectories(directories, progressCallback = null) {
10351062 }
10361063
10371064 try {
1038- const result = await processDirectory ( dir . path , dir . files , progressCallback , dir . customTorrentName ) ;
1065+ const result = await processDirectory ( dir . path , dir . files , progressCallback , dir . customTorrentName , options ) ;
10391066 results . push ( result ) ;
10401067 } catch ( error ) {
10411068 log ( `❌ Erreur pour ${ dir . name } : ${ error . message } ` , 'error' ) ;
0 commit comments