[Twitter] Pulling images from tweets
in
Contributed Library Questions
•
7 months ago
Hi,
I need help. I'd like to pull images from tweets with a certein hashtag.
I use this API for the first time ever (most of the code is from the tutorial
http://blog.blprnt.com/blog/blprnt/updated-quick-tutorial-processing-twitter - that's why I'm using a dated library).
I've managed to pull URLs from tweets but:
1. Not every URL is an image
2. 99% of images are not direct file link but something like this:
http://instagram.com/p/XA5EPtQgs6/
I know that it's possible to pull media data only -
https://dev.twitter.com/docs/tweet-entities - but it's JSON and I'm kinda lost.
ATM all I get is an error "The file
http://instagr.am/p/XA5EPtQgs6/ contains bad image data, or may not be an image."
Here's the code (it's a mess but I was trying to get it to work, not to make it pretty:)
- /*
- Moje dane:
- consumer key:
- consumer secret:
- acces token:
- acces token secret:
- */
- //Build an ArrayList to hold all of the words that we get from the imported tweets
- ArrayList<String> words = new ArrayList();
- String url,y;
- URL x;
- PImage webImg;
- void setup() {
- //Set the size of the stage, and the background to black.
- size(400,400);
- background(0);
- smooth();
- //Credentials
- ConfigurationBuilder cb = new ConfigurationBuilder();
- cb.setOAuthConsumerKey("");
- cb.setOAuthConsumerSecret("");
- cb.setOAuthAccessToken("");
- cb.setOAuthAccessTokenSecret("");
- //Make the twitter object and prepare the query
- Twitter twitter = new TwitterFactory(cb.build()).getInstance();
- Query query = new Query(""#art"");
- query.setRpp(100);
- //Try making the query request.
- try {
- QueryResult result = twitter.search(query);
- ArrayList tweets = (ArrayList) result.getTweets();
- for (int i = 0; i < tweets.size(); i++) {
- Tweet t = (Tweet) tweets.get(i);
- String user = t.getFromUser();
- String msg = t.getText();
- //url = t.getProfileImageUrl();
- //url = t.getURL();
- //URL url = t.getURL();
- Date d = t.getCreatedAt();
- //println("Tweet by " + user + " at " + d + ": " + msg);
- URLEntity[] urls = t.getURLEntities();
- for(URLEntity url : urls){
- //System.out.println("A TO JEST DZIWNE" + url.getURL());
- x = url.getExpandedURL();
- println(url.getURL());
- }
- //Break the tweet into words
- String[] input = msg.split(" ");
- for (int j = 0; j < input.length; j++) {
- //Put each word into the words ArrayList
- words.add(input[j]);
- }
- };
- }
- catch (TwitterException te) {
- println("Couldn't connect: " + te);
- };
- }
- void draw() {
- //Draw a faint black rectangle over what is currently on the stage so it fades over time.
- fill(0,1);
- //rect(0,0,width,height);
- //Draw a word from the list of words that we've built
- int i = (frameCount % words.size());
- String word = words.get(i);
- //Put it somewhere random on the stage, with a random size and colour
- fill(255,random(50,150));
- textSize(random(10,30));
- text(word, random(width), random(height));
- //String img = url;
- // Load image from a web server
- if(mousePressed){
- word= words.get(i+1);
- }
- y=""+x;
- //println(y);
- webImg = loadImage(y, "jpg");
- image(webImg,0,0);
- }
Thx! :)
1