Skip to content
This repository has been archived by the owner on Sep 18, 2018. It is now read-only.

Commit

Permalink
adding html view
Browse files Browse the repository at this point in the history
  • Loading branch information
hay committed Jul 18, 2011
1 parent 433f018 commit 3f0a30b
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
.DS_Store
tweets.html
node_modules
tweets.json
128 changes: 81 additions & 47 deletions backuptweets.js
@@ -1,58 +1,87 @@
var request = require('request'),
var fs = require('fs'),
request = require('request'),
async = require('async'),
mustache = require('mustache'),
_ = require('underscore');

const API_COUNT_MAX = 20;

module.exports = function(args, cb) {
var count = 0,
pages = Math.floor(args.max / API_COUNT_MAX),
tweets = [];

function getUrl(page) {
var url = ''.concat(
'http://api.twitter.com/1/statuses/user_timeline.json',
'?screen_name=' + args.screen_name,
'&page=' + page,
'&count=' + API_COUNT_MAX,
'&trim_user=' + true,
'&include_rts=1'
);

return url;
}
var args = {
// Default options, will be overwritten in init with defaultArgs
"max" : 3200,
"debug" : false
};

function log() {
if (args.debug) {
console.log.apply(this, arguments);
}
function formatResults(results, cb) {
htmlFormatResults(results, function(html) {
cb({
"html" : html,
"json" : JSON.stringify(results),
"js" : results
});
})
}

function getUrl(page) {
var url = ''.concat(
'http://api.twitter.com/1/statuses/user_timeline.json',
'?screen_name=' + args.user,
'&page=' + page,
'&count=' + API_COUNT_MAX,
'&trim_user=' + true,
'&include_rts=1'
);

return url;
}

function htmlFormatResults(results, cb) {
var view = {
"user" : args.user,
"tweets" : results
};

fs.readFile("htmlview.html", "utf-8", function(err, data) {
var html = mustache.to_html(data, view);
cb(html);
});
}

function log() {
if (args.debug) {
console.log.apply(this, arguments);
}
}

function init(initArgs, cb) {
args = _.extend(args, initArgs);

var requests = [],
pages = Math.floor(args.max / API_COUNT_MAX);

// Create a series of callbacks for the requests
var requests = [];
for (var page = 1; page < pages + 1; page++) {
(function(page) {
requests.push(function(callback) {
var url = getUrl(page);

log("Fetching", url);
log("Page", page);

request(
{
url : url
},
function(error, response, body) {
log("Got a response");

if (error) {
callback(error);
} else {
callback(null, JSON.parse(body));
requests.push(function(callback) {
var url = getUrl(page);

log("Fetching", url);
log("Page", page);

request(
{
url : url
},
function(error, response, body) {
log("Got a response");

if (error) {
callback(error);
} else {
callback(null, JSON.parse(body));
}
}
}
)
});
)
});
})(page);
}

Expand All @@ -62,8 +91,13 @@ module.exports = function(args, cb) {
log("An error occured", err);
cb(false);
} else {
// Mash it all together
cb(_.union(results));
// Mash all queries together
results = _.union(results);
formatResults(results, function(finalResults) {
cb(finalResults);
});
}
});
}
}

module.exports = init;
13 changes: 8 additions & 5 deletions example.js
Expand Up @@ -2,15 +2,18 @@ var backuptweets = require('./backuptweets.js'),
fs = require('fs');

backuptweets({
"screen_name" : "huskyr",
"user" : "huskyr",
"max" : 40,
"debug" : true
}, function(tweets) {
if (tweets) {
console.log(tweets);
fs.writeFile("tweets.json", JSON.stringify(tweets), function() {
console.log('ready');
})
fs.writeFile("tweets.json", tweets.json, function() {
console.log('json file ready');
});

fs.writeFile("tweets.html", tweets.html, function() {
console.log('html file ready');
});
} else {
console.log("error");
}
Expand Down
98 changes: 98 additions & 0 deletions htmlview.html
@@ -0,0 +1,98 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>{{user}}'s tweets</title>
<style>
* {
margin: 0;
padding: 0;
}

body {
font: 12px/15px Helvetica, Arial, sans-serif;
color: #555;
background: #f5f5f5;
margin: 20px auto;
}

table {
overflow:hidden;
border:1px solid #d3d3d3;
background:#fefefe;
width:70%;
margin:5% auto 0;
-moz-border-radius:5px; /* FF1+ */
-webkit-border-radius:5px; /* Saf3-4 */
border-radius:5px;
-moz-box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
-webkit-box-shadow: 0 0 4px rgba(0, 0, 0, 0.2);
}

th, td {padding:18px 28px 18px; text-align:center; }

th {padding-top:22px; text-shadow: 1px 1px 1px #fff; background:#e8eaeb;}

td {border-top:1px solid #e0e0e0; border-right:1px solid #e0e0e0;}

tr:nth-child(odd) td {background:#f6f6f6;}

td.first, th.first {text-align:left}

td.last {border-right:none;}

/*
Background gradients are completely unnessary but a neat effect.
*/

td {
background: -moz-linear-gradient(100% 25% 90deg, #fefefe, #f9f9f9);
background: -webkit-gradient(linear, 0% 0%, 0% 25%, from(#f9f9f9), to(#fefefe));
}

tr.odd-row td {
background: -moz-linear-gradient(100% 25% 90deg, #f6f6f6, #f1f1f1);
background: -webkit-gradient(linear, 0% 0%, 0% 25%, from(#f1f1f1), to(#f6f6f6));
}

th {
background: -moz-linear-gradient(100% 20% 90deg, #e8eaeb, #ededed);
background: -webkit-gradient(linear, 0% 0%, 0% 20%, from(#ededed), to(#e8eaeb));
}
</style>
</head>
<body>
<center>
<h1>{{user}}'s' tweets</h1>
</center>

<table>
<thead>
<tr>
<td>Date</td>
<td>Text</td>
</tr>
</thead>
<tfoot>
<tr>
<td>Date</td>
<td>Text</td>
</tr>
</tfoot>
<tbody>
{{#tweets}}
<tr>
<td>{{created_at}}</td>
<td>{{text}}</td>
</tr>
{{/tweets}}
</tbody>
</table>

<center>
<p>
Generated by <a href="http://haykranen.nl">Hay's</a> <a href="http://www.github.com/hay/backuptweets/">backuptweets</a>.
</p>
</center>
</body>
</html>

0 comments on commit 3f0a30b

Please sign in to comment.