Permalink
Browse files

initial commit of personal twitter archive client and server

  • Loading branch information...
0 parents commit 61043c370d25af573e352ea06a846805bcdd7440 @ewanleith committed Apr 27, 2011
Showing with 101 additions and 0 deletions.
  1. +45 −0 client/pta.js
  2. +12 −0 client/views/index.jade
  3. +5 −0 client/views/layout.jade
  4. +39 −0 server/pta_server.js
45 client/pta.js
@@ -0,0 +1,45 @@
+var app = require('express').createServer();
+var mongodb = require('mongodb');
+var jade = require('jade');
+
+function getTweets(res)
+{
+ var server = new mongodb.Server("127.0.0.1", 27017, {});
+ new mongodb.Db('twitterarchive', server, {}).open(function (error, client) {
+ if (error) throw error;
+ var collection = new mongodb.Collection(client, 'archive');
+ collection.find({},{'from_user':1,'created_at':1,'text':1},{sort:[['created_at','desc']]}, function(err,cursor) {
+ //collection.find({},{sort:[['created_at','desc']]}, function(err,cursor) {
+ cursor.toArray(function(err, items) {
+ //console.log(items[0]);
+ //console.log(items[1]);
+ buildPage(res,items);
+ })
+ })
+})
+
+}
+
+function buildPage(res,items)
+{
+ var tweetstring = ''
+ for (var t in items)
+ {
+ tweetstring = tweetstring + ' '+ items[t]['created_at']+' '+items[t]['from_user']+' '+items[t]['text']+'<br>\n';
+ }
+ res.render('index.jade', {tweets:items, pageTitle:'Your Personal Twitter Archive'});
+ //console.log(items[0]); // Show top entry
+}
+
+app.set ('view engine', 'jade');
+
+
+app.get('/', function(req, res){
+ getTweets(res);
+});
+
+app.get('*', function(req, res){
+ res.send('Page not found, should be a 404 here');
+});
+
+app.listen(3000);
12 client/views/index.jade
@@ -0,0 +1,12 @@
+h1= pageTitle
+p Welcome to #{pageTitle}
+
+- if (tweets.length)
+ - each val,key in tweets
+ ul
+ li Sent at: #{val.created_at}
+ li From user: #{val.from_user}
+ li Text: #{val.text}
+
+p
+a(href='http://www.ewanleith.com') Created by Ewan Leith
5 client/views/layout.jade
@@ -0,0 +1,5 @@
+!!!
+html
+ head
+ title= pageTitle
+ body!= body
39 server/pta_server.js
@@ -0,0 +1,39 @@
+// Personal Twitter Archive Gathered
+// Created by Ewan Leith
+
+var sys = require('sys'),
+ async = require('async'),
+ rest = require('restler');
+
+var mongodb = require('mongodb');
+
+var username="ewantoo";
+
+function printTweet(tweetdata) {
+ var server = new mongodb.Server("127.0.0.1", 27017, {});
+ new mongodb.Db('twitterarchive', server, {}).open(function (error, client) {
+ if (error) throw error;
+ var collection = new mongodb.Collection(client, 'archive');
+ collection.update([tweetdata][0],[tweetdata][0],{upsert:true}, function(err) {
+ if (err) console.warn(err.message);
+ });
+ //console.log([tweetdata][0]); //Output logged data to console
+});
+
+
+}
+
+
+function launchCollect() {
+ rest.get('http://search.twitter.com/search.json?q='+username).on('success', function(data) {
+ async.forEach(data['results'], printTweet, function(err) {
+ });
+ });
+ rest.get('http://search.twitter.com/search.json?q=+from%3A'+username).on('success', function(data) {
+ async.forEach(data['results'], printTweet, function(err) {
+ });
+ });
+ setTimeout("launchCollect()", 60000);
+}
+
+setTimeout(launchCollect, 100);

0 comments on commit 61043c3

Please sign in to comment.