server.js (3235B)
1 var sys = require('sys'), 2 fs = require('fs'), 3 http = require('http'), 4 path = require('path'), 5 urlParse = require('url').parse, 6 paperboy = require('paperboy'), 7 xml2js = require('xml2js'), 8 webroot = path.join(path.dirname(__filename), 'static'), 9 router = require('./router'), 10 tumblr = require('./tumblr'); 11 12 router.server.listen(2666); 13 14 paperboy.filepath = function (webroot, urlString) { 15 var url = urlParse(urlString), 16 file = url.pathname.split('/').pop(), 17 fp = path.normalize(path.join(webroot, file)); 18 return([null, fp]); 19 }; 20 21 var getPage = function (email, password, offset, data, end) { 22 var path = [ 23 '/api/likes?email=', 24 email, 25 '&password=', 26 password, 27 '&start=', 28 offset, 29 '&num=', 30 50 31 ].join(''); 32 console.log(email); 33 var client = http.createClient(80, 'www.tumblr.com'); 34 var crequest = client.request('GET', path, {'host': 'www.tumblr.com'}); 35 crequest.on('response', function (cresponse) { 36 if (cresponse.statusCode != 200) { return end(); } 37 var parser = new xml2js.Parser(); 38 var body = ''; 39 cresponse.setEncoding('utf8'); 40 cresponse.on('data', function (chunk) { body += chunk; }); 41 cresponse.on('end', function (chunk) { 42 if (chunk) { body += chunk; } 43 parser.parseString(body); 44 }); 45 parser.addListener('end', function(result) { 46 var posts = (result.posts.post.length == undefined) ? [result.posts.post] : result.posts.post; 47 data(posts); 48 console.log(posts.length); 49 if (posts.length < 50) { return end(); } 50 getPage(email, password, offset + 50, data, end); 51 }); 52 }); 53 crequest.end(); 54 }; 55 56 /** 57 * subroutines 58 **/ 59 60 var sendStatic = function (request, response) { 61 paperboy.deliver(webroot, request, response); 62 }; 63 64 /** 65 * static assets 66 **/ 67 68 router.get('thomblr/index.html', sendStatic); 69 router.get('thomblr/fav.png', sendStatic); 70 71 /** 72 * the one and only interesting endpoint 73 **/ 74 router.get('thomblr/bookmarks.html', function (request, response) { 75 var url = urlParse(request.url, true); 76 if (!url.query) { 77 response.writeHead(400); 78 response.end('Bad Request'); 79 return; 80 } 81 var email = encodeURIComponent(url.query.email), 82 password = encodeURIComponent(url.query.password); 83 if (!email || !password) { 84 response.writeHead(400); 85 response.end('Bad Request'); 86 return; 87 } 88 response.writeHead(200); 89 var header = [ 90 '<!DOCTYPE NETSCAPE-Bookmark-file-1>', 91 '<!-- This is an automatically generated file.', 92 ' It will be read and overwritten.', 93 ' DO NOT EDIT! -->', 94 '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">', 95 '<TITLE>Bookmarks</TITLE>', 96 '<H1>Bookmarks</H1>', 97 '<DL><p>', 98 ]; 99 response.write(header.join('\n') + '\n'); 100 getPage(email, password, 0, 101 function (posts) { 102 posts.forEach(function (post) { 103 var link = tumblr.serialize(post); 104 var html = [ 105 '<DT><A HREF="', 106 link.url.replace('"', '\\"'), 107 '">', 108 link.title.replace('<', '<').replace('>', '>'), 109 '</A>\n' 110 ].join(''); 111 response.write(html); 112 }); 113 }, 114 function () { 115 response.end('</DL><p>\n'); 116 } 117 ); 118 });