DavidMS
Posts: 139
Joined: Fri May 25, 2012 8:48 pm
Contact: Website

using Tweepy to 'receive' pictures

Wed Jan 07, 2015 9:48 pm

Is it possible to use Tweepy to capture an picture from a Tweet ?

Basically I am trying to build an application which will look at Twitter for tweets with a specific #hashtag grab the attached picture and display it on a LCD display screen attached to a Rpi. there are a number of examples that capture the tweet text but I cannot find any which get a picture attached to the tweet ?
http://meanderingpi.wordpress.com/
11 Raspberry Pi's and counting....

User avatar
DougieLawson
Posts: 38517
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: using Tweepy to 'receive' pictures

Wed Jan 07, 2015 10:05 pm

You can send pictures with Tweepy.
https://twitter.com/IMSlights/status/428334873859067905

Here's my Tweepy code to fetch that tweet (and a few others in my timeline)

Code: Select all

auth = tweepy.OAuthHandler(Ckey, Csec)
auth.set_access_token(Akey, Asec)
twapi = tweepy.API(auth)
page_list = []
n = 0
for page in tweepy.Cursor(twapi.user_timeline, include_entities="true", count=2).pages(16):
  page_list.append(page)
  n = n+1


for page in page_list:
  for status in page:
    #print dir(status)
    try:
       print "Entities:",status.entities
       print "Extended_entities:",status.extended_entities
    except:
       print "\r\n"
    print "Text:",status.text
That one has these attributes (from print dir(status))

Code: Select all

['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_api', 'author', 'contributors', 'coordinates', 'created_at', 'destroy', 'entities', 'extended_entities', 'favorite', 'favorite_count', 'favorited', 'geo', 'id', 'id_str', 'in_reply_to_screen_name', 'in_reply_to_status_id', 'in_reply_to_status_id_str', 'in_reply_to_user_id', 'in_reply_to_user_id_str', 'lang', 'parse', 'parse_list', 'place', 'possibly_sensitive', 'retweet', 'retweet_count', 'retweeted', 'retweets', 'source', 'source_url', 'text', 'truncated', 'user']
If we look at tweepy.models.status.entities there's the URL for the photo in there

Code: Select all

 {u'symbols': [], u'user_mentions': [], u'hashtags': [], u'urls': [], u'media': [{u'expanded_url': u'http://twitter.com/IMSlights/status/428334873859067905/photo/1', u'display_url': u'pic.twitter.com/aeQ8XOLkf1', u'url': u'http://t.co/aeQ8XOLkf1', u'media_url_https': u'https://pbs.twimg.com/media/BfHATFjIMAAXb8Y.jpg', u'id_str': u'428334873716469760', u'sizes': {u'small': {u'h': 76, u'resize': u'fit', u'w': 340}, u'large': {u'h': 158, u'resize': u'fit', u'w': 698}, u'medium': {u'h': 135, u'resize': u'fit', u'w': 600}, u'thumb': {u'h': 150, u'resize': u'crop', u'w': 150}}, u'indices': [31, 53], u'type': u'photo', u'id': 428334873716469760L, u'media_url': u'http://pbs.twimg.com/media/BfHATFjIMAAXb8Y.jpg'}]}
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

DavidMS
Posts: 139
Joined: Fri May 25, 2012 8:48 pm
Contact: Website

Re: using Tweepy to 'receive' pictures

Thu Jan 08, 2015 1:35 pm

Thanks I will give it a try this weekend and see where I get to :)
http://meanderingpi.wordpress.com/
11 Raspberry Pi's and counting....

User avatar
DougieLawson
Posts: 38517
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: using Tweepy to 'receive' pictures

Thu Jan 08, 2015 1:41 pm

DavidMS wrote:Thanks I will give it a try this weekend and see where I get to :)
Feel free to follow me http://twitter.com/DougieLawson or follow my http://twitter.com/IMSLights and/or http://twitter.com/DougiesArduino ids if you need someone or something to tweet @.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

DavidMS
Posts: 139
Joined: Fri May 25, 2012 8:48 pm
Contact: Website

Re: using Tweepy to 'receive' pictures

Sat Jan 10, 2015 5:53 pm

Ok so I can get to point where I have captured the tweet but I am struggling with how separate out the ulr - clearly I want to grab 'media_url_https' but I am not sure how ?

I think the basic problem is a lack of understanding of string handling in Python

Code: Select all

print(status.entities)
works fine but I am not clear on how to get to next level of detail
http://meanderingpi.wordpress.com/
11 Raspberry Pi's and counting....

User avatar
DougieLawson
Posts: 38517
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: using Tweepy to 'receive' pictures

Sat Jan 10, 2015 6:32 pm

Code: Select all

for page in page_list:
  for status in page:
    #print dir(status)
    try:
       print "Entities:",status.entities
       print "Extended_entities:",status.extended_entities
    except:
       print "\r\n"
    print "Text:",status.text
    print "Username:",status.user.name
    print "Geo:",status.geo
    print "Created at:",status.created_at
    try:
      for url in status.entities['media']:
         print url['expanded_url']
         print url['display_url']
    except:
         print "\r\n"
There's better ways to test for the existence of a key in the data from Twitter, but that gets the job done.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

DavidMS
Posts: 139
Joined: Fri May 25, 2012 8:48 pm
Contact: Website

Re: using Tweepy to 'receive' pictures

Sun Jan 11, 2015 1:30 am

Dougie, thanks for your help I finally cracked it. The demo users streamlistener and looks for tweet from a specific user containing a specific hashtag, grabs the photo and displays it using pygame. In truth I need to spend some more time understanding Streamlistener to be a bit more clear on how it works .....

Code: Select all

import tweepy

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

import pygame  

import urllib
   
  
# Consumer keys and access tokens, used for OAuth  
consumer_key = 'xxxx'  
consumer_secret = 'xxxx'  
access_token = 'xxxx'  
access_token_secret = 'xxxx'  

class StdOutListener(StreamListener):
    ''' Handles data received from the stream. '''

    def on_status(self, status):
        # Prints the text of the tweet

        for media in status.entities['media']:
		urlref= media['media_url_https']
		print urlref

		resource = urllib.urlopen(urlref)
		output = open("file01.jpg","wb")
		output.write(resource.read())
		output.close()

	#display with pygame for test purposes

		screen.fill((black))
	        pygame.display.update()
		img = pygame.image.load("file01.jpg").convert()
		screen.blit(img, (10, 10))
		pygame.display.flip()
 
        return
 
    def on_error(self, status_code):
        print('Got an error with status code: ' + str(status_code))
        return True # To continue listening
 
    def on_timeout(self):
        print('Timeout...')
        return True # To continue listening
 
#main

#setup pygame
pygame.init()

size = width, height = 900, 900
speed = [2, 2]
black = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Tweet Test')



listener = StdOutListener()
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

while True: 
    stream = Stream(auth, listener)
    stream.filter(follow=['xxxxx'], track=['#pic_test']) 
http://meanderingpi.wordpress.com/
11 Raspberry Pi's and counting....

pulford
Posts: 26
Joined: Thu Dec 13, 2012 12:37 am

Re: using Tweepy to 'receive' pictures

Wed Jun 24, 2015 8:19 pm

I got it to work a couple times but I'm mostly ending up with a blank black screen. I can see the filename print on the console but it never shows up on screen.

Any ideas?
thx

User avatar
DougieLawson
Posts: 38517
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: using Tweepy to 'receive' pictures

Wed Jun 24, 2015 10:32 pm

There's nothing in my code sample to fetch the image URL and display it. It gets the URL ONLY.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

pulford
Posts: 26
Joined: Thu Dec 13, 2012 12:37 am

Re: using Tweepy to 'receive' pictures

Thu Jun 25, 2015 5:08 am

DougieLawson wrote:There's nothing in my code sample to fetch the image URL and display it. It gets the URL ONLY.
Oops sorry. I actually got DavidMS code working. But problem is, if there's no image it crashes the application. I'm having a hard time combining your entity checker with his code.

Any help appreciated. Thx

DavidMS
Posts: 139
Joined: Fri May 25, 2012 8:48 pm
Contact: Website

Re: using Tweepy to 'receive' pictures

Fri Jun 26, 2015 5:25 pm

I have just loaded the original code onto a Rpi and checked it and it worked fine. If you are getting the onscreen message that the application has captured a tweet with an attached jpg then then is sounds like there is an issue with Pygame. The Rpi I used is running the latest NOOBS image recently updated. May be try some other pygame code to see if that works ok. The Pygame code is only there to show the main capture section is working and was not really my main focus.
http://meanderingpi.wordpress.com/
11 Raspberry Pi's and counting....

pulford
Posts: 26
Joined: Thu Dec 13, 2012 12:37 am

Re: using Tweepy to 'receive' pictures

Wed Jul 01, 2015 9:21 pm

DavidMS wrote:I have just loaded the original code onto a Rpi and checked it and it worked fine. If you are getting the onscreen message that the application has captured a tweet with an attached jpg then then is sounds like there is an issue with Pygame. The Rpi I used is running the latest NOOBS image recently updated. May be try some other pygame code to see if that works ok. The Pygame code is only there to show the main capture section is working and was not really my main focus.
It's just that if there is no picture attached it breaks because it doesn't know what to do.
thx

Return to “Python”