Skip to content

Instantly share code, notes, and snippets.

@sidmitra
Created August 9, 2015 05:49
Show Gist options
  • Save sidmitra/34ab870459f051d37d6d to your computer and use it in GitHub Desktop.
Save sidmitra/34ab870459f051d37d6d to your computer and use it in GitHub Desktop.
Webtask.io - Twitter Daily Activity Chart
module.exports =
function (context, req, res) {
var Twit = require('twit');
var ejs = require('ejs');
var T = new Twit({
consumer_key: context.data.consumer_key,
consumer_secret: context.data.consumer_secret,
access_token: context.data.access_token,
access_token_secret: context.data.access_token_secret
})
twitOptions = {
screen_name: context.data.username,
trim_user: true,
count: context.data.count || 200,
//include_rts: false,
contributor_details: false,
//exclude_replies: true
}
T.get('statuses/user_timeline', twitOptions, function(err, data, response) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(ejs.render(view.stringify(), {
username: context.data.username,
data: data,
}));
})
}
function view() {/*
<html>
<head>
<title>Twitter Daily Activity </title>
<link href="//cdn.rawgit.com/Kraigo/glanceyear/master/glanceyear.css" rel="stylesheet">
<script src="//code.jquery.com/jquery-2.0.3.min.js"></script>
<script src="//cdn.rawgit.com/Kraigo/glanceyear/master/jquery.glanceyear.js"></script>
<style>
#debug {color: #000; font-weight: bold;}
</style>
</head>
<body>
<h1>Twitter Daily Activity for <%= username %></h1>
<div class="glanceyear-container">
<h1 class="glanceyear-header">Glance Year
<span class="glanceyear-quantity"></span>
</h1>
<div class="glanceyear-content" id="js-glanceyear">
</div>
<div class="glanceyear-summary">
<div class="glanceyear-legend">
Less&nbsp;
<span style="background-color: #eee"></span>
<span style="background-color: #c3dbda"></span>
<span style="background-color: #5caeaa"></span>
<span style="background-color: #277672"></span>
&nbsp;More
</div>
Calendar with map data entered <br>
<span id="debug"></span>
</div>
</div>
<script>
$(function() {
var tweetData = <%- JSON.stringify(data) %>;
// custom date method
function get_yyyy_mm_dd(dt) {
var yyyy = dt.getFullYear().toString();
var mm = (dt.getMonth()+1).toString(); // getMonth() is zero-based
var dd = dt.getDate().toString();
return yyyy +"-"+ mm +"-"+ dd; // padding
};
// count activity per day
var tweets = [];
for(var tweet of tweetData){
var dt = new Date(tweet.created_at);
var yyyy_mm_dd = get_yyyy_mm_dd(dt);
if (tweets[yyyy_mm_dd]==undefined){
tweets[yyyy_mm_dd] = 1;
}
else{
tweets[yyyy_mm_dd] = tweets[yyyy_mm_dd] + 1;
}
}
var massive = [];
for(var key in tweets){
massive.push({
date: key,
value: tweets[key].toString()});
}
//console.log(massive)
$('#js-glanceyear').empty().glanceyear(
massive,
{
eventClick: function(e) {
$('#debug').html('Date: '+ e.date + ', Count: ' + e.count);
},
showToday: false
}
);
});
</script>
</body>
</html>
*/}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment