Skip to content

Instantly share code, notes, and snippets.

Created April 21, 2010 03:53
Show Gist options
  • Save anonymous/373395 to your computer and use it in GitHub Desktop.
Save anonymous/373395 to your computer and use it in GitHub Desktop.
import processing.core.PApplet;
import processing.xml.XMLElement;
import java.lang.reflect.*;
import java.net.URLEncoder;
public class AsyncTwitterSearch extends Thread
{
PApplet parent;
String searchTerm;
Object callback;
String lang;
TweetFilterStrategy filter;
public static final String SEARCH_URI = "http://search.twitter.com/search.atom?q=";
AsyncTwitterSearch(PApplet _parent, String _searchTerm, String _lang, TweetFilterStrategy _filter, Object _callback) {
System.out.println("AsyncTwitter");
parent = _parent;
filter = _filter;
lang = _lang;
callback = _callback;
searchTerm = _searchTerm;
}
public void run() {
System.out.println("Hello from a thread!");
loadMessages();
}
void loadMessages() {
XMLElement xml = null;
String u = AsyncTwitterSearch.SEARCH_URI + '"' + URLEncoder.encode(searchTerm) + "\"&lang="+lang;
System.out.println(u);
xml = new XMLElement(parent, u);
XMLElement[] entries = xml.getChildren("entry");
String[] messages = new String[entries.length];
for (int i=0; i<entries.length; i++) {
messages[i] = filter.filter(searchTerm, entries[i].getChildren("title")[0].getContent());
}
// call the callback
try {
Class[] argTypes = { String[].class };
Method method = callback.getClass().getDeclaredMethod("onMessages", argTypes);
method.invoke(callback, new Object[] {messages});
} catch(NoSuchMethodException e) {
System.out.println("You need to define onMessages(String[] messages)");
} catch(Exception e) {
System.out.println("Something ain't right: "+e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment