Permalink
Browse files

Added a new plugin: user tweets

  • Loading branch information...
1 parent c2e856d commit d902b4c4e9c7b9461138b505bbd5baf7273c2267 @pedrofranceschi committed Jan 2, 2011
Showing with 110 additions and 0 deletions.
  1. +70 −0 plugins/user_tweets/plugin.js
  2. +40 −0 plugins/user_tweets/view.html
@@ -0,0 +1,70 @@
+var sys = require('sys');
+var http = require('http');
+var pluginHelper = require("../../lib/helper");
+
+var cacheCreation;
+var lastData;
+var lastUsername;
+
+exports.initialize = function () {
+ return {
+ pluginInfos: {
+ "call_name": "user_tweets",
+ "name": "User Tweets",
+ "description": "A plugin to show the last tweets from someone",
+ "creator_name": "Pedro Franceschi",
+ "creator_email": "pedrohfranceschi@gmail.com",
+ "version": "0.1"
+ },
+ configVariables: [
+ {"access_name": "username", "name": "Twitter Username", "description": "The username to be searched", "field_type": "text"},
+ ],
+ run: function(req, res, callback) {
+ var self = this;
+ pluginHelper.getConfigVariableValue(self, 'username', function(username){
+ if(username == undefined) {
+ callback("");
+ }
+ getLastUserTweets(username, function(data){
+ pluginHelper.renderView(self, 'view.html', { 'username': username, 'data': data }, function(final_content){
+ callback(final_content);
+ });
+ });
+
+ function getLastUserTweets(username, callback) {
+ var d = new Date();
+ if(cacheCreation == undefined) {
+ cacheCreation = d.getTime();
+ }
+
+ var difference = Math.floor((d.getTime() - cacheCreation)/1000);
+ if(difference <= 60 && lastData != undefined && lastUsername == username) {
+ callback(lastData);
+ } else {
+ lastUsername = username;
+ cacheCreation = d.getTime();
+
+ var responseData = "";
+ var google = http.createClient(80, 'api.twitter.com');
+ var request = google.request('GET', '/1/statuses/user_timeline.json?screen_name=' + escape(username) + '&count=3',
+ {'host': 'api.twitter.com', 'USER_AGENT': 'MyUserAgent'});
+ request.end();
+ request.on('response', function (response) {
+ response.setEncoding('utf8');
+ response.on('data', function (chunk) {
+ responseData += chunk;
+ });
+ response.on('end', function () {
+ var parser = JSON.parse(unescape(responseData));
+ lastData = parser;
+
+ callback(lastData);
+ });
+ });
+ }
+ }
+ });
+
+ }
+ }
+}
@@ -0,0 +1,40 @@
+<%
+
+ function relative_time(time_value) {
+ var values = time_value.split(" ");
+ time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
+ var parsed_date = Date.parse(time_value);
+ var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
+ var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
+ delta = delta + (relative_to.getTimezoneOffset() * 60);
+
+ if (delta < 60) {
+ return 'less than a minute ago';
+ } else if(delta < 120) {
+ return 'about a minute ago';
+ } else if(delta < (60*60)) {
+ return (parseInt(delta / 60)).toString() + ' minutes ago';
+ } else if(delta < (120*60)) {
+ return 'about an hour ago';
+ } else if(delta < (24*60*60)) {
+ return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
+ } else if(delta < (48*60*60)) {
+ return '1 day ago';
+ } else {
+ return (parseInt(delta / 86400)).toString() + ' days ago';
+ }
+ }
+
+%>
+
+<h3>Twitter</h3>
+
+<% if(data != undefined) { %>
+<% for(var i=0; i < data.length; i++) { %>
+ <!-- <li><span class="content"><%= data[i]["text"] %><br/><br/> -->
+ <span class="content"><%= data[i]["text"] %></span> <a style="font-size:85%" class="time" href="http://twitter.com/<%= username %>/statuses/<%= data[i].id %>"><%= relative_time (data[i].created_at) %></a>
+ <br/><br/>
+<% } %>
+<ol/>
+<i>Follow <a href="https://twitter.com/<%= username %>"><%= username %></a> on Twitter!</i>
+<% } %>

0 comments on commit d902b4c

Please sign in to comment.