Skip to content

Commit 65fe298

Browse files
committed
Update 024 Bots - Building an OpenAI Twitter News Bot with Python.ipynb
1 parent 77dbe01 commit 65fe298

File tree

1 file changed

+172
-81
lines changed

1 file changed

+172
-81
lines changed

024 Bots - Building an OpenAI Twitter News Bot with Python.ipynb

Lines changed: 172 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,32 @@
99
},
1010
{
1111
"cell_type": "code",
12-
"execution_count": 6,
12+
"execution_count": 9,
1313
"metadata": {},
1414
"outputs": [],
1515
"source": [
1616
"# A tutorial for this file is available at www.relataly.com\n",
17-
"# Tested with Python 3.9.13, 1.2.0, Pandas 1.3.4, OpenAI 0.27.3, Tweepy 4.13.0, Requests 2.26.0, Azure.identity 1.7.0, azure.keyvault.secrets 4.6.0\n",
17+
"# Tested with Python 3.9.13, 1.2.0, Pandas 1.3.4, OpenAI 0.27.3, Tweepy 4.13.0, Requests 2.26.0, \n",
1818
"\n",
1919
"import logging\n",
2020
"import pandas as pd\n",
2121
"import openai\n",
2222
"import tweepy\n",
2323
"import csv\n",
2424
"import requests\n",
25-
"from azure.keyvault.secrets import SecretClient\n",
26-
"from azure.identity import AzureCliCredential\n",
2725
"\n",
2826
"# Set API Keys and Authentification\n",
29-
"logging.info('Setting Key Vault Auth')\n",
30-
"client = SecretClient(f\"https://{'keyvaultforbot'}.vault.azure.net/\", AzureCliCredential())\n",
31-
"\n",
3227
"logging.info('Setting NewsAPI API Key')\n",
33-
"NEWSAPI_API_KEY = client.get_secret('newsapi-api-key').value\n",
28+
"NEWSAPI_API_KEY = <your API key> # replace with own API key\n",
3429
"\n",
3530
"logging.info('Setting OpenAI API Key')\n",
36-
"OPENAI_API_KEY = client.get_secret('openai-api-key').value\n",
37-
"openai.api_key = OPENAI_API_KEY\n",
31+
"openai.api_key = <your API key> # replace with own API key\n",
3832
"\n",
3933
"logging.info('Setting Twitter API Key')\n",
40-
"auth=tweepy.OAuthHandler(client.get_secret('twitter-api-key').value,\n",
41-
" client.get_secret('twitter-api-secret').value)\n",
42-
"auth.set_access_token(client.get_secret('twitter-access-token').value,\n",
43-
" client.get_secret('twitter-access-secret').value)\n",
34+
"auth=tweepy.OAuthHandler(<your API key>,\n",
35+
" <your API secret>)\n",
36+
"auth.set_access_token(<your access token>,\n",
37+
" <your access secret>)\n",
4438
"twitter_api=tweepy.API(auth)\n",
4539
"\n",
4640
"CSV_NAME = 'news_log.csv'"
@@ -55,7 +49,7 @@
5549
},
5650
{
5751
"cell_type": "code",
58-
"execution_count": 7,
52+
"execution_count": 10,
5953
"metadata": {},
6054
"outputs": [],
6155
"source": [
@@ -79,7 +73,7 @@
7973
},
8074
{
8175
"cell_type": "code",
82-
"execution_count": 8,
76+
"execution_count": 11,
8377
"metadata": {},
8478
"outputs": [],
8579
"source": [
@@ -127,7 +121,7 @@
127121
},
128122
{
129123
"cell_type": "code",
130-
"execution_count": 9,
124+
"execution_count": 12,
131125
"metadata": {},
132126
"outputs": [],
133127
"source": [
@@ -174,7 +168,7 @@
174168
},
175169
{
176170
"cell_type": "code",
177-
"execution_count": 10,
171+
"execution_count": 13,
178172
"metadata": {},
179173
"outputs": [
180174
{
@@ -207,114 +201,114 @@
207201
" <tbody>\n",
208202
" <tr>\n",
209203
" <th>0</th>\n",
210-
" <td>6 Things Horizon 3 Needs to Succeed - Beyond C...</td>\n",
211-
" <td>In this clip of Podcast Beyond, Jada Griffin, ...</td>\n",
212-
" <td>https://www.youtube.com/watch?v=2eHTGrobyEg</td>\n",
204+
" <td>The iPhone 14 Pro is a demonstration of what o...</td>\n",
205+
" <td>Apple’s iPhone 14 Pro is getting more useful w...</td>\n",
206+
" <td>https://www.theverge.com/2023/5/5/23711698/iph...</td>\n",
213207
" <td>False</td>\n",
214208
" </tr>\n",
215209
" <tr>\n",
216210
" <th>1</th>\n",
217-
" <td>MMORPG Chrono Odyssey 'Gameplay Reveal' traile...</td>\n",
218-
" <td>South Korea-based game developer NPIXEL has re...</td>\n",
219-
" <td>https://www.gematsu.com/2023/05/mmorpg-chrono-...</td>\n",
211+
" <td>Huge update is launched for billions of Gmail ...</td>\n",
212+
" <td>Google has unveiled a huge update that signals...</td>\n",
213+
" <td>https://www.dailymail.co.uk/sciencetech/articl...</td>\n",
220214
" <td>False</td>\n",
221215
" </tr>\n",
222216
" <tr>\n",
223217
" <th>2</th>\n",
224-
" <td>Google is replacing Chrome’s misleading lock i...</td>\n",
225-
" <td>Google announced that the lock will be replace...</td>\n",
226-
" <td>https://www.theverge.com/2023/5/3/23709498/goo...</td>\n",
218+
" <td>JBL Tour Pro 2 review: Welcome to the smart ca...</td>\n",
219+
" <td>JBL threw a ton of features in the Tour Pro 2 ...</td>\n",
220+
" <td>https://www.engadget.com/jbl-tour-pro-2-review...</td>\n",
227221
" <td>False</td>\n",
228222
" </tr>\n",
229223
" <tr>\n",
230224
" <th>3</th>\n",
231-
" <td>The new Pokémon Trading Card Game app is final...</td>\n",
232-
" <td>Pokémon Trading Card Game Live will launch on ...</td>\n",
233-
" <td>https://www.theverge.com/2023/5/3/23709584/pok...</td>\n",
225+
" <td>Tech Issues Are Plaguing Next-Gen Games - Next...</td>\n",
226+
" <td>In this week's monster-sized episode of Next-G...</td>\n",
227+
" <td>https://www.youtube.com/watch?v=WqF9V1dwBW0</td>\n",
234228
" <td>False</td>\n",
235229
" </tr>\n",
236230
" <tr>\n",
237231
" <th>4</th>\n",
238-
" <td>Apple and Google are teaming up on a plan to m...</td>\n",
239-
" <td>Apple and Google are working together on a new...</td>\n",
240-
" <td>https://www.cnn.com/2023/05/03/tech/airtags-ap...</td>\n",
232+
" <td>Xbox boss raises eyebrows with claim Microsoft...</td>\n",
233+
" <td>As the fallout from Redfall's release continue...</td>\n",
234+
" <td>https://www.eurogamer.net/xbox-boss-raises-eye...</td>\n",
241235
" <td>False</td>\n",
242236
" </tr>\n",
243237
" <tr>\n",
244238
" <th>5</th>\n",
245-
" <td>Researchers See Through a Mouse's Eyes by Deco...</td>\n",
246-
" <td>Don't worry, the technology can't be used to r...</td>\n",
247-
" <td>https://gizmodo.com/ai-deep-learning-brain-act...</td>\n",
248-
" <td>True</td>\n",
239+
" <td>May 2023 update lands on Galaxy S22, starting ...</td>\n",
240+
" <td>Samsung is one week into the May 2023 security...</td>\n",
241+
" <td>https://www.sammobile.com/news/may-2023-update...</td>\n",
242+
" <td>False</td>\n",
249243
" </tr>\n",
250244
" <tr>\n",
251245
" <th>6</th>\n",
252-
" <td>F1 23 release date announced with Braking Poin...</td>\n",
253-
" <td>F1 2023 launches June 16, 2023 for PlayStation...</td>\n",
254-
" <td>https://www.polygon.com/23709519/f1-23-game-re...</td>\n",
246+
" <td>What to expect at Google I/O 2023 - Engadget</td>\n",
247+
" <td>Google is set to introduce Android 14 and new ...</td>\n",
248+
" <td>https://www.engadget.com/what-to-expect-at-goo...</td>\n",
255249
" <td>False</td>\n",
256250
" </tr>\n",
257251
" <tr>\n",
258252
" <th>7</th>\n",
259-
" <td>iOS 17 Coming Soon for iPhones and Rumored to ...</td>\n",
260-
" <td>Apple is expected to announce iOS 17 during it...</td>\n",
261-
" <td>https://www.macrumors.com/2023/05/03/ios-17-ru...</td>\n",
253+
" <td>PSA: you've got just one weekend left to claim...</td>\n",
254+
" <td>You’ve got until May 9th to claim the dozen-pl...</td>\n",
255+
" <td>https://www.theverge.com/2023/5/5/23712262/son...</td>\n",
262256
" <td>False</td>\n",
263257
" </tr>\n",
264258
" <tr>\n",
265259
" <th>8</th>\n",
266-
" <td>Pokémon GO's Monthly Earnings Have Fallen To A...</td>\n",
267-
" <td>Only $34.7 million, the horror</td>\n",
268-
" <td>https://www.nintendolife.com/news/2023/05/poke...</td>\n",
260+
" <td>How to Lock Specific iPhone Apps Behind Face I...</td>\n",
261+
" <td>In the interests of enhanced privacy, some thi...</td>\n",
262+
" <td>https://www.macrumors.com/how-to/automatically...</td>\n",
269263
" <td>False</td>\n",
270264
" </tr>\n",
271265
" <tr>\n",
272266
" <th>9</th>\n",
273-
" <td>Double Dragon Gaiden: Rise of the Dragons - Of...</td>\n",
274-
" <td>Watch the announcement trailer for Double Drag...</td>\n",
275-
" <td>https://www.youtube.com/watch?v=MksA6p7rqOM</td>\n",
276-
" <td>False</td>\n",
267+
" <td>Microsoft is allegedly working with AMD to exp...</td>\n",
268+
" <td>Microsoft is allegedly providing engineering r...</td>\n",
269+
" <td>https://www.theverge.com/2023/5/5/23712242/mic...</td>\n",
270+
" <td>True</td>\n",
277271
" </tr>\n",
278272
" </tbody>\n",
279273
"</table>\n",
280274
"</div>"
281275
],
282276
"text/plain": [
283277
" title \\\n",
284-
"0 6 Things Horizon 3 Needs to Succeed - Beyond C... \n",
285-
"1 MMORPG Chrono Odyssey 'Gameplay Reveal' traile... \n",
286-
"2 Google is replacing Chrome’s misleading lock i... \n",
287-
"3 The new Pokémon Trading Card Game app is final... \n",
288-
"4 Apple and Google are teaming up on a plan to m... \n",
289-
"5 Researchers See Through a Mouse's Eyes by Deco... \n",
290-
"6 F1 23 release date announced with Braking Poin... \n",
291-
"7 iOS 17 Coming Soon for iPhones and Rumored to ... \n",
292-
"8 Pokémon GO's Monthly Earnings Have Fallen To A... \n",
293-
"9 Double Dragon Gaiden: Rise of the Dragons - Of... \n",
278+
"0 The iPhone 14 Pro is a demonstration of what o... \n",
279+
"1 Huge update is launched for billions of Gmail ... \n",
280+
"2 JBL Tour Pro 2 review: Welcome to the smart ca... \n",
281+
"3 Tech Issues Are Plaguing Next-Gen Games - Next... \n",
282+
"4 Xbox boss raises eyebrows with claim Microsoft... \n",
283+
"5 May 2023 update lands on Galaxy S22, starting ... \n",
284+
"6 What to expect at Google I/O 2023 - Engadget \n",
285+
"7 PSA: you've got just one weekend left to claim... \n",
286+
"8 How to Lock Specific iPhone Apps Behind Face I... \n",
287+
"9 Microsoft is allegedly working with AMD to exp... \n",
294288
"\n",
295289
" description \\\n",
296-
"0 In this clip of Podcast Beyond, Jada Griffin, ... \n",
297-
"1 South Korea-based game developer NPIXEL has re... \n",
298-
"2 Google announced that the lock will be replace... \n",
299-
"3 Pokémon Trading Card Game Live will launch on ... \n",
300-
"4 Apple and Google are working together on a new... \n",
301-
"5 Don't worry, the technology can't be used to r... \n",
302-
"6 F1 2023 launches June 16, 2023 for PlayStation... \n",
303-
"7 Apple is expected to announce iOS 17 during it... \n",
304-
"8 Only $34.7 million, the horror \n",
305-
"9 Watch the announcement trailer for Double Drag... \n",
290+
"0 Apple’s iPhone 14 Pro is getting more useful w... \n",
291+
"1 Google has unveiled a huge update that signals... \n",
292+
"2 JBL threw a ton of features in the Tour Pro 2 ... \n",
293+
"3 In this week's monster-sized episode of Next-G... \n",
294+
"4 As the fallout from Redfall's release continue... \n",
295+
"5 Samsung is one week into the May 2023 security... \n",
296+
"6 Google is set to introduce Android 14 and new ... \n",
297+
"7 You’ve got until May 9th to claim the dozen-pl... \n",
298+
"8 In the interests of enhanced privacy, some thi... \n",
299+
"9 Microsoft is allegedly providing engineering r... \n",
306300
"\n",
307301
" url relevance \n",
308-
"0 https://www.youtube.com/watch?v=2eHTGrobyEg False \n",
309-
"1 https://www.gematsu.com/2023/05/mmorpg-chrono-... False \n",
310-
"2 https://www.theverge.com/2023/5/3/23709498/goo... False \n",
311-
"3 https://www.theverge.com/2023/5/3/23709584/pok... False \n",
312-
"4 https://www.cnn.com/2023/05/03/tech/airtags-ap... False \n",
313-
"5 https://gizmodo.com/ai-deep-learning-brain-act... True \n",
314-
"6 https://www.polygon.com/23709519/f1-23-game-re... False \n",
315-
"7 https://www.macrumors.com/2023/05/03/ios-17-ru... False \n",
316-
"8 https://www.nintendolife.com/news/2023/05/poke... False \n",
317-
"9 https://www.youtube.com/watch?v=MksA6p7rqOM False "
302+
"0 https://www.theverge.com/2023/5/5/23711698/iph... False \n",
303+
"1 https://www.dailymail.co.uk/sciencetech/articl... False \n",
304+
"2 https://www.engadget.com/jbl-tour-pro-2-review... False \n",
305+
"3 https://www.youtube.com/watch?v=WqF9V1dwBW0 False \n",
306+
"4 https://www.eurogamer.net/xbox-boss-raises-eye... False \n",
307+
"5 https://www.sammobile.com/news/may-2023-update... False \n",
308+
"6 https://www.engadget.com/what-to-expect-at-goo... False \n",
309+
"7 https://www.theverge.com/2023/5/5/23712262/son... False \n",
310+
"8 https://www.macrumors.com/how-to/automatically... False \n",
311+
"9 https://www.theverge.com/2023/5/5/23712242/mic... True "
318312
]
319313
},
320314
"metadata": {},
@@ -324,7 +318,9 @@
324318
"name": "stdout",
325319
"output_type": "stream",
326320
"text": [
327-
"Already tweeted: Researchers See Through a Mouse's Eyes by Decoding Brain Signals - Gizmodo\n"
321+
"Tweet too long: 335\n",
322+
"error tweet too long\n",
323+
"Tweeted: Microsoft is allegedly working with AMD to expand into AI chips - The Verge\n"
328324
]
329325
}
330326
],
@@ -380,6 +376,101 @@
380376
"\n",
381377
"main_bot()"
382378
]
379+
},
380+
{
381+
"cell_type": "code",
382+
"execution_count": 14,
383+
"metadata": {},
384+
"outputs": [
385+
{
386+
"data": {
387+
"text/html": [
388+
"<div>\n",
389+
"<style scoped>\n",
390+
" .dataframe tbody tr th:only-of-type {\n",
391+
" vertical-align: middle;\n",
392+
" }\n",
393+
"\n",
394+
" .dataframe tbody tr th {\n",
395+
" vertical-align: top;\n",
396+
" }\n",
397+
"\n",
398+
" .dataframe thead th {\n",
399+
" text-align: right;\n",
400+
" }\n",
401+
"</style>\n",
402+
"<table border=\"1\" class=\"dataframe\">\n",
403+
" <thead>\n",
404+
" <tr style=\"text-align: right;\">\n",
405+
" <th></th>\n",
406+
" <th>title</th>\n",
407+
" </tr>\n",
408+
" </thead>\n",
409+
" <tbody>\n",
410+
" <tr>\n",
411+
" <th>0</th>\n",
412+
" <td>Researchers See Through a Mouse's Eyes by Deco...</td>\n",
413+
" </tr>\n",
414+
" <tr>\n",
415+
" <th>1</th>\n",
416+
" <td>Google DeepMind CEO says human-level AI will b...</td>\n",
417+
" </tr>\n",
418+
" <tr>\n",
419+
" <th>2</th>\n",
420+
" <td>AMD jumps 9% on report Microsoft is collaborat...</td>\n",
421+
" </tr>\n",
422+
" <tr>\n",
423+
" <th>3</th>\n",
424+
" <td>Next week's Google I/O event may change Samsun...</td>\n",
425+
" </tr>\n",
426+
" <tr>\n",
427+
" <th>4</th>\n",
428+
" <td>Microsoft barrels ahead with AI plans, opens u...</td>\n",
429+
" </tr>\n",
430+
" <tr>\n",
431+
" <th>5</th>\n",
432+
" <td>'Godfather of artificial intelligence' says AI...</td>\n",
433+
" </tr>\n",
434+
" <tr>\n",
435+
" <th>6</th>\n",
436+
" <td>Microsoft is allegedly working with AMD to exp...</td>\n",
437+
" </tr>\n",
438+
" <tr>\n",
439+
" <th>7</th>\n",
440+
" <td>katze</td>\n",
441+
" </tr>\n",
442+
" </tbody>\n",
443+
"</table>\n",
444+
"</div>"
445+
],
446+
"text/plain": [
447+
" title\n",
448+
"0 Researchers See Through a Mouse's Eyes by Deco...\n",
449+
"1 Google DeepMind CEO says human-level AI will b...\n",
450+
"2 AMD jumps 9% on report Microsoft is collaborat...\n",
451+
"3 Next week's Google I/O event may change Samsun...\n",
452+
"4 Microsoft barrels ahead with AI plans, opens u...\n",
453+
"5 'Godfather of artificial intelligence' says AI...\n",
454+
"6 Microsoft is allegedly working with AMD to exp...\n",
455+
"7 katze"
456+
]
457+
},
458+
"execution_count": 14,
459+
"metadata": {},
460+
"output_type": "execute_result"
461+
}
462+
],
463+
"source": [
464+
"\n",
465+
"CSV_NAME = 'news_log.csv'\n",
466+
"df_old = pd.read_csv(CSV_NAME)\n",
467+
"import pandas as pd\n",
468+
"title = 'katze'\n",
469+
"df = pd.DataFrame({'title': [title]})\n",
470+
"df\n",
471+
"df_old = pd.concat([df_old, df], ignore_index=True)\n",
472+
"df_old"
473+
]
383474
}
384475
],
385476
"metadata": {

0 commit comments

Comments
 (0)