Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I've been running into this problem ever since I wrote the following code. The code is supposed to return a query of tweets, based on a key-search using the tweeter API. I have yet to resolve this issue, could someone kindly help me fix this problem? Thank you!

EDIT: Apologies, here's the error message I get href="https://imgur.com/a/IpM8UTL"

# YOU ARE REQUIRED TO INSERT YOUR CODE IN THIS CELL
'''
You will be required to insert your own codes to complete this function. 
Walk through this function and enter your own codes where instructed.
'''
def retrieve_tweets(api, keyword, batch_count, total_count, latitude, longitude, delta_lat, delta_lon):
   
    """
    collects tweets using the Twitter search API
    
    api:         Twitter API instance
    keyword:     search keyword
    batch_count: maximum number of tweets to collect per each request
    total_count: maximum number of tweets in total
    latitude:    latitude of the location centre from where tweets are retrieved
    longitude:   longitude of the location centre from where tweets are retrieved
    delta_lat:   latitude delta (from the location centre) in degrees
    delta_lon:   longitude delta (from the location centre) in degrees
    """

    
    # the collection of tweets to be returned
    tweets_unfiltered = []
    tweets = []
    
    # the number of tweets within a single query
    batch_count = str(batch_count)
    
    '''
    You are required to insert your own code where instructed to perform the first query to Twitter API.
    Hint: revise the practical session on Twitter API on how to perform query to Twitter API.
    '''    
    resp = api.request('tweets/search/recent', {'query':  keyword, # INSERT YOUR CODE 
                                     'max_results': total_count, # INSERT YOUR CODE
                                     'tweet.fields': {'lang':'en'},      
                                     'place.fields':{
                                     'geo': {
                                     "type": "Feature",
                                     "bbox": [
                                     longitude - delta_lon,
                                     latitude - delta_lat,
                                     longitude + delta_lon,
                                     latitude + delta_lat
                                      ],
                                      "properties": {}
                                      }}})
    

    # check first if there was an error
    if ('errors' in resp.json()):
        errors = resp.json()['title']
        if (errors == 'Invalid Request'):
            print('Too many attempts to load tweets or too many tweets to load.')
            print('You need to wait for a few minutes before accessing Twitter API again or reduce max_results.')
    

    
    if ('statuses' in resp.json()):
        tweets_unfiltered += resp.json()['statuses']
        tweets = [tweet for tweet in tweets_unfiltered if ((tweet['retweeted'] != True) and ('RT @' not in tweet['text']))]
    
        # find the max_id_str for the next batch
        ids = [tweet['id'] for tweet in tweets_unfiltered]
        max_id_str = str(min(ids))

        # loop until as many tweets as total_count is collected
        number_of_tweets = len(tweets)
        while number_of_tweets < total_count:

            resp = api.request('search/tweets', {'q': keyword,
                                             'count': total_count,
                                             'lang':'en',
                                             'result_type': 'recent', 
                                             'max_id': max_id_str,
                                             'geocode':'{},{},{}mi'.format(latitude, longitude, delta_lat, delta_lon)}) #INSERT YOUR CODE
                          

            if ('statuses' in resp.json()):
                tweets_unfiltered += resp.json()['statuses']
                tweets = [tweet for tweet in tweets_unfiltered if ((tweet['retweeted'] != True) and ('RT @' not in tweet['text']))]
 
                ids = [tweet['id'] for tweet in tweets_unfiltered]
                max_id_str = str(min(ids))
            
                number_of_tweets = len(tweets)
        
            print("{} tweets are collected for keyword {}. Last tweet created at {}".format(number_of_tweets, 
                                                                                    keyword, 
                                                                                    tweets[number_of_tweets-1]['created_at']))

    
    ids = [int(tweet['id']) for tweet in tweets_unfiltered]
    max_id_str = str(min(ids))
    
    tweets = [tweet for tweet in tweets_unfiltered if (('RT @' not in tweet['text']) & (tweet['lang'] == 'en'))]
    
    
    # loop until as many tweets as total_count is collected
    number_of_tweets = len(tweets)
    
    while number_of_tweets < total_count:

    
        resp = api.request('tweets/search/recent', {'query': keyword, # INSERT YOUR CODE
                                         'max_results':total_count, # INSERT YOUR CODE                                     
                                         'until_id': max_id_str,
                                         'tweet.fields': {'lang':'en'},      
                                         'place.fields':{
                                         'geo': {
                                         "type": "Feature",
                                         "bbox": [
                                         longitude - delta_lon,
                                         latitude - delta_lat,
                                         longitude + delta_lon,
                                         latitude + delta_lat
                                         ],
                                         "properties": {}
                                         }}})

            
        tweets_unfiltered += resp
        tweets = [tweet for tweet in tweets_unfiltered if (('RT @' not in tweet['text']) & (tweet['lang'] == 'en'))]
 
        ids = [int(tweet['id']) for tweet in tweets_unfiltered]
        max_id_str = str(min(ids))
            
        number_of_tweets = len(tweets)
        
        print("{} tweets are collected for keyword {}. ".format(number_of_tweets, keyword))
   
    return tweets

# YOU ARE REQUIRED TO INSERT YOUR CODE IN THIS CELL
'''
Your task is to write the code to perform three function calls, each corresponds to one keyword. 
And, you are required to collect at least 200 tweets for each key word.
'''

# Collecting the tweets for three assigned keywords, 
# Your function call should look like this:
#      retrieve_tweets(api, keyword, batch_count, total_count, latitude, longitude, delta_lat, delta_lon)



k1_tweets = retrieve_tweets(api,'donkey',50,200,PLACE_LAT,PLACE_LON, DELTA_LAT, DELTA_LON )
k2_tweets = retrieve_tweets(api,'condition',50,200,PLACE_LAT,PLACE_LON, DELTA_LAT, DELTA_LON)
k3_tweets = retrieve_tweets(api,'motion',50,200,PLACE_LAT,PLACE_LON, DELTA_LAT, DELTA_LON)

# PLEASE NOTE THAT IF YOU RUN THIS CELL, IT MIGHT TAKE A WHILE TO DOWNLOAD ALL THE TWEETS REQUIRED.
# MAKE SURE THAT YOU WAIT UNTILL THE CELL FINISHES RUNNING.


What I have tried:

I've tested the code and other possibilities such as excluding one of the queries, but nothing worked so far.
Posted
Updated 26-May-22 4:56am
v3
Comments
Richard MacCutchan 26-May-22 10:15am    
You need to ask a proper question. We have no idea what coding error you are referring to.

1 solution

It would be so much easier for everyone if you posted the actual error message with your question. However, having said that the error is at the following piece of code:
Python
ids = [int(tweet['id']) for tweet in tweets_unfiltered]
max_id_str = str(min(ids))

And the message states:
ValueError: min() arg is an empty sequence

Which tells you that the list comprehension did not return any values. This could be because tweets_unfiltered does not contain any data. So you need to use the debugger to find out why.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900