Created
August 15, 2012 14:41
-
-
Save bobobo1618/3360678 to your computer and use it in GitHub Desktop.
Tumblr Downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require 'fs' | |
http = require 'http' | |
xml2js = require 'xml2js' | |
user = process.argv[process.argv.length-1] | |
processdata = (rawData, action)-> | |
if rawData.posts? and rawData.posts.post? | |
posts = rawData.posts | |
postMeta = posts['@'] | |
postList = posts.post | |
if postList? | |
action post for post in postList | |
else | |
console.log 'No posts.' | |
else | |
console.log 'Invalid rawData.' | |
console.dir rawData | |
processStuff = (user, offset, action, endaction)-> | |
getOpts = { | |
host: user+'.tumblr.com', | |
port: 80, | |
path: '/api/read?num=50start='+offset.toString() | |
} | |
http.get getOpts, (httpRes)-> | |
xmlParser = new xml2js.Parser | |
resData = '' | |
httpRes.on 'data', (chunk)-> | |
resData += chunk | |
httpRes.on 'end', ()-> | |
xmlParser.parseString resData, (err, xmlRes)-> | |
if xmlRes and xmlRes.posts? | |
totalPosts = xmlRes.posts['@'].total | |
processdata xmlRes, action | |
if offset+50 < totalPosts | |
processStuff user, offset+50, action, endaction | |
else | |
endaction() | |
else | |
if err | |
console.log 'Error' | |
console.dir err | |
else | |
console.log 'Invalid data.' | |
console.dir getOpts | |
console.dir xmlRes | |
httpRes.on 'close', ()-> | |
console.log 'Connection closed. Fail.' | |
ws = fs.createWriteStream user+'.json', {flags: 'w', encoding: 'utf8', mode: 0660} | |
ws.on 'open', (fd)-> | |
mydata = [] | |
postaction = (post)-> | |
mydata.push post | |
endaction = ()-> | |
ws.write JSON.stringify mydata | |
ws.close | |
console.log 'Done.' | |
processStuff user, 0, postaction, endaction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment