forked from hackingthemarkets/wallstreetbets-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_wsb.py
50 lines (37 loc) · 1.6 KB
/
search_wsb.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
from psaw import PushshiftAPI
import config
import datetime
import psycopg2
import psycopg2.extras
connection = psycopg2.connect(host=config.DB_HOST, database=config.DB_NAME, user=config.DB_USER, password=config.DB_PASS)
cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
cursor.execute("""
SELECT * FROM stock
""")
rows = cursor.fetchall()
stocks = {}
for row in rows:
stocks['$' + row['symbol']] = row['id']
api = PushshiftAPI()
start_time = int(datetime.datetime(2021, 1, 30).timestamp())
submissions = api.search_submissions(after=start_time,
subreddit='wallstreetbets',
filter=['url','author', 'title', 'subreddit'])
for submission in submissions:
words = submission.title.split()
cashtags = list(set(filter(lambda word: word.lower().startswith('$'), words)))
if len(cashtags) > 0:
print(cashtags)
print(submission.title)
for cashtag in cashtags:
if cashtag in stocks:
submitted_time = datetime.datetime.fromtimestamp(submission.created_utc).isoformat()
try:
cursor.execute("""
INSERT INTO mention (dt, stock_id, message, source, url)
VALUES (%s, %s, %s, 'wallstreetbets', %s)
""", (submitted_time, stocks[cashtag], submission.title, submission.url))
connection.commit()
except Exception as e:
print(e)
connection.rollback()