Created
November 9, 2011 18:19
-
-
Save kuzyn/1352335 to your computer and use it in GitHub Desktop.
echo
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
class TweetStream { | |
Queue tweetQueue; | |
ArrayList<Tweet> collection; | |
boolean firstSave = true; | |
TweetStream() { | |
collection = new ArrayList(); | |
tweetQueue = new LinkedList(); | |
println("TweetStream initiated"); | |
} | |
void update() { | |
// check if new tweets have to be pulled | |
// if yes, do so | |
// | |
} | |
void updateRecord() { | |
ArrayList<Tweet> newTweets = queryTwitter("google"); | |
if (newTweets != null) { | |
ArrayList<Tweet> temp = new ArrayList(); | |
Date collectionsLastDate; | |
if (collection.size() == 0) { | |
collectionsLastDate = new Date(0,0,0); | |
} | |
else { | |
collectionsLastDate = collection.get(collection.size()-1).getCreatedAt(); | |
} | |
for (int i = 0; i < newTweets.size(); i++) { | |
Date currentTweetsDate = newTweets.get(i).getCreatedAt(); | |
if (collectionsLastDate.compareTo(currentTweetsDate) >= 0 ) { | |
break; | |
} | |
else { | |
temp.add(newTweets.get(i)); | |
} | |
} | |
println(temp.size() + " new Tweets"); | |
for (int i = temp.size()-1; i >= 0; i--) { | |
collection.add(temp.get(i)); | |
if (temp.size() > 70) { | |
println(collectionsLastDate +" - " + temp.get(i).getCreatedAt() + ""); | |
} | |
} | |
} | |
} | |
void saveFile() { | |
//String lines[] = loadStrings("lines.txt"); | |
for (int i = 0; i < collection.size()-1; i++) { | |
output.println(collection.get(i).getCreatedAt() + ""); | |
} | |
output.flush(); | |
Tweet temp = collection.get(collection.size()-1); | |
collection = new ArrayList(); | |
collection.add(temp); | |
println("Data saved"); | |
//exit(); // Stop the program | |
} | |
private ArrayList<Tweet> queryTwitter(String sQuery) { | |
int tempMillis = millis(); | |
Twitter twitter = new TwitterFactory().getInstance(); | |
twitter.setOAuthConsumer(consumer_key, consumer_secret); | |
twitter.setOAuthAccessToken(new AccessToken( oauth_token, oauth_token_secret) ); | |
Query query = new Query(sQuery); | |
query.setRpp(100); | |
try { | |
QueryResult result = twitter.search(query); | |
ArrayList tweets = (ArrayList) result.getTweets(); | |
/* | |
for (int i = 0; i < tweets.size(); i++) { | |
Tweet t = (Tweet) tweets.get(i); | |
String user = t.getFromUser(); | |
String msg = t.getText(); | |
Date d = t.getCreatedAt(); | |
println("Tweet by " + user + " at " + d + ": " + msg); | |
} | |
*/ | |
println("receiving took: " + (millis() - tempMillis) ); | |
return tweets; | |
} | |
catch (TwitterException e) { | |
println(e.getStatusCode()); | |
return null; | |
} | |
} | |
} |
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
import processing.opengl.*; | |
import twitter4j.conf.*; | |
import twitter4j.internal.async.*; | |
import twitter4j.internal.org.json.*; | |
import twitter4j.internal.logging.*; | |
import twitter4j.json.*; | |
import twitter4j.internal.util.*; | |
import twitter4j.management.*; | |
import twitter4j.auth.*; | |
import twitter4j.api.*; | |
import twitter4j.util.*; | |
import twitter4j.internal.http.*; | |
import twitter4j.*; | |
import twitter4j.internal.json.*; | |
import java.util.Date; | |
String consumer_key = "YOUR-KEY"; | |
String consumer_secret = "YOUR-SECRET"; | |
String oauth_token = "YOUR-TOKEN"; | |
String oauth_token_secret = "YOUR-TOKEN-SECRET"; | |
PrintWriter output; | |
TweetStream tStream; | |
int lastSecond = -1; | |
int lastMinute; | |
void setup() { | |
size(640, 480, OPENGL); | |
tStream = new TweetStream(); | |
lastMinute = minute(); | |
output = createWriter("numbers.txt"); | |
//tStream.updateRecord(); | |
} | |
void draw() { | |
background(255); | |
if( lastSecond != second()){ | |
tStream.updateRecord(); | |
lastSecond = second(); | |
} | |
if( lastMinute != minute() ){ | |
tStream.saveFile(); | |
lastMinute = minute(); | |
} | |
} | |
void keyPressed() { // Press a key to save the data | |
tStream.saveFile(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment