UNPKG

@subtitles/providers

Version:

Providers are the core of applications, where the subtitles are collected. Each provider exports a unique strategy for gathering data. From legendastv's web scraping from opensubtitle API usage, you can collect subtitles from your favorite tv shows and mo

61 lines (60 loc) 2.22 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const http_1 = require("http"); const cookie_1 = __importDefault(require("cookie")); const form_urlencoded_1 = __importDefault(require("form-urlencoded")); const flat_cache_1 = __importDefault(require("flat-cache")); const CACHE_ID = 'legendas.tv-auth'; const CACHE_PATH = '/tmp/subtitle-finder/cache'; async function authenticate(url, username, password) { const cache = flat_cache_1.default.load(CACHE_ID, CACHE_PATH); const cookies = cache.getKey('cookies'); if (cookies) return cookies; return new Promise((resolve, reject) => { const req = (0, http_1.request)({ method: 'POST', protocol: 'http:', host: url.host, path: '/login', port: '80', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, }, async (res) => { const cookies = res.headers['set-cookie']?.map(raw => { const parsedCookie = cookie_1.default.parse(raw); const { url, domain, path, expires, httpOnly, secure, sameSite, ...session } = parsedCookie; const name = Object.getOwnPropertyNames(session)[0]; const value = session[name]; return { name, value, url, secure, domain, path, expires, httpOnly, sameSite, }; }); if (!cookies) return reject(); cache.setKey('cookies', cookies); resolve(cookies); }); const body = { _method: 'POST', 'data[User][username]': username, 'data[User][password]': password, }; const postData = (0, form_urlencoded_1.default)(body); req.on('error', reject); req.end(postData); }); } exports.default = authenticate;