Created
March 13, 2010 12:08
-
-
Save evadne/331291 to your computer and use it in GitHub Desktop.
Punchy Live’s twitter interactivity engine that is going to be polished and extended into monoTwitterEngine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// plNews.js | |
/* | |
Delegate methods: | |
plNewsDidReceiveNewerTweets to delegate object | |
plNewsDidReceiveOlderTweets to delegate object | |
*/ | |
plScript.plNews = { | |
initWithConf: function(conf) { | |
// plScript.plNews.conf = $.extend({}, plScript.plNews.conf, conf); | |
plScript.plNews.init(); | |
}, | |
init: function () { | |
d("plNews initialized."); | |
d("plNews is setting up a timer to fire and call refreshStatus()."); | |
plScript.plNews.refreshStatusIntervalHolder = setInterval(plScript.plNews.refreshStatus, plScript.plNews.conf.refreshInterval); | |
}, | |
description: function() { | |
return $.extend({}, plScript.plNews.conf, plScript.plNews.memory); | |
}, | |
delegates: { | |
plNewsDidReceiveNewerTweets: {}, | |
plNewsDidReceiveOlderTweets: {} | |
}, | |
conf: { | |
'refreshInterval': 5000, | |
'observe': 'punchparty', | |
'historyStartsAfterID': 6733469736, | |
'historyEndsWithID': undefined /* Never ends */ | |
}, | |
memory: { | |
'knownLatestTweetID': undefined, | |
'knownEarliestTweetID': undefined, | |
'retrievedLatestTweetID': undefined, | |
'retrievedEarliestTweetID': undefined | |
}, | |
silo: { | |
}, | |
// '32535436': {} etc — Tweet ID as identifier | |
siloObjectTemplate: { | |
'tweetTimestamp': '', | |
'tweetContext': '' | |
/* ,'isImportant': false */ | |
}, | |
refreshStatusIntervalHolder: {}, | |
refreshstatusIntervalOngoing: false, | |
refreshStatus: function() { | |
$.getJSON( | |
plScript.plNews.twitterTunnelURIWithParameters("from:" + String(plScript.plNews.conf.observe) + "&rpp=1"), | |
plScript.plNews.didRefreshStatusWithData | |
); | |
// Polls Twitter Search to see the user’s latest tweet. | |
}, | |
didRefreshStatusWithData: function(data) { | |
d("Got ID of the latest Tweet and calling refreshToID"); | |
plScript.plNews.refreshToID(Number(data.max_id)); | |
}, | |
refreshToID: function(inID) { | |
d("Refreshing our memory to keep eveything posted till this tweet ID" + inID); | |
if (plScript.plNews.memory.knownLatestTweetID == undefined) | |
plScript.plNews.memory.knownLatestTweetID = Number(inID); | |
plScript.plNews.refreshToNow(); | |
// This HELPER function refreshes knownlatestTweetId then calls refreshToNow to clean us up. | |
}, | |
refreshToNowOngoing: false, | |
refreshToNow: function() { | |
d("refreshToNow called. Checking if there is a need to refresh the Tweet Silo by comparing the local Known tweet ID with the remote available Tweet ID"); | |
d(plScript.plNews.description); | |
if (plScript.plNews.memory.retrievedLatestTweetID == undefined) | |
plScript.plNews.memory.retrievedLatestTweetID = 0; | |
if (plScript.plNews.memory.knownLatestTweetID <= plScript.plNews.memory.retrievedLatestTweetID) return; | |
d("There is a need to load more recent tweets, calling $.getJSON with ppl of 100"); | |
$.getJSON( | |
plScript.plNews.twitterTunnelURIWithParameters("from:" + String(plScript.plNews.conf.observe) + "&since_id=" + plScript.plNews.memory.retrievedLatestTweetID + "&rpp=100"), | |
plScript.plNews.didRefreshToNowWithData | |
); | |
// Polls Twitter about tweets newer than retrieved | |
}, | |
didRefreshToNowWithData: function(data) { | |
d("refresh to now completed with following data"); | |
d(data); | |
if (data.results.length == 0) return; | |
d("did not retrieve an empty set, start processing"); | |
var retrievedlatestTweetIDInBatch = 0; | |
$.each(data.results, function(index, child) { | |
d("tweet message:"); | |
d(child); | |
retrievedlatestTweetIDInBatch = Math.max(retrievedlatestTweetIDInBatch, child.id); | |
}); | |
// Processes data, keep reordering refreshToNow using setInterval till we have everything | |
// Check in to silo one-by-one | |
// Fix Known pointers — retrievedLatestTweet | |
plScript.plNews.memory.retrievedLatestTweetID = retrievedlatestTweetIDInBatch; | |
d("Latest tweet in memory:"); | |
d(plScript.plNews.memory.retrievedLatestTweetID); | |
// Check if there still is a need to refresh more; | |
d("calling once again refreshToNow and have it bail itself out if we already have everything"); | |
//setTimeout(plScript.plNews.refreshToNow, 5); | |
// if ([delegate respondsToSelector:didReceiveNewerTweets]) | |
// [delegate didReceiveNewerTweets:data]; | |
if (plScript.plNews.delegates.plNewsDidReceiveNewerTweets) plScript.plNews.delegates.plNewsDidReceiveNewerTweets(data.results); | |
}, | |
refreshToPastOngoing: false, | |
refreshToPast: function() { | |
// Polls Twitter about tweets older than retrieved | |
}, | |
didRefreshToPastWithData: function() { | |
// Processes data, keep reordering refreshToPast using setInterval till we have everything | |
// Check in to silo one-by-one | |
// if ([delegate respondsToSelector:didReceiveOlderTweets]) | |
// [delegate didReceiveOlderTweets:data]; | |
if (plScript.plNews.delegates.plNewsDidReceiveOlderTweets) plScript.plNews.delegates.plNewsDidReceiveOlderTweets(data); | |
}, | |
twitterTunnelURIBase: 'http://search.twitter.com/search.json?', | |
twitterTunnelURIWithParameters: function(append) { | |
return plScript.plNews.twitterTunnelURIBase + "q=" + String(append) + "&callback=?"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment