forked from OpenBB-finance/OpenBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_api.py
279 lines (231 loc) · 13.2 KB
/
twitter_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import argparse
import requests
import pandas as pd
import re
import flair
import matplotlib.pyplot as plt
import iso8601
import dateutil
import config_terminal as cfg
from helper_funcs import *
# ------------------------------------------------- INFERENCE -------------------------------------------------
def inference(l_args, s_ticker):
parser = argparse.ArgumentParser(prog='infer',
description="""Print quick sentiment inference from last tweets that contain
the ticker. This model splits the text into character-level
tokens and uses the DistilBERT model to make predictions. DistilBERT is a distilled
version of the powerful BERT transformer model. Not only time period of these,
but also frequency. [Source: Twitter]""")
parser.add_argument('-n', "--num", action="store", dest="n_num", type=int, default=100, choices=range(10,101),
help='num of latest tweets to infer from.')
try:
(ns_parser, l_unknown_args) = parser.parse_known_args(l_args)
if l_unknown_args:
print(f"The following args couldn't be interpreted: {l_unknown_args}\n")
return
# Get tweets using Twitter API
params = {
'q': '$'+s_ticker,
'tweet_mode': 'extended',
'lang': 'en',
'count': str(ns_parser.n_num)
}
# Request Twitter API
response = requests.get('https://api.twitter.com/1.1/search/tweets.json',
params=params,
headers={'authorization': 'Bearer '+ cfg.API_TWITTER_BEARER_TOKEN})
# Create dataframe
df_tweets = pd.DataFrame()
# Check that the API response was successful
if response.status_code == 200:
for tweet in response.json()['statuses']:
row = get_data(tweet)
df_tweets = df_tweets.append(row, ignore_index=True)
# Load sentiment model
sentiment_model = flair.models.TextClassifier.load('en-sentiment')
print("")
# We will append probability/sentiment preds later
probs = []
sentiments = []
for s_tweet in df_tweets['text'].to_list():
tweet = clean_tweet(s_tweet, s_ticker)
# Make sentiment prediction
sentence = flair.data.Sentence(tweet)
sentiment_model.predict(sentence)
# Extract sentiment prediction (POSITIVE/NEGATIVE) and confidence (0-1)
probs.append(sentence.labels[0].score)
sentiments.append(sentence.labels[0].value)
# Add probability and sentiment predictions to tweets dataframe
df_tweets['probability'] = probs
df_tweets['sentiment'] = sentiments
# Add sentiment estimation (probability positive for POSITIVE sentiment, and negative for NEGATIVE sentiment)
df_tweets['sentiment_estimation'] = df_tweets.apply(lambda row: row['probability']*(-1, 1)[row['sentiment']=='POSITIVE'], axis=1).cumsum()
# Cumulative sentiment_estimation
df_tweets['prob_sen'] = df_tweets.apply(lambda row: row['probability']*(-1, 1)[row['sentiment']=='POSITIVE'], axis=1)
# Percentage of confidence
if df_tweets['sentiment_estimation'].values[-1] > 0:
n_pos = df_tweets[df_tweets['prob_sen']>0]['prob_sen'].sum()
n_pct = round(100*n_pos/df_tweets['probability'].sum())
else:
n_neg = abs(df_tweets[df_tweets['prob_sen']<0]['prob_sen'].sum())
n_pct = round(100*n_neg/df_tweets['probability'].sum())
# Parse tweets
dt_from = dateutil.parser.parse(df_tweets['created_at'].values[-1])
dt_to = dateutil.parser.parse(df_tweets['created_at'].values[0])
print(f"From: {dt_from.strftime('%Y-%m-%d %H:%M:%S')}")
print(f"To: {dt_to.strftime('%Y-%m-%d %H:%M:%S')}")
print(f"{len(df_tweets)} tweets were analyzed.")
dt_delta = (dt_to - dt_from)
n_freq = dt_delta.total_seconds()/len(df_tweets)
print(f"Frequency of approx 1 tweet every {round(n_freq)} seconds.")
s_sen = f"{('NEGATIVE', 'POSITIVE')[int(df_tweets['sentiment_estimation'].values[-1] > 0)]}"
s_conf = f"{round(100*df_tweets['sentiment_estimation'].values[-1]/df_tweets['probability'].sum())}%"
print(f"The sentiment of {s_ticker} is: {s_sen} ({s_conf})")
print("")
except:
print("")
# ------------------------------------------------- SENTIMENT -------------------------------------------------
def sentiment(l_args, s_ticker):
parser = argparse.ArgumentParser(prog='sen',
description="""Plot in-depth sentiment predicted from tweets from last days
that contain pre-defined ticker. This model splits the text into character-level
tokens and uses the DistilBERT model to make predictions. DistilBERT is a distilled
version of the powerful BERT transformer model. Note that a big num of tweets extracted per
hour in conjunction with a high number of days in the past, will make the
algorithm take a long period of time to estimate sentiment. [Source: Twitter]""")
# in reality this argument could be 100, but after testing it takes too long to compute which may not be acceptable
parser.add_argument('-n', "--num", action="store", dest="n_tweets", type=int, default=10, choices=range(10,61),
help='num of tweets to extract per hour.')
parser.add_argument('-d', "--days", action="store", dest="n_days_past", type=int, default=7, choices=range(1,8),
help='num of days in the past to extract tweets.')
try:
(ns_parser, l_unknown_args) = parser.parse_known_args(l_args)
if l_unknown_args:
print(f"The following args couldn't be interpreted: {l_unknown_args}\n")
return
# Setup API request params and headers
headers = {'authorization': f'Bearer {cfg.API_TWITTER_BEARER_TOKEN}'}
params = {
'query': f'({s_ticker}) (lang:en)',
'max_results': str(ns_parser.n_tweets),
'tweet.fields': 'created_at,lang'
}
# Date format string required by twitter
dtformat = '%Y-%m-%dT%H:%M:%SZ'
# Algorithm to extract
dt_recent = datetime.now() - timedelta(seconds=20)
dt_old = dt_recent - timedelta(days=ns_parser.n_days_past)
print(f"From {dt_recent.date()} retrieving {ns_parser.n_tweets*24} tweets ({ns_parser.n_tweets} tweets/hour)")
df_tweets = pd.DataFrame()
while True:
# Iterate until we haven't passed the old number of days
if dt_recent < dt_old:
break
# Update past datetime
dt_past = dt_recent - timedelta(minutes=60)
if dt_past.day < dt_recent.day:
print(f"From {dt_past.date()} retrieving {ns_parser.n_tweets*24} tweets ({ns_parser.n_tweets} tweets/hour)")
# Assign from and to datetime parameters for the API
params['start_time'] = dt_past.strftime(dtformat)
params['end_time'] = dt_recent.strftime(dtformat)
# Send API request
response = requests.get('https://api.twitter.com/2/tweets/search/recent',
params=params,
headers=headers)
# Update recent datetime
dt_recent = dt_past
# If response from API request is a success
if response.status_code == 200:
# Iteratively append our tweet data to our dataframe
for tweet in response.json()['data']:
row = get_data(tweet)
df_tweets = df_tweets.append(row, ignore_index=True)
# Load sentiment model
print("")
sentiment_model = flair.models.TextClassifier.load('en-sentiment');
print("")
# Append probability and sentiment preds later
probs = []
sentiments = []
for s_tweet in df_tweets['text'].to_list():
tweet = clean_tweet(s_tweet, s_ticker)
# Make sentiment prediction
sentence = flair.data.Sentence(tweet)
sentiment_model.predict(sentence)
# Extract sentiment prediction (POSITIVE/NEGATIVE) and confidence (0-1)
probs.append(sentence.labels[0].score)
sentiments.append(sentence.labels[0].value)
# Add probability and sentiment predictions to tweets dataframe
df_tweets['probability'] = probs
df_tweets['sentiment'] = sentiments
# Sort tweets per date
df_tweets.sort_index(ascending=False, inplace=True)
# Add sentiment estimation (probability positive for POSITIVE sentiment, and negative for NEGATIVE sentiment)
df_tweets['sentiment_estimation'] = df_tweets.apply(lambda row: row['probability']*(-1, 1)[row['sentiment']=='POSITIVE'], axis=1).cumsum()
# Cumulative sentiment_estimation
df_tweets['prob_sen'] = df_tweets.apply(lambda row: row['probability']*(-1, 1)[row['sentiment']=='POSITIVE'], axis=1)
# Percentage of confidence
if df_tweets['sentiment_estimation'].values[-1] > 0:
n_pos = df_tweets[df_tweets['prob_sen']>0]['prob_sen'].sum()
n_pct = round(100*n_pos/df_tweets['probability'].sum())
else:
n_neg = abs(df_tweets[df_tweets['prob_sen']<0]['prob_sen'].sum())
n_pct = round(100*n_neg/df_tweets['probability'].sum())
s_sen = f"{('NEGATIVE', 'POSITIVE')[int(df_tweets['sentiment_estimation'].values[-1] > 0)]}"
#df_tweets.to_csv(r'notebooks/tweets.csv', index=False)
df_tweets.reset_index(inplace=True)
# Plotting
plt.subplot(211)
plt.title(f"Twitter's {s_ticker} sentiment over time is {s_sen} ({n_pct} %)")
plt.plot(df_tweets.index, df_tweets['sentiment_estimation'].values, lw=3, c='cyan')
plt.xlim(df_tweets.index[0], df_tweets.index[-1])
plt.grid(b=True, which='major', color='#666666', linestyle='-', lw=1.5, alpha=0.5)
plt.minorticks_on()
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2)
plt.ylabel('Cumulative Sentiment')
l_xticks = list()
l_xlabels = list()
l_xticks.append(0)
l_xlabels.append(df_tweets['created_at'].values[0].split(' ')[0])
n_day = datetime.strptime(df_tweets['created_at'].values[0], "%Y-%m-%d %H:%M:%S").day
n_idx = 0
n_next_idx = 0
for n_next_idx, dt_created in enumerate(df_tweets['created_at']):
if datetime.strptime(dt_created, "%Y-%m-%d %H:%M:%S").day > n_day:
l_xticks.append(n_next_idx)
l_xlabels.append(df_tweets['created_at'].values[n_next_idx].split(' ')[0])
l_val_days = df_tweets['sentiment_estimation'].values[n_idx:n_next_idx]-df_tweets['sentiment_estimation'].values[n_idx]
plt.plot(range(n_idx, n_next_idx), l_val_days, lw=3, c='tab:blue')
n_day_avg = np.mean(l_val_days)
if n_day_avg > 0:
plt.hlines(n_day_avg, n_idx, n_next_idx, linewidth=2.5, linestyle='--', color='green', lw=3)
else:
plt.hlines(n_day_avg, n_idx, n_next_idx, linewidth=2.5, linestyle='--', color='red', lw=3)
n_idx = n_next_idx
n_day += 1
l_val_days = df_tweets['sentiment_estimation'].values[n_idx:]-df_tweets['sentiment_estimation'].values[n_idx]
plt.plot(range(n_idx, len(df_tweets)), l_val_days, lw=3, c='tab:blue')
n_day_avg = np.mean(l_val_days)
if n_day_avg > 0:
plt.hlines(n_day_avg, n_idx, len(df_tweets), linewidth=2.5, linestyle='--', color='green', lw=3)
else:
plt.hlines(n_day_avg, n_idx, len(df_tweets), linewidth=2.5, linestyle='--', color='red', lw=3)
l_xticks.append(len(df_tweets))
datetime.strptime(dt_created, "%Y-%m-%d %H:%M:%S") + timedelta(days=1)
l_xlabels.append(datetime.strftime(datetime.strptime(df_tweets['created_at'].values[len(df_tweets)-1], "%Y-%m-%d %H:%M:%S") + timedelta(days=1), "%Y-%m-%d"))
plt.xticks(l_xticks, l_xlabels)
plt.axhspan(plt.gca().get_ylim()[0], 0, facecolor='r', alpha=0.1)
plt.axhspan(0, plt.gca().get_ylim()[1], facecolor='g', alpha=0.1)
plt.subplot(212)
plt.bar(df_tweets[df_tweets['prob_sen']>0].index, df_tweets[df_tweets['prob_sen']>0]['prob_sen'].values, color='green')
plt.bar(df_tweets[df_tweets['prob_sen']<0].index, df_tweets[df_tweets['prob_sen']<0]['prob_sen'].values, color='red')
for l_x in l_xticks[1:]:
plt.vlines(l_x, -1, 1, linewidth=2, linestyle='--', color='k', lw=3)
plt.xlim(df_tweets.index[0], df_tweets.index[-1])
plt.xticks(l_xticks, l_xlabels)
plt.grid(b=True, which='major', color='#666666', linestyle='-')
plt.ylabel('Sentiment')
plt.xlabel("Time")
plt.show()
except:
print("")