Advertisement
stegner

HBO Max downloader

Oct 12th, 2021 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const path = require('path');
  3. const https = require('https');
  4. const parser = require('fast-xml-parser');
  5.  
  6. let file;
  7. let baseURL;
  8. const langs = {};
  9.  
  10. const options = {
  11.   ignoreAttributes : false,
  12.   parseNodeValue : true,
  13.   parseAttributeValue : true,
  14. };
  15.  
  16. if(process.argv[2]){
  17.   loadFile(process.argv[2]);
  18. }
  19. else {
  20.   error(`Usage: node ${path.basename(__filename)} [MDP url]`);
  21. }
  22.  
  23. function error(message){
  24.   console.log("\x1b[31m", message, "\x1b[0m");
  25.   process.exit();
  26. }
  27.  
  28. function loadFile(url) {
  29.   baseURL = url.substr(0, url.lastIndexOf("/")+1);
  30.  
  31.   https.get(url, function (res) {
  32.     var data = '';
  33.     res.on('data', function (chunk) {
  34.       data += chunk;
  35.     });
  36.     res.on('end', function () {
  37.       readXML(data);
  38.     });
  39.   }).on('error', function (err) {
  40.     error(err.message);
  41.   });
  42. }
  43.  
  44. function readXML(xml){
  45.   const data = parser.parse(xml, options);
  46.   if(data.MPD?.Period?.AdaptationSet){
  47.     data.MPD.Period.AdaptationSet.forEach((adaptation) => {
  48.       if(adaptation["@_contentType"]=="text"){
  49.         langs[adaptation["@_lang"]]=adaptation.Representation.SegmentTemplate["@_media"];
  50.       }
  51.     });
  52.     selectLang();
  53.   }
  54.   else {
  55.     error("Invalid XML");
  56.   }
  57. }
  58.  
  59. function selectLang(){
  60.   console.log("Availaible languages:");
  61.   for (const lang in langs) {
  62.     console.log(lang);
  63.   }
  64.  
  65.   const readline = require('readline').createInterface({
  66.     input: process.stdin,
  67.     output: process.stdout
  68.   });
  69.  
  70.   readline.question('Select language: ', lang => {
  71.     downloadLanguage(lang);
  72.     readline.close();
  73.   });
  74. }
  75.  
  76. function downloadLanguage(lang) {
  77.   console.log(`Donwloading ${lang}...`);
  78.   file = fs.createWriteStream(`${lang}.vtt`, { flags: 'a' });
  79.   download(lang);
  80. }
  81.  
  82. function download(lang, count=1) {
  83.   const url = baseURL+langs[lang].replace("$Number$",count);
  84.  
  85.   https.get(url, function(res) {
  86.     if(res.statusCode==200){
  87.       res.on('data', (chunk) => {
  88.         file.write(chunk);
  89.       });
  90.       res.on('end', () => {
  91.         download(lang, count+1);
  92.       });
  93.     }
  94.     else {
  95.       console.log("Done!");
  96.       file.end();
  97.       selectLang();
  98.     }
  99.   }).on('error', function(err) {
  100.     file.end();
  101.     error(err.message);
  102.   });
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement