Datasets:
Upload 01_API_Data.ipynb
Browse files- 01 Codes/01_API_Data.ipynb +121 -0
01 Codes/01_API_Data.ipynb
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"> Notebook to download data using twitter's API"
|
| 8 |
+
]
|
| 9 |
+
},
|
| 10 |
+
{
|
| 11 |
+
"cell_type": "code",
|
| 12 |
+
"execution_count": 1,
|
| 13 |
+
"metadata": {},
|
| 14 |
+
"outputs": [],
|
| 15 |
+
"source": [
|
| 16 |
+
"import tweepy\n",
|
| 17 |
+
"import os\n",
|
| 18 |
+
"import csv\n",
|
| 19 |
+
"import pandas as pd"
|
| 20 |
+
]
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"cell_type": "code",
|
| 24 |
+
"execution_count": 2,
|
| 25 |
+
"metadata": {},
|
| 26 |
+
"outputs": [],
|
| 27 |
+
"source": [
|
| 28 |
+
"# Saved the API Keys in the environment variables\n",
|
| 29 |
+
"consumer_key = os.getenv(\"TWITTER_CONSUMER_KEY\")\n",
|
| 30 |
+
"consumer_secret = os.getenv(\"TWITTER_CONSUMER_SECRET\")\n",
|
| 31 |
+
"access_token = os.getenv(\"TWITTER_ACCESS_TOKEN\")\n",
|
| 32 |
+
"access_token_secret = os.getenv(\"TWITTER_ACCESS_TOKEN_SECRET\")\n",
|
| 33 |
+
"bearer_token = os.getenv(\"BEARER_TOKEN\")"
|
| 34 |
+
]
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
"cell_type": "code",
|
| 38 |
+
"execution_count": 3,
|
| 39 |
+
"metadata": {},
|
| 40 |
+
"outputs": [],
|
| 41 |
+
"source": [
|
| 42 |
+
"import requests\n",
|
| 43 |
+
"import json\n",
|
| 44 |
+
"from requests_oauthlib import OAuth1\n",
|
| 45 |
+
"import urllib\n",
|
| 46 |
+
"\n",
|
| 47 |
+
"\n",
|
| 48 |
+
"auth = OAuth1(consumer_key, consumer_secret, access_token, access_token_secret)"
|
| 49 |
+
]
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"cell_type": "code",
|
| 53 |
+
"execution_count": 4,
|
| 54 |
+
"metadata": {},
|
| 55 |
+
"outputs": [
|
| 56 |
+
{
|
| 57 |
+
"name": "stdout",
|
| 58 |
+
"output_type": "stream",
|
| 59 |
+
"text": [
|
| 60 |
+
"https://api.twitter.com/2/tweets/search/recent?query=%23AppleVisionPro-is:retweets&tweet.fields=author_id,created_at&max_results=10000\n"
|
| 61 |
+
]
|
| 62 |
+
}
|
| 63 |
+
],
|
| 64 |
+
"source": [
|
| 65 |
+
"def create_url():\n",
|
| 66 |
+
" query = urllib.parse.quote(\"#AppleVisionPro\") + \"-is:retweets\"\n",
|
| 67 |
+
" tweet_fields = \"tweet.fields=author_id,created_at\"\n",
|
| 68 |
+
" max_results = \"max_results=10000\"\n",
|
| 69 |
+
" url = \"https://api.twitter.com/2/tweets/search/recent?query={}&{}&{}\".format(\n",
|
| 70 |
+
" query, tweet_fields, max_results\n",
|
| 71 |
+
" )\n",
|
| 72 |
+
" print(url)\n",
|
| 73 |
+
" return url\n",
|
| 74 |
+
"\n",
|
| 75 |
+
"\n",
|
| 76 |
+
"def connect_to_endpoint(url):\n",
|
| 77 |
+
" headers = {\n",
|
| 78 |
+
" \"Authorization\": \"Bearer \" + bearer_token,\n",
|
| 79 |
+
" \"Content-Type\": \"application/json\",\n",
|
| 80 |
+
" }\n",
|
| 81 |
+
" response = requests.get(url, headers=headers, auth=auth)\n",
|
| 82 |
+
" # print(response)\n",
|
| 83 |
+
" return response.json()\n",
|
| 84 |
+
"\n",
|
| 85 |
+
"\n",
|
| 86 |
+
"def main():\n",
|
| 87 |
+
" url = create_url()\n",
|
| 88 |
+
" json_response = connect_to_endpoint(url)\n",
|
| 89 |
+
" # Create a DataFrame from the JSON response\n",
|
| 90 |
+
" df = pd.json_normalize(json_response)\n",
|
| 91 |
+
" # Save the DataFrame as a Parquet file\n",
|
| 92 |
+
" df.to_parquet(\"avp_tweets.parquet.gzip\", compression=\"gzip\")\n",
|
| 93 |
+
"\n",
|
| 94 |
+
"\n",
|
| 95 |
+
"if __name__ == \"__main__\":\n",
|
| 96 |
+
" main()"
|
| 97 |
+
]
|
| 98 |
+
}
|
| 99 |
+
],
|
| 100 |
+
"metadata": {
|
| 101 |
+
"kernelspec": {
|
| 102 |
+
"display_name": "Python 3",
|
| 103 |
+
"language": "python",
|
| 104 |
+
"name": "python3"
|
| 105 |
+
},
|
| 106 |
+
"language_info": {
|
| 107 |
+
"codemirror_mode": {
|
| 108 |
+
"name": "ipython",
|
| 109 |
+
"version": 3
|
| 110 |
+
},
|
| 111 |
+
"file_extension": ".py",
|
| 112 |
+
"mimetype": "text/x-python",
|
| 113 |
+
"name": "python",
|
| 114 |
+
"nbconvert_exporter": "python",
|
| 115 |
+
"pygments_lexer": "ipython3",
|
| 116 |
+
"version": "3.10.13"
|
| 117 |
+
}
|
| 118 |
+
},
|
| 119 |
+
"nbformat": 4,
|
| 120 |
+
"nbformat_minor": 2
|
| 121 |
+
}
|