|
| 1 | +import { FastifyReply, FastifyRequest } from 'fastify' |
| 2 | +import { SourceService } from '../services/source.service.js' |
| 3 | +import { OMSSConfig, SourceResponse, Source } from '../core/types.js' |
| 4 | +import { TMDBService } from '../services/tmdb.service.js' |
| 5 | + |
| 6 | +interface StreamParams { |
| 7 | + type: string |
| 8 | + id: string |
| 9 | +} |
| 10 | + |
| 11 | +interface StremioStream { |
| 12 | + url?: string |
| 13 | + ytId?: string |
| 14 | + infoHash?: string |
| 15 | + fileIdx?: number |
| 16 | + name?: string |
| 17 | + title?: string |
| 18 | + description?: string |
| 19 | + behaviorHints?: { |
| 20 | + notWebReady?: boolean |
| 21 | + bingeGroup?: string |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +interface StremioManifest { |
| 26 | + id: string |
| 27 | + version: string |
| 28 | + name: string |
| 29 | + description: string |
| 30 | + logo?: string |
| 31 | + resources: Array<string | { name: string; types: string[] }> |
| 32 | + types: string[] |
| 33 | + catalogs: Array<any> |
| 34 | + idPrefixes?: string[] |
| 35 | +} |
| 36 | + |
| 37 | +export class StremioController { |
| 38 | + constructor( |
| 39 | + private readonly sourceService: SourceService, |
| 40 | + private readonly config: OMSSConfig, |
| 41 | + private readonly tmdbService: TMDBService |
| 42 | + ) {} |
| 43 | + |
| 44 | + /** |
| 45 | + * GET /stremio/manifest.json |
| 46 | + */ |
| 47 | + async getManifest(_req: FastifyRequest, reply: FastifyReply) { |
| 48 | + const safeName = this.config.name |
| 49 | + .toLowerCase() |
| 50 | + .replace(/[^a-z\s]/g, '') |
| 51 | + .trim() |
| 52 | + .replace(/\s+/g, '.') |
| 53 | + |
| 54 | + const manifest: StremioManifest = { |
| 55 | + id: `omss.${safeName}`, |
| 56 | + version: this.config.version, |
| 57 | + name: this.config.name, |
| 58 | + description: this.config.note || 'Your backend exposed as a Stremio addon', |
| 59 | + resources: ['stream'], |
| 60 | + types: ['movie', 'series'], |
| 61 | + catalogs: [], |
| 62 | + idPrefixes: ['tmdb', 'tt'], |
| 63 | + } |
| 64 | + |
| 65 | + return reply.code(200).send(manifest) |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Resolve an incoming Stremio ID to a TMDB ID string. |
| 70 | + * |
| 71 | + * Stremio uses: |
| 72 | + * - "tt1234567" → IMDb movie |
| 73 | + * - "tt1234567:1:2" → IMDb series S01E02 |
| 74 | + * - "tmdb:603" → TMDB movie |
| 75 | + * - "tmdb:1399:1:1" → TMDB series S01E01 |
| 76 | + * |
| 77 | + * Returns null when the ID cannot be resolved. |
| 78 | + */ |
| 79 | + private async resolveTmdbId(rawId: string, type: string): Promise<{ tmdbId: string; season?: number; episode?: number } | null> { |
| 80 | + const clean = rawId.replace(/\.json$/, '') |
| 81 | + const parts = clean.split(':') |
| 82 | + |
| 83 | + if (parts[0].startsWith('tt')) { |
| 84 | + const imdbId = parts[0] |
| 85 | + const season = parts[1] ? parseInt(parts[1], 10) : undefined |
| 86 | + const episode = parts[2] ? parseInt(parts[2], 10) : undefined |
| 87 | + |
| 88 | + const mediaType = type === 'series' || type === 'tv' ? 'tv' : 'movie' |
| 89 | + |
| 90 | + const tmdbId = await this.tmdbService.findTmdbIdByImdbId(imdbId, mediaType) |
| 91 | + |
| 92 | + if (!tmdbId) return null |
| 93 | + |
| 94 | + return { tmdbId, season, episode } |
| 95 | + } |
| 96 | + |
| 97 | + if (parts[0] === 'tmdb' && parts.length >= 2) { |
| 98 | + const tmdbId = parts[1] |
| 99 | + const season = parts[2] ? parseInt(parts[2], 10) : undefined |
| 100 | + const episode = parts[3] ? parseInt(parts[3], 10) : undefined |
| 101 | + return { tmdbId, season, episode } |
| 102 | + } |
| 103 | + |
| 104 | + return null |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * GET /stremio/stream/:type/:id.json |
| 109 | + * |
| 110 | + * Supported ID formats: |
| 111 | + * Movies: tt1234567 | tmdb:603 |
| 112 | + * Series: tt1234567:1:2 | tmdb:1399:1:1 |
| 113 | + */ |
| 114 | + async getStream(request: FastifyRequest<{ Params: StreamParams }>, reply: FastifyReply) { |
| 115 | + const { type, id } = request.params |
| 116 | + const mediaType = type === 'series' || type === 'tv' ? 'tv' : 'movie' |
| 117 | + |
| 118 | + const resolved = await this.resolveTmdbId(id, type) |
| 119 | + |
| 120 | + if (!resolved) { |
| 121 | + return reply.code(400).send({ |
| 122 | + error: { |
| 123 | + code: 'INVALID_PARAMETER', |
| 124 | + message: 'Invalid ID format or unsupported ID type', |
| 125 | + }, |
| 126 | + traceId: request.id, |
| 127 | + }) |
| 128 | + } |
| 129 | + |
| 130 | + const { tmdbId, season, episode } = resolved |
| 131 | + |
| 132 | + try { |
| 133 | + let omssResponse: SourceResponse | null = null |
| 134 | + const mediaType = type === 'movie' ? 'movie' : 'tv' |
| 135 | + |
| 136 | + const mediaObject = await this.tmdbService.getMediaObject(mediaType, tmdbId, season, episode) |
| 137 | + const mediaTitle = mediaObject.title ?? tmdbId |
| 138 | + |
| 139 | + if (type === 'movie') { |
| 140 | + omssResponse = await this.sourceService.getMovieSources(tmdbId) |
| 141 | + } else if (type === 'series' || type === 'tv') { |
| 142 | + if (season === undefined || episode === undefined || !Number.isFinite(season) || !Number.isFinite(episode)) { |
| 143 | + return reply.code(400).send({ |
| 144 | + error: { |
| 145 | + code: 'INVALID_PARAMETER', |
| 146 | + message: 'An error occurred while processing the request', |
| 147 | + }, |
| 148 | + traceId: request.id, |
| 149 | + }) |
| 150 | + } |
| 151 | + omssResponse = await this.sourceService.getTVSources(tmdbId, season, episode) |
| 152 | + } else { |
| 153 | + return reply.code(400).send({ |
| 154 | + error: { |
| 155 | + code: 'INVALID_PARAMETER', |
| 156 | + message: 'An error occurred while processing the request', |
| 157 | + }, |
| 158 | + traceId: request.id, |
| 159 | + }) |
| 160 | + } |
| 161 | + |
| 162 | + const streams: StremioStream[] = (omssResponse.sources || []).map((source: Source, index: number): StremioStream => { |
| 163 | + // e.g. "4K UHD • HLS" or "1080p • MP4" |
| 164 | + const name = `${this.config.name} [${source.quality} • ${source.type.toUpperCase()}]` |
| 165 | + |
| 166 | + // Audio track summary: "EN, FR, DE" or "EN" or omit if empty |
| 167 | + const audioSummary = source.audioTracks.length > 0 ? source.audioTracks.map((t) => t.label || t.language.toUpperCase()).join(', ') : null |
| 168 | + |
| 169 | + // Multi-line description rendered by Stremio below the name |
| 170 | + const descLines: string[] = [ |
| 171 | + `📡 Provider: ${source.provider.name}`, |
| 172 | + |
| 173 | + ] |
| 174 | + |
| 175 | + if (audioSummary) { |
| 176 | + descLines.push(`🔊 ${audioSummary}`) |
| 177 | + } |
| 178 | + descLines.push(`🛡️ Proxied`) |
| 179 | + |
| 180 | + const bingeGroup = `${this.config.name}-${source.provider.id}-${source.quality}`.toLowerCase().replace(/\s+/g, '-') |
| 181 | + |
| 182 | + return { |
| 183 | + url: source.url, |
| 184 | + name, |
| 185 | + title: descLines.join('\n'), |
| 186 | + behaviorHints: { |
| 187 | + bingeGroup, |
| 188 | + }, |
| 189 | + } |
| 190 | + }) |
| 191 | + |
| 192 | + return reply.code(200).send({ streams }) |
| 193 | + } catch (err) { |
| 194 | + request.log.error({ err, type, id }, '[Stremio] Error resolving streams') |
| 195 | + return reply.code(400).send({ |
| 196 | + error: { |
| 197 | + code: 'INVALID_PARAMETER', |
| 198 | + message: 'An error occurred while processing the request', |
| 199 | + }, |
| 200 | + traceId: request.id, |
| 201 | + }) |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments