skip to Main Content

I’m trying to write a simple python programme that uses the tweepy API for twitter and wget to retrieve the image link from a twitter post ID (Example: twitter.com/ExampleUsername/12345678), then download the image from the link. The actual programme works fine, but there is a problem. While it runs FOR every ID in the dictionary (if there are 2 IDs, it runs 2 times), it doesn’t use every ID, so the script ends up looking at the last ID on the dictionary, then downloading the image from that same id however many times there is an ID in the dictionary. Does anyone know how to make the script run again for every ID?

tl;dr I want the programme to look at the first ID, grab its image link, download it, then do the same thing with the next ID until its done all of the IDs.

#!/usr/bin/env python
# encoding: utf-8

import tweepy #https://github.com/tweepy/tweepy
import wget


#Twitter API credentials
consumer_key = "nice try :)"
consumer_secret = "nice try :)"
access_key = "nice try :)"
access_secret = "my, this joke is getting really redundant"

def get_all_tweets():
        #authorize twitter, initialize tweepy
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_key, access_secret)
        api = tweepy.API(auth)

        id_list = [1234567890, 0987654321]
        # Hey StackOverflow, these are example ID's. They won't work as they're not real twitter ID's, so if you're gonna run this yourself, you'll want to find some twitter IDs on your own

# tweets = api.statuses_lookup(id_list)
        for i in id_list:
            tweets = []
            tweets.extend(api.statuses_lookup(id_=id_list, include_entities=True))
            for tweet in tweets:
                spacefiller = (1+1)
                # this is here so the loop runs, if it doesn't the app breaks 
            a = len(tweets)
            print(tweet.entities['media'][0]['media_url'])
            url = tweet.entities['media'][0]['media_url']
            wget.download(url)
get_all_tweets()

Thanks,
~CS

2

Answers


  1. Chosen as BEST ANSWER

    I figured it out! I knew that loop was being used for something...

    I moved everything from a = len(tweets to wget.download(url) into the for tweet in tweets: loop, and removed the for i in id_list: loop.

    Thanks to tdelany this programme works now! Thanks everyone!

    Here's the new code if anyone wants it:

    #!/usr/bin/env python
    # encoding: utf-8
    
    import tweepy #https://github.com/tweepy/tweepy
    import wget
    
    
    #Twitter API credentials
    consumer_key = "nice try :)"
    consumer_secret = "nice try :)"
    access_key = "nice try :)"
    access_secret = "my, this joke is getting really redundant"
    
    def get_all_tweets():
            #authorize twitter, initialize tweepy
            auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
            auth.set_access_token(access_key, access_secret)
            api = tweepy.API(auth)
    
            id_list = [1234567890, 0987654321]
            # Hey StackOverflow, these are example ID's. They won't work as they're not real twitter ID's, so if you're gonna run this yourself, you'll want to find some twitter IDs on your own
    
            tweets = []
            tweets.extend(api.statuses_lookup(id_=id_list, include_entities=True))
            for tweet in tweets:
                a = len(tweets)
                print(tweet.entities['media'][0]['media_url'])
                url = tweet.entities['media'][0]['media_url']
                wget.download(url)
    get_all_tweets()
    

  2. One strange thing I see is that the variable i declared in the outer loop is never used after on. Shouldn’t your code be

     tweets.extend(api.statuses_lookup(id_=i, include_entities=True))
    

    and not id_=id_list as you wrote?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search