-
-
Save brysonian/373396 to your computer and use it in GitHub Desktop.
Async searching of twitter for processing
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.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