import os
print(os.environ["BEARER_TOKEN"])
import os
import json
import requests
import pandas as pd
def auth():
return os.environ["BEARER_TOKEN"]
def create_url(query, max_results, next_token):
query = query
max_results = '100'
url = "https://api.twitter.com/2/tweets/search/recent?query={}&tweet.fields=author_id,lang,created_at&max_results={}".format(query, max_results)
if next_token:
url = "https://api.twitter.com/2/tweets/search/recent?query={}&tweet.fields=author_id,lang,created_at&max_results={}&next_token={}".format(query, max_results, next_token)
return url
def create_headers(bearer_token):
headers = {"Authorization": "Bearer {}".format(bearer_token)}
return headers
def connect_to_endpoint(url, headers):
response = requests.request("GET", url, headers=headers)
if response.status_code != 200:
raise Exception(response.status_code, response.text)
return response.json()
def get_tweets(query, max_results, next_token=None):
bearer_token = auth()
url = create_url(query, max_results, next_token)
headers = create_headers(bearer_token)
json_response = connect_to_endpoint(url, headers)
return json.dumps(json_response, indent=4, sort_keys=True)
query = "Arsenal Edu lang:en -is:retweet"
limit = "100"
tweet_list = []
next_token = ""
for i in range(20):
data = get_tweets(
query,
limit,
next_token
)
data = json.loads(data)
next_token = data['meta']['next_token']
tweet_list += data['data']
tweets_df = pd.DataFrame(tweet_list)
tweets_df
from textblob import TextBlob
def clean(tweet):
return str(tweet).encode('ascii', 'ignore').decode('UTF-8')
def get_subjectivity(tweet):
return round(TextBlob(tweet).sentiment.subjectivity, 2)
def get_polarity(tweet):
return round(TextBlob(tweet).sentiment.polarity, 2)
tweets_df['text'] = tweets_df['text'].apply(clean)
tweets_df['subjectivity'] = tweets_df['text'].apply(get_subjectivity)
tweets_df['polarity'] = tweets_df['text'].apply(get_polarity)
tweets_df.drop(tweets_df[tweets_df['polarity'] == 0].index, inplace=True)
tweets_df.drop_duplicates(subset='text', keep="first", inplace=True)
tweets_df