Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import requests
import json
btoken = 'YOUR_TOKEN_HERERERRERER'
url = 'https://api.twitter.com/2/tweets/search/recent'
headers = {"Authorization": f"Bearer {btoken}"}
all_data = []
payload = {
'query' : '"cultural heritage"',
'max_results': 100,
'expansions' : 'attachments.poll_ids,attachments.media_keys,author_id,entities.mentions.username,geo.place_id,in_reply_to_user_id,referenced_tweets.id,referenced_tweets.id.author_id',
'media.fields' : 'duration_ms,height,media_key,preview_image_url,type,url,width,public_metrics',
'tweet.fields' : 'attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,public_metrics,possibly_sensitive,referenced_tweets,source,text,withheld'
}
r = requests.get(url,params=payload, headers=headers)
data = json.loads(r.text)
all_data = all_data + data['data']
next_token = data['meta']['next_token']
for number in range(1,5):
print('on request #',number)
payload = {
'query' : '"cultural heritage"',
'next_token' : next_token
}
r = requests.get(url,params=payload, headers=headers)
all_data = all_data + data['data']
next_token = data['meta']['next_token']
json.dump(all_data,open('twitter_data_tmp.json','w'),indent=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment