Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get media_url from tweets using the Tweepy API

I am using this code:

import tweepy
from tweepy.api import API
import urllib
import os

i = 1
consumer_key="xx"
consumer_secret="xx"
access_token="xx"
access_token_secret="xx"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.secure = True
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

class MyStreamListener(tweepy.StreamListener):
    def __init__(self, api=None):
        self.api = api or API()
        self.n = 0
        self.m = 10

    def on_status(self, status):
        if 'media' in status.entities:
            for image in  status.entities['media']:
                global i
                #picName = status.user.screen_name
                picName = "pic%s.jpg" % i
                i += 1
                link = image['media_url']
                filename = os.path.join("C:/Users/Charbo/Documents/Python/",picName)
                urllib.urlretrieve(link,filename)
                #use to test
                print(status.user.screen_name)

        else: 
            print("no media_url")

        self.n = self.n+1

        if self.n < self.m: 
            return True
        else:
            print ('tweets = '+str(self.n))
            return False

    def on_error(self, status):
        print (status)

myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth, MyStreamListener(),timeout=30)
myStream.filter(track=['#feelthebern'])

I am trying the access the media_url under 'photo' in my dictionary. But I am getting the following error: 'dict' object has no attribute 'media'. I would appreciate help navigating the JSON.

Thanks in advance!

like image 979
MarcCharbo Avatar asked Apr 19 '16 07:04

MarcCharbo


People also ask

How do I extract data from Twitter using Tweepy?

Steps to obtain keys: – For access token, click ” Create my access token”. The page will refresh and generate access token. Tweepy is one of the library that should be installed using pip. Now in order to authorize our app to access Twitter on our behalf, we need to use the OAuth Interface.

How can I get more than 100 tweets on Tweepy?

If you need more than 100 Tweets, you have to use the paginator method and specify the limit i.e. the total number of Tweets that you want. Replace limit=1000 with the maximum number of tweets you want. Replace the limit=1000 with the maximum number of tweets you want (gist).

How do I get tweet location on Tweepy?

Identifying the location in the GUI : In order to get the location we have to do the following : Identify the user ID or the screen name of the profile. Get the User object of the profile using the get_user() method with the user ID or the screen name. From this object, fetch the location attribute present in it.


2 Answers

You should try two things :

  • Add entities to your request

>

tweepy.Cursor(api.search, q="#hashtag", count=5, include_entities=True)
  • Check if media is not nul :

>

if 'media' in tweet.entities:
    for image in  tweet.entities['media']:
        (do smthing with image['media_url'])

Hope this will help

like image 171
Falknn Avatar answered Nov 07 '22 07:11

Falknn


This reply might be a little late, but I'm sure other people will find it useful someday. I actually didn't want to retweet any tweet with a video in it. So I built this function.... and it works perfectly.

def on_status(self, status):
    #Ignores the tweet so long as I am the Author, or it's a reply to a tweet
    if status.in_reply_to_status_id is not None or \
        status.user.id == self.me.id:
        return

    #I only retweet tweets that I haven't yet retweeted. I also don't want to retweet any tweets that are quotes.
    if not status.retweeted and not status.is_quote_status:
        #Checking whether the tweet has no "media" in it.
        if 'media' not in status.entities:
            try:
                print(status.text)
                status.retweet()
                time.sleep(40) #Sleep for 40 seconds to avoid limits
            except Exception as e:
                print("Error on_data %s" % str(e))
                print("Error from retweeting")
        #If tweet has media, I only retweet a tweet with a photo
        elif 'media' in status.entities:
            media_details = status.entities['media']
            media_details_kind = media_details[0]
            #print(vide['type'])
            
            if media_details_kind['type'] == 'photo':
                try:
                    print("It is a photo")
                    status.retweet()
                    time.sleep(40)
                except Exception as e:
                    print("Error on_data %s" % str(e))
                    print("Error from retweeting")
        else: #Anything else is a video or GIF. I do nothing. 
            print("Sorry, this might be a video. Cound't retweet because it is neither a photo nor a text")
            print(status.text)
like image 25
Manzu Gerald Avatar answered Nov 07 '22 06:11

Manzu Gerald