Skip to content

Commit 81adf7b

Browse files
committed
Twitter v2 example
1 parent 757e9a1 commit 81adf7b

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 9,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stdout",
10+
"output_type": "stream",
11+
"text": [
12+
"200\n",
13+
"200\n",
14+
"200\n",
15+
"200\n",
16+
"200\n",
17+
"RT @ShannonJoyRadio: This morning at 9am a single nurse who is refusing the #COVID jab at Highland Hospital STOOD UP to Goliath ... The Uni…\n",
18+
"RT @MicroPapillon: ⚡🔞Un film #porno est diffusé par erreur en salle d'attente à l'hôpital de #Bayonne.\n",
19+
"\n",
20+
"Personne ne s'est plaint de l'atten…\n",
21+
"RT @JulikaBrand: in #Germany ➡️#police now attacks people, who address #Covid-restrictions... https://t.co/RDKN8bGjfH\n",
22+
"RT @laabdon: 🧪 Todos los hisopado de #COVID-19 realizados al plantel del Club @Nacional de Football dieron resultado #NEGATIVO. https://t.c…\n",
23+
"RT @LeahCMa: @DubreuilhMarcel 🤣🤣... Onfray victime du #Covid ...\n",
24+
"RT @780AM: #COVID🦠| \"En 24 horas del inicio de los síntomas, la persona ya se puede hisopar. Muchos esperan hasta cinco días y ya le contag…\n",
25+
"RT @RatnaK_Nanduri: #OxygenBeds available at #Guntur contact: Community Health Centre +91 86442 46941 #Validated on 24thMay 11:10 AM IST #C…\n",
26+
"#オードリー・タン デジタルとAIの未来を語る\n",
27+
"インタビュー時間20時間超!\n",
28+
"#テクノロジー界 の叡智が、描く\n",
29+
"デジタルとAI(人工知能)の未来!\n",
30+
"#台湾 は、2020年に全世界を襲った #新型コロナウイルス( #COVID-19)の封じ込めに唯一成功しました。\n",
31+
"\n",
32+
"https://t.co/np8iGxGiQ2\n",
33+
"RT @KuttyTwitz: முதலில் #ஈஷா யோக மையம்\n",
34+
"\n",
35+
"இரண்டாவதாக #ஜியரை தேர்ந்தெடுப்பது\n",
36+
"\n",
37+
"மூன்றாவதாக #பத்மாசேஷாத்ரி சேஷாத்ரி \n",
38+
"\n",
39+
" நான்காவதாக மகரிஷி வித்யா…\n",
40+
"RT @KPSCALnews: Pregnant & wondering if you should get the #COVID-19 vaccine? Questioning whether you should still get the vaccine? Join us…\n",
41+
"RT @Rwandapolice: Abapolisi b'u Rwanda bari mu butumwa bwo kubungabunga amahoro muri Sudani y'Epfo bakoze ubukangurambaga bwo kurwanya #COV…\n",
42+
"RT @laabdon: 🧪 Todos los hisopado de #COVID-19 realizados al plantel del Club @Nacional de Football dieron resultado #NEGATIVO. https://t.c…\n",
43+
"RT @RepDebDingell: 58% of Michiganders have received at least one dose of the #COVID vaccines!\n",
44+
" \n",
45+
"Benefits of getting vaccinated are clear –…\n",
46+
"RT @AlexBerenson: Today in great moments in #Covid panic porn:\n",
47+
"\n",
48+
"\"Demand for beds in US hospital intensive care units is likely to far excee…\n",
49+
"RT @shapeNprint: shapeNprint does it all!\n",
50+
"\n",
51+
"#freelancing #Ethics #GainSNZ #GainSZN #Apple #Freelance #Splatoon2 #RobloxDev #CatGirl #retweet…\n",
52+
"RT @andresenior: #Moderna says its #Covid #vaccine is 100% effective in teens, plans to seek FDA OK in early #June\n",
53+
"https://t.co/Hy8ewR8owt\n",
54+
"#BrianKemp just banned #covid-19 passports in #Georgia Thank goodness!!!! Atleast he came thru for GA on some things\n",
55+
"RT @KTNV: #UPDATE: Nevada reports 265 new #COVID cases over last 24 hours; test positivity rate drops to 4.4%\n",
56+
"\n",
57+
"Latest -- https://t.co/Qo54e…\n",
58+
"Não é “normal” @GloboNews uma gestora ser interrogada no #Senado pelo apelido pejorativo, apesar da depoente ter mostrado um desconforto na #CPI #covid com relação ao nome. Isso é desrespeitoso. O que interessa é a fala da gestora\n",
59+
"RT @buycrazydomains: productize enterprise #initiatives with #domains \n",
60+
"💰 Buy #domain #covid #lockdown https://t.co/qTvwSQLAvj https://t.co/…\n"
61+
]
62+
}
63+
],
64+
"source": [
65+
"import requests\n",
66+
"import os\n",
67+
"import json\n",
68+
"import twitter_authentication as config\n",
69+
"import time\n",
70+
"\n",
71+
"# Save your bearer token in a file called twitter_authentication.py in this directory\n",
72+
"bearer_token = config.bearer_token\n",
73+
"query = '(#COVID) OR (#COVID-19)'\n",
74+
"out_file = 'raw_tweets.json'\n",
75+
"\n",
76+
"search_url = \"https://api.twitter.com/2/tweets/search/all\"\n",
77+
"\n",
78+
"# Optional params: start_time,end_time,since_id,until_id,max_results,next_token,\n",
79+
"# expansions,tweet.fields,media.fields,poll.fields,place.fields,user.fields\n",
80+
"query_params = {'query': query,\n",
81+
" 'tweet.fields': 'author_id,public_metrics',\n",
82+
" 'max_results': 500\n",
83+
" }\n",
84+
"\n",
85+
"\n",
86+
"def create_headers(bearer_token):\n",
87+
" headers = {\"Authorization\": \"Bearer {}\".format(bearer_token)}\n",
88+
" return headers\n",
89+
"\n",
90+
"\n",
91+
"def connect_to_endpoint(url, headers, params, next_token = None):\n",
92+
" if next_token:\n",
93+
" params['next_token'] = next_token\n",
94+
" response = requests.request(\"GET\", search_url, headers=headers, params=params)\n",
95+
" print(response.status_code)\n",
96+
" if response.status_code != 200:\n",
97+
" raise Exception(response.status_code, response.text)\n",
98+
" return response.json()\n",
99+
"\n",
100+
"\n",
101+
"def get_tweets(num_tweets = 2000):\n",
102+
" tweets = []\n",
103+
" next_token = None\n",
104+
" while len(tweets) < num_tweets:\n",
105+
" headers = create_headers(bearer_token)\n",
106+
" json_response = connect_to_endpoint(search_url, headers, query_params, next_token)\n",
107+
" for tweet in json_response['data']:\n",
108+
" tweets.append(tweet)\n",
109+
" try:\n",
110+
" next_token = json_response['meta']['next_token']\n",
111+
" except KeyError:\n",
112+
" break\n",
113+
" time.sleep(2)\n",
114+
" return tweets\n",
115+
"\n",
116+
"\n",
117+
"\n",
118+
"def main():\n",
119+
" tweets = get_tweets(500)\n",
120+
" with open(out_file, 'w') as f:\n",
121+
" json.dump(tweets, f)\n",
122+
"\n",
123+
"\n",
124+
"\n",
125+
"if __name__ == \"__main__\":\n",
126+
" main()"
127+
]
128+
}
129+
],
130+
"metadata": {
131+
"kernelspec": {
132+
"display_name": "Python 3",
133+
"language": "python",
134+
"name": "python3"
135+
},
136+
"language_info": {
137+
"codemirror_mode": {
138+
"name": "ipython",
139+
"version": 3
140+
},
141+
"file_extension": ".py",
142+
"mimetype": "text/x-python",
143+
"name": "python",
144+
"nbconvert_exporter": "python",
145+
"pygments_lexer": "ipython3",
146+
"version": "3.8.8"
147+
}
148+
},
149+
"nbformat": 4,
150+
"nbformat_minor": 4
151+
}

0 commit comments

Comments
 (0)