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:
  • Using a for{} loop and the String.startsWith() method, check if each line begins with the String "<pubDate>" ...

  • If it does, use your solution from Assignment 0702 to compute, for each Twitter post, the timestamp at which it was posted (represented as milliseconds since January 1, 1970). NOTE: These will be returned as 64-bit long numbers (not as 32-bit int numbers). This is because integers only have enough precision to represent 25 days' worth of milliseconds!

  • Now we are going to display the history of my most recent 200 Twitter postings from 2008, on a timeline which maps the year of 2008 to the width of the Processing canvas. To begin, here are some helpful constants, which you should incorporate into your code:
    // 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);
    
  • For security reasons (as I explained in class), an applet running this code in a browser will not work properly. Instead of uploading an applet, take a screenshot of your working project and upload that instead. Your solution (for the Twitter timeline of user "golan", anyway) should look like the image posted above.

    hide statement