Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 = """<!DOCTYPE html>
- <html>
- <head>
- <title>MEMESTONK 9001</title>
- <style>
- body {
- font-family: roboto;
- font-family: tahoma;
- font-family: arial;
- }
- a {
- color: black;
- text-decoration: none;
- }
- </style>
- <body>
- """
- def nav_ribbon():
- result = ''
- for sub in subreddits:
- result += '<a href="/%s">%s</a> -- ' % (sub, sub)
- return result
- def make_table(tickers):
- result = '\n<table><tr><th>Ticker</th><th>Count</th><th>Yahoo</th><th>Nasdaq</th><th>Fintel</th><th>Reddit</th></tr>\n'
- for ticker in tickers:
- result += '<tr><td>%s</td><td>%s</td>' % (ticker['ticker'], ticker['count'])
- result += '<td><a href="https://finance.yahoo.com/quote/%s">%s</a></td>' % (ticker['ticker'], 'Yahoo')
- result += '<td><a href="https://www.nasdaq.com/market-activity/stocks/%s/short-interest">%s</a></td>' % (ticker['ticker'], 'Nasdaq')
- result += '<td><a href="https://fintel.io/ss/us/%s">%s</a></td>' % (ticker['ticker'], 'Fintel')
- result += '<td><a href="https://www.reddit.com/search/?q=%s&t=day">%s</a></td></tr>\n' % (ticker['ticker'], 'Reddit')
- result += '</table>'
- return result
- app = flask.Flask('MEMESTONKS')
- @app.route('/', methods=['GET'])
- def home():
- html = header
- html += nav_ribbon()
- html += '<hr></body></html>'
- return flask.Response(html, mimetype='text/html')
- @app.route('/<subreddit>', methods=['GET'])
- def reddit(subreddit):
- html = header
- html += nav_ribbon()
- html += '<hr>'
- tickers = scrape_tickers(subreddit)
- html += make_table(tickers)
- html += '</body></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)
Add Comment
Please, Sign In to add comment