Skip to content

Instantly share code, notes, and snippets.

@Kurobyte
Last active April 14, 2016 14:06
Show Gist options
  • Select an option

  • Save Kurobyte/4178b80217f25a8cccc5da439f04ec53 to your computer and use it in GitHub Desktop.

Select an option

Save Kurobyte/4178b80217f25a8cccc5da439f04ec53 to your computer and use it in GitHub Desktop.
Downloads videos from some tumblr blog using Node.

usage:

node index.js <some tumblr blog url>
/*
* Tumblr Video Downloader - v0.1b - by Kurobyte Ivan M.
*/
var http = require("http");
var jsdom = require("jsdom");
var fs = require("fs");
var pages = 0;
var inpURL = process.argv[2];
if (inpURL.lastIndexOf('/') != inpURL.length - 1)
inpURL += '/';
getHtml(inpURL, getNPages);
function getHtml(url, callback) {
http.get(url, (res) => {
var data = "";
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
callback(url, data)
})
res.resume();
}).on('error', (e) => {
console.log(`Got error: ${e.message}`);
});
}
function getNPages(url, data) {
jsdom.env(
data,
["http://code.jquery.com/jquery.js"],
function (err, window) {
pages = parseInt(window.$(".pagination").find("a:not([title])").last().text());
console.log("Nº of pages:", pages);
for(var i = 1; i <= pages; i++) {
getHtml(url+'page/'+i, getVideos);
}
}
);
}
function getVideos(url, data) {
jsdom.env(
data,
["http://code.jquery.com/jquery.js"],
function (err, window) {
var ifrm = window.$("iframe");
var vids = [];
for(var i = 0; i < ifrm.length; i++) {
if (ifrm[i].src.indexOf("/video/") > -1) {
vids.push(ifrm[i].src.replace("https:","http:"));
}
}
for(var i = 0; i < vids.length; i++) {
getHtml(vids[i], getSourceVideo);
}
}
);
}
function getSourceVideo(url, data) {
jsdom.env(
data,
["http://code.jquery.com/jquery.js"],
function (err, window) {
var src = window.$("source");
for(var i = 0; i < src.length; i++) {
var filename = src[i].src.split('/');
downloadVideo("http://vt.tumblr.com/"+filename[5]+"_"+filename[6]+".mp4", filename[5]+".mp4");
}
}
);
}
function downloadVideo(url, filename) {
var file = fs.createWriteStream(filename);
var request = http.get(url, function(response) {
response.pipe(file);
});
}
{
"name": "tmblr",
"version": "0.1.0",
"description": "Tumblr Video Downloader",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Kurobyte (Ivan M.)",
"license": "ISC",
"dependencies": {
"jsdom": "^8.3.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment