-
Notifications
You must be signed in to change notification settings - Fork 0
/
redstreet.py
101 lines (74 loc) · 3.11 KB
/
redstreet.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
from keys import CLIENT_ID, SECRET_TOKEN, USERNAME, PASSWORD
from collections import Counter
import praw
import csv
import sys
# Default number of comments to lookup
no_of_comments = 50
# Number of comments to lookup as specified as the command line argument
try:
no_of_comments = int(sys.argv[1])
except:
pass
# Connect to Reddit API
reddit = praw.Reddit(client_id=CLIENT_ID, client_secret=SECRET_TOKEN, user_agent='RedStreetBets', username=USERNAME, password=PASSWORD)
#################################################
# START: Get the number of specified comments #
#################################################
subreddit = reddit.subreddit('wallstreetbets')
print('Fetching comments...')
top_subreddit = subreddit.new(limit=no_of_comments)
words_collection = []
print('Fragmenting comments into separate words...')
for submission in top_subreddit:
title = submission.title
title_words = title.split()
words_collection.append(title_words)
###############################################
# END: Get the number of specified comments #
###############################################
###########################################
# START: Funnel out possible stock names #
###########################################
potential_stock_symbols = []
known_not_stocks = ['UPVOTE', 'SUPPORT', 'YOLO', 'YOLO.', 'CLASS', 'ACTION', 'AGAINST', 'ROBINHOOD', 'GAIN', 'LOSS', 'PORN', 'WSB', 'I', 'STILL', "DIDN'T", 'HEAR', 'NO', 'BELL']
for title in words_collection:
for word in title:
if word.isupper() and word not in known_not_stocks:
potential_stock_symbols.append(word)
#########################################
# END: Funnel out possible stock names #
#########################################
#######################################
# START: Get stock tickers from csv #
#######################################
stocks_list = []
with open('./files/unique_usa_tickers.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
for row in csv_reader:
stocks_list.append(row[0])
#####################################
# END: Get stock tickers from csv #
#####################################
########################################
# START: Find most discussed tickers #
########################################
stock_dict = Counter()
def get_stock_list(comments, stocks_list):
for potential_stock_symbol in comments:
for ticker in stocks_list:
if ticker == potential_stock_symbol or potential_stock_symbol == (ticker+'.') or potential_stock_symbol == ('$'+ticker) or potential_stock_symbol == (ticker+'!'):
stock_dict[ticker] += 1
def Reverse(lst):
new_lst = lst[::-1]
return new_lst
get_stock_list(potential_stock_symbols, stocks_list)
ordered_stock_dict = sorted(stock_dict.items(), key=lambda stock_dict: stock_dict[1])
largest_discussed_tickers = Reverse(ordered_stock_dict)
######################################
# END: Find most discussed tickers #
######################################
print('\nTotal comments read :', no_of_comments)
print("\nTop 20 most discussed tickers-\n")
for tick in largest_discussed_tickers[:20]:
print(tick)