-
Notifications
You must be signed in to change notification settings - Fork 2
/
stocks.py
38 lines (27 loc) · 1.08 KB
/
stocks.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
from psaw import PushshiftAPI
import pandas as pd
import datetime as dt
def reddit_scrape(ticker):
"""searches Reddit for comments containing ticker_symbol
param ticker: stock ticker to be searched
"""
data = {}
end_epoch = int(dt.datetime.utcnow().timestamp())
api = PushshiftAPI()
# create dictionary with epoch dates as keys (spans a week)
for day in range(2, 9):
# data[end_epoch - (day * 86400)] = 0
test = end_epoch - (day * 86400)
convert = dt.datetime.fromtimestamp(test)
data[convert.date()] = 0
# pulls number of comments stock is mentioned in and attaches number to corresponding dictionary keys
for key in data:
start_epoch = key
api_request_generator = api.search_submissions(q=ticker, after=start_epoch, before=end_epoch)
stock_submissions = pd.DataFrame([submission.d_ for submission in api_request_generator])
data[key] = stock_submissions.size
end_epoch = start_epoch
return data
if __name__ == "__main__":
# Testing
print(reddit_scrape("$NAKD"))