thomblr

convrt yr tumblr <3 to bookmrks
git clone https://wehaveforgeathome.hates.computer/thomblr.git
Log | Files | Refs | LICENSE

commit 9d539227792de57a06cceebcb625a6e390af5782
parent 17434ad1b4978f99424beba4d08b4689e809e904
Author: Ryan Wolf <rwolf@borderstylo.com>
Date:   Fri, 25 Mar 2011 00:23:15 +0000

pagination ftw

Diffstat:
Mserver.js | 91++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 53 insertions(+), 38 deletions(-)

diff --git a/server.js b/server.js @@ -18,6 +18,39 @@ paperboy.filepath = function (webroot, urlString) { return([null, fp]); }; +var getPage = function (email, password, offset, size, data, end) { + var path = [ + '/api/likes?email=', + email, + '&password=', + password, + '&size=', + size, + '&start=', + offset + ].join(''); + var client = http.createClient(80, 'www.tumblr.com'); + var crequest = client.request('GET', path, {'host': 'www.tumblr.com'}); + crequest.on('response', function (cresponse) { + if (cresponse.statusCode != 200) { return end(); } + var parser = new xml2js.Parser(); + var body = ''; + cresponse.setEncoding('utf8'); + cresponse.on('data', function (chunk) { body += chunk; }); + cresponse.on('end', function (chunk) { + if (chunk) { body += chunk; } + parser.parseString(body); + }); + parser.addListener('end', function(result) { + var posts = (result.posts.post.length == undefined) ? [result.posts.post] : result.posts.post; + data(posts); + if (posts.length < size) { return end(); } + getPage(email, password, offset + size, size, data, end); + }); + }); + crequest.end(); +}; + /** * subroutines **/ @@ -34,7 +67,7 @@ router.get('thomblr/index.html', sendStatic); router.get('thomblr/fav.png', sendStatic); /** - * the one and only interesting call + * the one and only interesting endpoint **/ router.get('thomblr/bookmarks.html', function (request, response) { var url = urlParse(request.url, true); @@ -50,40 +83,21 @@ router.get('thomblr/bookmarks.html', function (request, response) { response.end('Bad Request'); return; } - var path = '/api/likes?email=' + email + '&password=' + password; - var client = http.createClient(80, 'www.tumblr.com'); - var crequest = client.request('GET', path, {'host': 'www.tumblr.com'}); - crequest.on('response', function (cresponse) { - if (cresponse.statusCode != 200) { - response.writeHead(400); - response.end('Bad Request'); - return; - } - var parser = new xml2js.Parser(); - var body = ''; - cresponse.setEncoding('utf8'); - cresponse.on('data', function (chunk) { body += chunk; }); - cresponse.on('end', function (chunk) { - if (chunk) { body += chunk; } - parser.parseString(body); - }); - parser.addListener('end', function(result) { - response.writeHead(200); - - - var header = [ - '<!DOCTYPE NETSCAPE-Bookmark-file-1>', - '<!-- This is an automatically generated file.', - ' It will be read and overwritten.', - ' DO NOT EDIT! -->', - '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">', - '<TITLE>Bookmarks</TITLE>', - '<H1>Bookmarks</H1>', - '<DL><p>', - ]; - response.write(header.join('\n') + '\n'); - if (!result.posts.post) { return response.end(''); } - result.posts.post.forEach(function (post) { + response.writeHead(200); + var header = [ + '<!DOCTYPE NETSCAPE-Bookmark-file-1>', + '<!-- This is an automatically generated file.', + ' It will be read and overwritten.', + ' DO NOT EDIT! -->', + '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">', + '<TITLE>Bookmarks</TITLE>', + '<H1>Bookmarks</H1>', + '<DL><p>', + ]; + response.write(header.join('\n') + '\n'); + getPage(email, password, 0, 50, + function (posts) { + posts.forEach(function (post) { var link = tumblr.serialize(post); var html = [ '<DT><A HREF="', @@ -94,8 +108,9 @@ router.get('thomblr/bookmarks.html', function (request, response) { ].join(''); response.write(html); }); + }, + function () { response.end('</DL><p>\n'); - }); - }); - crequest.end(); + } + ); });