From a12ae9248aac7a40ce6adf4498b6186cf527bfa3 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Fri, 14 Aug 2020 10:26:56 +0200 Subject: [PATCH] Turn twist-dl into a nodejs script and make it fully work --- twist-dl.node | 192 ++++++++++++++++++++++++++++++++++++++++++++++++++ twist-dl.sh | 13 ---- 2 files changed, 192 insertions(+), 13 deletions(-) create mode 100755 twist-dl.node delete mode 100755 twist-dl.sh diff --git a/twist-dl.node b/twist-dl.node new file mode 100755 index 0000000..d275c42 --- /dev/null +++ b/twist-dl.node @@ -0,0 +1,192 @@ +#!/bin/node + +const https = require('https'); +const fs = require('fs'); +const zlib = require('zlib'); +const crypto = require('crypto'); +const CryptoJS = require('crypto-js'); + +const url = process.argv[2]; +const episodes = process.argv.slice(3); + +const animeMatches = url.match(/\/a\/([^/]+)(\/([0-9]+))?/i); +const anime = animeMatches[1]; +const episode = Number(animeMatches[3]); +if (episodes.length === 0) episodes.push(episode); + +console.log('Anime:', anime, 'episodes:', episodes); + +function handleResponse(response, callback) { + const enc = response.headers['content-encoding']; + let d = ''; + response.on('data', data => d += enc === 'gzip' ? zlib.unzipSync(data) : data); + response.on('end', () => { + callback(d); + }); +} + +function getCookie(then) { + https.request(url, { + headers: { + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0", + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Language": "en-US,en;q=0.5", + "Accept-Encoding": "gzip, deflate, br", + "Connection": "keep-alive", + "Upgrade-Insecure-Requests": "1", + "Pragma": "no-cache", + "Cache-Control": "no-cache" + } + }, response => { + handleResponse(response, data => { + const script = data.match(/