@@ -5,6 +5,7 @@ import { createTMDBValidator } from '../middleware/validation.js'
55import { OMSSErrors } from '../core/errors.js'
66import { TMDBService } from '../services/tmdb.service.js'
77import { v4 as uuidv4 } from 'uuid'
8+ import { StremioService } from './stremio.service.js'
89
910export class SourceService {
1011 private tmdbValidator : ReturnType < typeof createTMDBValidator >
@@ -14,6 +15,7 @@ export class SourceService {
1415 private registry : ProviderRegistry ,
1516 private cache : CacheService ,
1617 private tmdbService : TMDBService ,
18+ private stremioService : StremioService ,
1719 private cacheTTL = { sources : 7200 , subtitles : 86400 }
1820 ) {
1921 setInterval ( ( ) => this . cleanupExpiredMappings ( ) , 60 * 60 * 1000 )
@@ -41,11 +43,29 @@ export class SourceService {
4143 const media = await this . tmdbService . getMediaObject ( 'movie' , tmdbId )
4244
4345 // Try to get IMDB ID
44- media . imdbId = await this . tmdbService . getImdbId ( tmdbId , 'movie' )
46+ media . imdbId = await this . tmdbService . getImdbId ( tmdbId , 'movie' ) ?? ''
4547
46- // Fetch from providers
47- const results = await this . fetchFromProviders ( 'movie' , media )
48- const response = this . buildResponse ( results )
48+ // Fetch from providers and Stremio addons concurrently
49+ const [ providerResults , stremioResult ] = await Promise . all ( [
50+ this . fetchFromProviders ( 'movie' , media ) ,
51+ this . stremioService ?. hasEnabledAddons ( )
52+ ? this . stremioService . getMovieSources ( media ) . catch ( ( err ) : ProviderResult => ( {
53+ sources : [ ] ,
54+ subtitles : [ ] ,
55+ diagnostics : [ {
56+ code : 'PROVIDER_ERROR' ,
57+ message : `Stremio integration failed: ${ err instanceof Error ? err . message : 'Unknown error' } ` ,
58+ field : '' ,
59+ severity : 'error' ,
60+ } ] ,
61+ } ) )
62+ : Promise . resolve ( null ) ,
63+ ] )
64+
65+ const allResults : ProviderResult [ ] = [ ...providerResults ]
66+ if ( stremioResult ) allResults . push ( stremioResult )
67+
68+ const response = this . buildResponse ( allResults )
4969
5070 // Throw error if no sources found
5171 if ( response . sources . length === 0 ) {
@@ -87,11 +107,29 @@ export class SourceService {
87107 const media = await this . tmdbService . getMediaObject ( 'tv' , tmdbId , season , episode )
88108
89109 // Try to get IMDB ID
90- media . imdbId = await this . tmdbService . getImdbId ( tmdbId , 'tv' )
110+ media . imdbId = await this . tmdbService . getImdbId ( tmdbId , 'tv' ) ?? ''
91111
92- // Fetch from providers
93- const results = await this . fetchFromProviders ( 'tv' , media )
94- const response = this . buildResponse ( results )
112+ // Fetch from providers and Stremio addons concurrently
113+ const [ providerResults , stremioResult ] = await Promise . all ( [
114+ this . fetchFromProviders ( 'tv' , media ) ,
115+ this . stremioService ?. hasEnabledAddons ( )
116+ ? this . stremioService . getTVSources ( media ) . catch ( ( err ) : ProviderResult => ( {
117+ sources : [ ] ,
118+ subtitles : [ ] ,
119+ diagnostics : [ {
120+ code : 'PROVIDER_ERROR' ,
121+ message : `Stremio integration failed: ${ err instanceof Error ? err . message : 'Unknown error' } ` ,
122+ field : '' ,
123+ severity : 'error' ,
124+ } ] ,
125+ } ) )
126+ : Promise . resolve ( null ) ,
127+ ] )
128+
129+ const allResults : ProviderResult [ ] = [ ...providerResults ]
130+ if ( stremioResult ) allResults . push ( stremioResult )
131+
132+ const response = this . buildResponse ( allResults )
95133
96134 // Throw error if no sources found
97135 if ( response . sources . length === 0 ) {
0 commit comments