I trust all is well with you and yours / thanks for taking a moment to read this. I am trying to figure out why this section of code does not respect 2 things.
- the count limit, it is supposed to only pull that many tweets, and it does whatever it wants to do.
- the code seems to loop indefinitely, and unless I put a hard stop, it doesn’t stop (hard stop is not currently included, though it would be something akin to adding a for in range loop that matches count. Though, I am not sure that would work either.
JavaScript
x
55
55
1
import tweepy
2
import Auth_Codes
3
import json
4
import os
5
6
7
def tweet_search(query=""):
8
print(f"'Query = {query}'")
9
10
twitter_auth_keys = {
11
"consumer_key" : Auth_Codes.consumer_key,
12
"consumer_secret" : Auth_Codes.consumer_secret,
13
"access_token" : Auth_Codes.access_token,
14
"access_token_secret" : Auth_Codes.access_token_secret
15
}
16
auth = tweepy.OAuthHandler(
17
twitter_auth_keys["consumer_key"],
18
twitter_auth_keys["consumer_secret"]
19
)
20
21
auth.set_access_token(
22
twitter_auth_keys["access_token"],
23
twitter_auth_keys["access_token_secret"]
24
)
25
26
api = tweepy.API(auth,wait_on_rate_limit=True)
27
28
cursor = tweepy.Cursor(api.search_tweets,q = f'{query}',lang = 'en',result_type = 'recent',count = 1)
29
print("Query Complete")
30
31
tweet_payload = []
32
33
for tweet in cursor.items():
34
35
current_searches_remaining = api.rate_limit_status()['resources']['search']['/search/tweets']['remaining']
36
current_search_limit = api.rate_limit_status()['resources']['search']['/search/tweets']['limit']
37
max_searches = current_search_limit * .25
38
39
if current_searches_remaining >= (max_searches):
40
print("Checking rate limit")
41
print("Searches Remaining: ", current_searches_remaining, "/", current_search_limit)
42
print("Converting payload to a useable format")
43
44
tweet_payload.append(tweet._json)
45
46
with open("Tweet_Payload.json", "w") as outfile:
47
outfile.write(json.dumps(tweet_payload,indent=4, sort_keys=True))
48
tweet_filter()
49
50
else:
51
print("Rate Limit Hit, Moving On")
52
tweet_filter()
53
print("Search Complete")
54
tweet_filter()
55
Advertisement
Answer
While I am still working out the finer details, after making the following change, it now loops nearly as expected.
JavaScript
1
7
1
cursor = tweepy.Cursor(api.search_tweets,q = f'{query}',lang = 'en',result_type = 'recent',count = 11)
2
print("Query Complete")
3
4
tweet_payload = []
5
6
for tweet in cursor.items(11):
7