Create stremio add-on that fetch subtitles from subtitlecat.com
- User
Create stremio add-on that fetch subtitles from subtitlecat.com
- Assistant
Create stremio add-on that fetch subtitles from subtitlecat.com
Creating a Stremio add-on to fetch subtitles from subtitlecat.com involves several steps, including scraping subtitlecat.com (if they don't provide an official API), packaging the results in a Stremio-compatible add-on format, and serving the add-on using Node.js.
Important note:
Before scraping any website, you should check the website’s terms of service to ensure that scraping is allowed. Unauthorized scraping may be against the website's policy and could lead to legal issues.
stremio-addon-sdk.mkdir stremio-subtitlecat-addon
cd stremio-subtitlecat-addon
npm init -y
npm install stremio-addon-sdk axios cheerio
stremio-addon-sdk: for building the add-onaxios: to make HTTP requestscheerio: to parse HTML and scrape the dataCreate a file index.js:
const { addonBuilder } = require('stremio-addon-sdk')
const axios = require('axios')
const cheerio = require('cheerio')
const manifest = {
id: 'org.subtitlecat.subtitles',
version: '1.0.0',
name: 'SubtitleCat Subtitles',
description: 'Fetches subtitles from subtitlecat.com',
types: ['movie', 'series', 'episode'],
resources: ['subtitles'],
types: ['movie', 'series', 'episode'],
idPrefixes: ['tt'], // to recognize imdb ids
// No requires auth in this example
}
const builder = new addonBuilder(manifest)
builder.defineSubtitlesHandler(async ({ id, type, imdb, season, episode, query }) => {
// id is usually imdb id like tt1234567
// For episodes, season and episode number are present
try {
if (!imdb && !query) {
return { subtitles: [] }
}
// We'll search by imdb id or fallback to query string title
// Construct search term
let searchTerm = ''
if (imdb) {
// subtitlecat search doesn't support imdb id directly
// so we can skip or use a mapping service to get title by imdb id (omitted here)
// for simplicity, use query or imdb to get movie name by OMDB API
// but to keep it simple, let's assume query has the name
searchTerm = query || ''
} else if(query) {
searchTerm = query
}
if (!searchTerm) {
return { subtitles: [] }
}
// Search subtitlecat.com
const subtitles = await searchSubtitlesOnSubtitlecat(searchTerm, language='all', season, episode)
return { subtitles }
} catch (e) {
console.error('Error fetching subtitles:', e)
return { subtitles: [] }
}
})
async function searchSubtitlesOnSubtitlecat(title, language = 'all', season = null, episode = null) {
// subtitlecat.com search url example:
// https://www.subtitlecat.com/search?q=the+matrix
const searchUrl = `https://www.subtitlecat.com/search?q=${encodeURIComponent(title)}`
const { data } = await axios.get(searchUrl, {
headers: {
'User-Agent': 'Mozilla/5.0',
}
})
const $ = cheerio.load(data)
const subtitles = []
// Inspect subtitlecat.com search results structure:
// Results are in table with class "files"
// Each row <tr> corresponds to a subtitle file, inside <a> tag with href to download
$('table.files > tbody > tr').each((i, el) => {
const row = $(el)
const titleCell = row.find('td.name > a')
let subtitleTitle = titleCell.text().trim()
let href = titleCell.attr('href')
if (!href) return
// href is relative path to subtitle page, e.g. /subtitle/12345/the-matrix-1999-english-srt
// need to fetch actual download link from that page
// To simplify, pick subtitles only if the title contains season/episode matching if episode given
if (season && episode) {
const seasonEpString = `S${season.toString().padStart(2,'0')}E${episode.toString().padStart(2, '0')}`
if (!subtitleTitle.toUpperCase().includes(seasonEpString.toUpperCase())) {
return // skip subtitles not matching season/episode for episodes
}
}
// Get language code or name, the language is in separate cell td.lang
const lang = row.find('td.lang').text().trim().toLowerCase()
// We will scrape download link from subtitle page (href)
subtitles.push({ href: 'https://www.subtitlecat.com' + href, language: lang, title: subtitleTitle })
})
// Now fetch the download links for each subtitle page
const subtitlesWithDownloadLinks = []
for (const sub of subtitles) {
try {
const { data: subPageData } = await axios.get(sub.href, {
headers: { 'User-Agent': 'Mozilla/5.0' }
})
const $$ = cheerio.load(subPageData)
// The download button is usually a link/button with id or class .btn-download or inside some container
// On subtitlecat.com, the download link is inside <a class="btn btn-primary float-right" href="...">Download</a>
const downloadLink = $$('a.btn.btn-primary.float-right').attr('href')
if (downloadLink) {
// The download link might be relative, make absolute
const absDownloadLink = downloadLink.startsWith('http') ? downloadLink : `https://www.subtitlecat.com${downloadLink}`
subtitlesWithDownloadLinks.push({
id: absDownloadLink, // unique id
url: absDownloadLink,
lang: sub.language,
rel: 'subtitle',
ext: 'srt',
name: sub.title,
})
}
} catch (e) {
continue
}
}
return subtitlesWithDownloadLinks
}
const addonInterface = builder.getInterface()
// Run a simple Express server if this file is executed directly
if (require.main === module) {
const express = require('express')
const app = express()
app.use('/manifest.json', (req, res) => res.json(manifest))
app.use('/', addonInterface)
const port = process.env.PORT || 7000
app.listen(port, () => console.log(`SubtitleCat addon running on http://localhost:${port}`))
}
module.exports = builder.getInterface()
Run the addon with:
node index.js
The add-on will be available on http://localhost:7000/manifest.json
You can add the add-on in Stremio via:
Community Add-ons → Add custom add-onhttp://localhost:7000/manifest.jsonIf you want, I can help make a more production-ready version or integrate with other subtitle sources as well.
Not yet included in a published release.