Code
String url = "http://twitter.com/statuses/user_timeline/golan.rss?count=200";
String[] strArray = loadStrings(url);
int nElements = strArray.length;
long startOf2008 = 1199163600000L;
long endOf2008 = 1230786000000L;
void setup(){
size(600,200);
background(0);
for (int i=0; i<nElements; i++){
String T=strArray[i].trim();
if(T.startsWith("<pubDate>")){
float f = map(plotTimeStamp(T), startOf2008, endOf2008, 0,width);
stroke(255);
line(f,0, f,height);
}
}
}
long plotTimeStamp(String someLine){
String TStamp = someLine;
int trimStrT = TStamp.indexOf(',');
String D = TStamp.substring(trimStrT + 2, trimStrT +4);
int dayInt = Integer.parseInt(D);
String Y = TStamp.substring(trimStrT + 9, trimStrT +13);
int yearInt = Integer.parseInt(Y);
String M = TStamp.substring(trimStrT + 5, trimStrT +8);
int monthInt=0;
String monthNames[] =
{ "Jan","Feb","Mar","Apr",
"May","Jun","Jul","Aug",
"Sep","Oct","Nov","Dec"};
for (int i=0; i<12; i++){
if (monthNames[i].equals(M)){
monthInt=i;
break;
}
}
GregorianCalendar greg = new GregorianCalendar(yearInt, monthInt, dayInt);
Date someDate = greg.getTime();
long millisecondsSince1970 = someDate.getTime();
println(someDate);
long pointPlot=millisecondsSince1970;
return pointPlot;
}
0703 - Displaying a Twitter Timeline: Displaying scraped dates
Statement:
In this assignment, we will scrape a Twitter feed in order to display a timeline of someone's Twitter posts in 2008.
The following two lines of code will scrape data from Twitter, in order to produce an array of Strings which store up to 200 of my most recent posts:
String url = "http://twitter.com/statuses/user_timeline/golan.rss?count=200";
String[] strArray = loadStrings(url);
Each element in this array is a String, which stores one line from an RSS feed of the Twitter user "golan". You can see the raw text of this RSS feed if you view the source of the URL in a browser, or if you println(strArray). (You can also try another Twitter user if you want. The Twitter API supports a maximum of 200 posts requested at a time.) So: Write a program which searches through this feed, as follows:
// Some helpful constants: in milliseconds since 1970. // Note that the "L" tells Java that the number is a long, not an int. long startOf2008 = 1199163600000L; long endOf2008 = 1230786000000L;Now we will display the timeline. Assuming we have a millisecondsSince1970 for each posting, try out the following code:
// map the timestamp of the Twitter post. // the input range is the start and end of 2008; // the output range is 0 to the width of the canvas: float f = map(millisecondsSince1970, startOf2008, endOf2008, 0,width); line(f,0, f,height);
hide statement