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.