-
Notifications
You must be signed in to change notification settings - Fork 173
/
WallStreetBets.py
294 lines (230 loc) · 10.5 KB
/
WallStreetBets.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 3 16:19:57 2021
@author: Administrator
"""
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: filetype=python
#if wallstreetbets can move the market
#why not join em?
#fuck investment banks and hedge funds
#they only make the rich richer
#this script scrapes the topics under different flairs
#we only care about the hottest within the past 24 hours
#im not willing to use any stemmer or lemmatizer here
#simply becuz i dont want any ticker code gets fucked over
#if u want any nlp cleansing, check the link below
# https://github.com/je-suis-tm/machine-learning/blob/master/naive%20bayes.ipynb
import logging
import time
import datetime as dt
import pandas as pd
from bs4 import BeautifulSoup as bs
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
from wordcloud import WordCloud
import win32com.client as win32
import requests
import traceback
global stopword_dict
global punctuations
global commodities_of_interests
punctuations= ['!', '(', ')', '[', ']', '{', '}', ';', ':', "'", '"',
'\\', ',', '<', '>', '.', '/', '?', '@', '#', '%', '^',
'&', '*', '_', '~',]
#remove swearing words
stopword_dict=punctuations+['i','yolo', 'fuck', 'fucking', 'shit',
'take', 'still', 'new', 'say', 'get',
'add', 'update', 'me', 'my', 'myself',
'we', 'our', 'ours', 'ourselves', 'be',
'you', "you're", "you've", "you'll",
"you'd", 'your', 'yours', 'yourself',
'yourselves', 'he', 'him', 'his', 'were',
'himself', 'she', "she's", 'her', 'been',
'hers', 'herself', 'it', "it's", 'being',
'its', 'itself', 'they', 'them', 'their',
'theirs', 'themselves', 'what', 'which',
'who', 'whom', 'this', 'that', "that'll",
'these', 'those', 'am', 'is', 'are', 'was',
'have', 'has', 'had', 'having', 'do',
'does', 'did', 'doing', 'a', 'an', 'the',
'and', 'but', 'if', 'or', 'because', 'as',
'until', 'while', 'of', 'at', 'by', 'for',
'with', 'about', 'against', 'between',
'into', 'through', 'during', 'before',
'after', 'above', 'below', 'to', 'from',
'up', 'down', 'in', 'out', 'on', 'off',
'over', 'under', 'again', 'further',
'then', 'once', 'here', 'there', 'when',
'where', 'why', 'how', 'all', 'any',
'both', 'each', 'few', 'more', 'most',
'other', 'some', 'such', 'no', 'nor',
'not', 'only', 'own', 'same', 'so', 'than',
'too', 'very', 's', 't', 'can', 'will',
'just', 'don', "don't", 'should',
"should've", 'now', 'd', 'll', 'm', 'o',
're', 've', 'y', 'ain', 'aren', "aren't",
'couldn', "couldn't", 'didn', "didn't",
'doesn', "doesn't", 'hadn', "hadn't",
'hasn', "hasn't", 'haven', "haven't",
'isn', "isn't", 'ma', 'mightn', "mightn't",
'mustn', "mustn't", 'needn', "needn't",
'shan', "shan't", 'shouldn', "shouldn't",
'wasn', "wasn't", 'weren', "weren't",
'won','other','others', "won't",'guys',
'another','many','much', 'wouldn','guy',
'go', "wouldn't",'retard','retards',
'dick','dickhead','bitch','porn',
'asshole','pussy','cock']
commodities_of_interests=['wheat','soybean','corn','milk','cheese',
'butter','whey','lean hog','live cattle',
'lumber','pork','cocoa','sugar','coffee',
'cotton','diesel','ethanol','natural gas',
'coal','gasoline','gasoil','methanol',
'urea','rough rice','oats','palm oil',
'lead','carbon','greenhouse gas','freight',
'baltic','iron ore','steel','aluminium',
'aluminum','copper','gold','silver','brent',
'wti','henry hub','uranium','cobalt',
'nickel','zinc','palladium','platinum',
'propane','naphtha','fuel oil']
def scraping_data(session):
"""scraping"""
logger = logging.getLogger('scraping starts')
flairs=['DD','Discussion',
'Chart','YOLO',
'"Earnings%20Thread"',
'Gain','Loss','News']
threads=[]
pages={}
for flair in flairs:
url=f'https://new.reddit.com/r/wallstreetbets/search?sort=hot&restrict_sr=on&q=flair%3A{flair}&t=day'
logger.debug(f'scraping {flair}')
time.sleep(5)
response=session.get(url,verify=False)
page=bs(response.content,'html.parser')
pages[url]=page
threads+=[i.text for i in page.find_all('span', attrs={'style':"font-weight:normal"})]
return threads
def create_wordcloud(text):
"""draw wordcloud"""
#use shape
mask=np.array(Image.open('silhouette.jpg'))
wordcloud=WordCloud(mask=mask,
#to draw the boundary
#contour_width=3,contour_color='grey',
background_color='white',
#color_func=image_colors,
colormap='gist_heat',
stopwords=stopword_dict,
height=900,
width=1200,
).generate(text)
ax=plt.figure(figsize=(12,9)).add_subplot(111)
#display the image of word cloud
plt.imshow(wordcloud)
#remove axis
plt.axis("off")
plt.savefig('output.png')
def create_df_from_dict(potential):
"""create df from dict"""
if len(potential)==0:
return pd.DataFrame()
#make sure each value has the same length
maxlen=max([len(potential[i]) for i in potential])
for i in potential:
if len(potential[i])!=maxlen:
potential[i]+=['']*(maxlen-len(potential[i]))
return pd.DataFrame().from_dict(potential)
def main():
""" main """
logger = logging.getLogger()
session=requests.Session()
threads=scraping_data(session)
logger.debug("prepare for wordcloud")
#etl
rawtext=''.join(threads)
cleantext=[i for i in rawtext.split(' ') if i.lower() not in stopword_dict]
#cleanse
potential_tickers={}
potential_commodities={}
for ind,val in enumerate(cleantext):
#remove punctuations
for j in punctuations:
if j in val:
cleantext[ind]=val.replace(j,'')
#remove stopword
if cleantext[ind].lower() in stopword_dict:
cleantext[ind]=''
#ticker starts with $
if val[0]=='$' and not val[1].isdigit():
potential_tickers[val]=[]
#find commodities of interests
for ii in commodities_of_interests:
if ii in val.lower():
potential_commodities[ii]=[]
#find the context
for ind,val in enumerate(threads):
for j in potential_commodities:
if j in val.lower():
potential_commodities[j].append(val)
for j in potential_tickers:
if j in val:
potential_tickers[j].append(val)
logger.debug("create output")
#count freq
lexicons=set([i for i in cleantext])
D={}
for word in lexicons:
D[word]=cleantext.count(word)
#create wordcount
df=pd.DataFrame()
df['word']=D.keys()
df['count']=D.values()
df.sort_values('count',inplace=True,ascending=False)
#create context finder
df_commodities=create_df_from_dict(potential_commodities)
df_tickers=create_df_from_dict(potential_tickers)
#concatenate
writer=pd.ExcelWriter('output.xlsx')
df_commodities.to_excel(writer,
sheet_name='potential commodities',
index=False)
df_tickers.to_excel(writer,sheet_name='potential tickers',
index=False)
df.to_excel(writer,sheet_name='word count',
index=False)
writer.save()
logger.debug("wordcloud")
processed=' '.join(cleantext)
create_wordcloud(processed)
#cleanse text
text_commodities=', '.join([i.title() for i in potential_commodities])
text_tickers=', '.join([i.upper() for i in potential_tickers])
#create html
row1=f"""*Commodities Mentioned: <font color="red">{text_commodities}</font>"""
row2=f"""*Tickers Mentioned: <font color="red">{text_tickers}</font>"""
disclaimer='*Please check the spreadsheet attached for the exact context of the mentioning.'
image="""<img width=800 height=600 id="1" src="cid:output.png">"""
html=f"""<p>{row1}</p><p>{row2}</p><br>{image}<br><p>{disclaimer}</p>"""
files=['output.png','output.xlsx']
#send email
try:
title = dt.datetime.today()
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
receivers = ['[email protected]',
mail.To = ';'.join(receivers)
mail.Attachments.Add(Source=files)
mail.Subject ='What was Reddit talking about %s'%(title)
mail.BodyFormat=2
mail.HTMLBody=html
mail.Send()
except Exception:
print(traceback.format_exc())
if __name__ == "__main__":
main()