import re import requests import flask subreddits = ['shortsqueeze', '1kto1mil', 'ShortSqueeze_Army', 'ShortSqueezeStonks', 'SqueezePlays', 'WallStreetBets', 'smallstreetbets', 'TheRaceTo10Million'] def scrape_tickers(subreddit): url = 'https://www.reddit.com/r/{}/top/.json?t=day/'.format(subreddit) headers = {'User-agent': 'Bleep blorp bot 0.1'} params = {'limit': 100000} info = requests.get(url, headers=headers, params=params) alltext = info.text tickers = re.findall(r'[A-Z]{3,}', alltext) # REGEX to find all caps strings of length 3 or more tickers = [i for i in tickers if len(i) <= 4] # filter out any longer than 4 unique = sorted(list(dict.fromkeys(tickers))) # get a unique list of symbols result = list() # build a list of dicts with counts for ticker in unique: info = {'ticker':ticker, 'count':tickers.count(ticker)} result.append(info) #result = [i for i in result if i['count'] > 1] # remove all instances with only 1 mention return sorted(result, key = lambda i: i['count'], reverse=True) # return list of dicts sorted by count of occurences header = """ MEMESTONK 9001 """ def nav_ribbon(): result = '' for sub in subreddits: result += '%s -- ' % (sub, sub) return result def make_table(tickers): result = '\n\n' for ticker in tickers: result += '' % (ticker['ticker'], ticker['count']) result += '' % (ticker['ticker'], 'Yahoo') result += '' % (ticker['ticker'], 'Nasdaq') result += '' % (ticker['ticker'], 'Fintel') result += '\n' % (ticker['ticker'], 'Reddit') result += '
TickerCountYahooNasdaqFintelReddit
%s%s%s%s%s%s
' return result app = flask.Flask('MEMESTONKS') @app.route('/', methods=['GET']) def home(): html = header html += nav_ribbon() html += '
' return flask.Response(html, mimetype='text/html') @app.route('/', methods=['GET']) def reddit(subreddit): html = header html += nav_ribbon() html += '
' tickers = scrape_tickers(subreddit) html += make_table(tickers) html += '' return flask.Response(html, mimetype='text/html') if __name__ == '__main__': print('go to http://localhost or http://127.0.0.1') app.run(host='0.0.0.0', port=80)