Created
February 23, 2012 17:17
-
-
Save jakevsrobots/1893874 to your computer and use it in GitHub Desktop.
Processing sketch that fetches stuff from twitter & flickr
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
ArrayList imageList; | |
int currentImageIndex = 0; | |
ArrayList fetchNewImages(String keyword) { | |
XMLElement xml = new XMLElement(this, "http://api.flickr.com/services/feeds/photos_public.gne?tags=" + keyword + "&lang=en-us&format=rss_200"); | |
XMLElement[] linkTags = xml.getChildren("entry/link"); | |
ArrayList imageList = new ArrayList(); | |
for (int i=0; i < linkTags.length; i++) { | |
XMLElement linkTag = linkTags[i]; | |
String rel = linkTag.getString("rel"); | |
if (rel.equals("enclosure")) { | |
imageList.add(linkTag.getString("href")); | |
} | |
} | |
return imageList; | |
} | |
String getRandomWordFromTwitterSearch(String search) { | |
// Search for tweets and download the XML | |
XMLElement xml = new XMLElement(this, "http://search.twitter.com/search.atom?q=" + search); | |
XMLElement[] tweets = xml.getChildren("entry/title"); | |
// Get a random word from the first tweet | |
String corpus = tweets[0].getContent(); | |
String[] words = corpus.split("\\s+"); | |
String randomWord = words[int(random(words.length))]; | |
println(randomWord); | |
return randomWord; | |
} | |
void setup() { | |
size(800, 600); | |
String keyword = getRandomWordFromTwitterSearch("justin+bieber"); | |
imageList = fetchNewImages(keyword); | |
} | |
void draw() { | |
background(0); | |
String url = (String)imageList.get(currentImageIndex); | |
PImage graphic = loadImage(url); | |
image(graphic, 0, 0); | |
currentImageIndex++; | |
if (currentImageIndex >= imageList.size()) { | |
currentImageIndex = 0; | |
String keyword = getRandomWordFromTwitterSearch("justin+bieber"); | |
imageList = fetchNewImages(keyword); | |
} | |
graphic.save("relax_" + currentImageIndex + ".jpg"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment