Advertisement
Guest User

Untitled

a guest
May 9th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Script for downloading all of the music off of coda's blog
  3.  * at: https://coda.s3m.us/category/ohc/page/137/
  4.  */
  5.  
  6. var request = require("request");
  7. var jsdom = require("jsdom");
  8.  
  9. function getPage(page, cb) {
  10.     var postData = {
  11.       action: "infinite_scroll",
  12.       page: page,
  13.       currentday: "07.08.06",
  14.       order: "DESC",
  15.       "query_args[paged]": 2,
  16.       "query_args[category_name]": "ohc",
  17.       "query_args[cat]": 10229527,
  18.       "query_args[nopaging]": false,
  19.       "query_args[no_found_rows]": false,
  20.       "query_args[comments_per_page]": 0,
  21.       "query_args[order]": "DESC"
  22.     };
  23.  
  24.     var options = {
  25.         url: 'https://coda.s3m.us/?infinity=scrolling',
  26.         method: 'POST',
  27.         // auth: {
  28.         //   user: 'admin',
  29.         //   pass: 'password',
  30.         // },
  31.         form: postData
  32.     };
  33.     var r = request(options, function(err, res, body) {
  34.        if (err) {
  35.           console.dir(err)
  36.           return
  37.        }
  38.        // console.dir('status code', res.statusCode)
  39.        // console.log(JSON.parse(body).postflair);
  40.  
  41.        cb(JSON.parse(body));
  42.        // https://codatrigger.files.wordpress.com/2015/08/eggshoes.mp3
  43.        // https://codatrigger.files.wordpress.com/2009/02/untripped.mp3
  44.        // and the generated ones look like:
  45.        // https://codatrigger.files.wordpress.com/2016/02/meca.mp3   <--- good
  46.        // https://codatrigger.files.wordpress.com/2006/08/so-so-damn-tired.mp3   <-- bad?
  47.     });
  48. }
  49.  
  50. function processUrl(index, listOfUrls, data, doneCb) {
  51.     // make sure we actually have a url, if not, just harmlessly passthrough to the next step
  52.     if (!listOfUrls[index])
  53.         return doneCb(data);
  54.  
  55.     // otherwise process the url async
  56.     jsdom.env(
  57.         listOfUrls[index],
  58.         ["http://code.jquery.com/jquery.js"],
  59.         function(err, window) {
  60.             var realUrl = "null";
  61.             Array.prototype.forEach.call(
  62.                 window.document.getElementsByTagName("a"),
  63.                 function(b) {
  64.                     if (b.href.indexOf(".mp3") > -1)
  65.                         realUrl = b.href;
  66.                 });
  67.  
  68.             console.log("found realUrl: " + realUrl);
  69.             data.push(realUrl);
  70.             if (index < listOfUrls.length-1) {
  71.                 processUrl(index+1, listOfUrls, data, doneCb);
  72.             }
  73.             else {
  74.                 doneCb(data);
  75.             }
  76.         });
  77. }
  78.  
  79. function tick(page, data) {
  80.     console.log("Requesting page " + page + "....");
  81.     getPage(page, function(body) {
  82.         var searchUrls = [];
  83.         for (var url in body.postflair) {
  84.             searchUrls.push(url);
  85.         }
  86.  
  87.         // process each of those urls, searching for the mp3 download URL, then finally
  88.         // continue to the next page
  89.         processUrl(0, searchUrls, data, function(data) {
  90.             if (page > 0) {
  91.                 tick(page-1, data);
  92.             }
  93.             else {
  94.                 console.log("Data: ");
  95.                 console.log(data);
  96.  
  97.                 console.log("dumping URLs to file...");
  98.                 var fs = require('fs');
  99.                 var stream_mp3s = fs.createWriteStream("all_urls.txt");
  100.                 var raw_out = fs.createWriteStream("raw_out.txt");
  101.                 stream_mp3s.once("open", function(fd) {
  102.                     for (var i = 0; i < data.length; i++) {
  103.                         stream_mp3s.write(
  104.                             (data[i]
  105.                                 .replace(/coda\.s3m\.us/gi, "codatrigger.files.wordpress.com") // convert domain
  106.                                 .replace(/([0-9]{4}\/[0-9]{2}\/)([0-9]{2}\/)/, "\$1") // remove day from URL
  107.                                 .replace(/\/$/,"") // remove trailing /
  108.                                 + ".mp3") // add mp3
  109.                             + "\n");
  110.                         raw_out.write(data[i] + "\n");
  111.                     }
  112.                     stream_mp3s.end();
  113.                     raw_out.end();
  114.                 });
  115.                 console.log("truly done.");
  116.             }
  117.         });
  118.     });
  119. }
  120.  
  121. // tick(136, []);
  122. tick(140, []);
  123.  
  124. console.log("done.");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement