Guest User

Untitled

a guest
Nov 29th, 2021
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. import re
  2. import requests
  3. import flask
  4.  
  5.  
  6. subreddits = ['shortsqueeze', '1kto1mil', 'ShortSqueeze_Army', 'ShortSqueezeStonks', 'SqueezePlays', 'WallStreetBets', 'smallstreetbets', 'TheRaceTo10Million']
  7.  
  8.  
  9. def scrape_tickers(subreddit):
  10.     url = 'https://www.reddit.com/r/{}/top/.json?t=day/'.format(subreddit)
  11.     headers = {'User-agent': 'Bleep blorp bot 0.1'}
  12.     params = {'limit': 100000}
  13.     info = requests.get(url, headers=headers, params=params)
  14.     alltext = info.text
  15.     tickers = re.findall(r'[A-Z]{3,}', alltext)  # REGEX to find all caps strings of length 3 or more
  16.     tickers = [i for i in tickers if len(i) <= 4]  # filter out any longer than 4
  17.     unique = sorted(list(dict.fromkeys(tickers)))  # get a unique list of symbols
  18.     result = list()  # build a list of dicts with counts
  19.     for ticker in unique:
  20.         info = {'ticker':ticker, 'count':tickers.count(ticker)}
  21.         result.append(info)
  22.     #result = [i for i in result if i['count'] > 1]  # remove all instances with only 1 mention
  23.     return sorted(result, key = lambda i: i['count'], reverse=True)  # return list of dicts sorted by count of occurences
  24.  
  25.  
  26. header = """<!DOCTYPE html>
  27. <html>
  28. <head>
  29. <title>MEMESTONK 9001</title>
  30. <style>
  31. body {
  32.  font-family: roboto;
  33.  font-family: tahoma;
  34.  font-family: arial;
  35. }
  36. a {
  37.  color: black;
  38.  text-decoration: none;
  39. }
  40. </style>
  41. <body>
  42. """  
  43.  
  44.  
  45. def nav_ribbon():
  46.     result = ''
  47.     for sub in subreddits:
  48.         result += '<a href="/%s">%s</a> -- ' % (sub, sub)
  49.     return result
  50.  
  51.  
  52. def make_table(tickers):
  53.     result = '\n<table><tr><th>Ticker</th><th>Count</th><th>Yahoo</th><th>Nasdaq</th><th>Fintel</th><th>Reddit</th></tr>\n'
  54.     for ticker in tickers:
  55.         result += '<tr><td>%s</td><td>%s</td>' % (ticker['ticker'], ticker['count'])
  56.         result += '<td><a href="https://finance.yahoo.com/quote/%s">%s</a></td>' % (ticker['ticker'], 'Yahoo')
  57.         result += '<td><a href="https://www.nasdaq.com/market-activity/stocks/%s/short-interest">%s</a></td>' % (ticker['ticker'], 'Nasdaq')
  58.         result += '<td><a href="https://fintel.io/ss/us/%s">%s</a></td>' % (ticker['ticker'], 'Fintel')
  59.         result += '<td><a href="https://www.reddit.com/search/?q=%s&t=day">%s</a></td></tr>\n' % (ticker['ticker'], 'Reddit')
  60.     result += '</table>'
  61.     return result
  62.  
  63.  
  64. app = flask.Flask('MEMESTONKS')
  65.  
  66.  
  67. @app.route('/', methods=['GET'])
  68. def home():
  69.     html = header
  70.     html += nav_ribbon()
  71.     html += '<hr></body></html>'
  72.     return flask.Response(html, mimetype='text/html')
  73.  
  74.  
  75. @app.route('/<subreddit>', methods=['GET'])
  76. def reddit(subreddit):
  77.     html = header
  78.     html += nav_ribbon()
  79.     html += '<hr>'
  80.     tickers = scrape_tickers(subreddit)
  81.     html += make_table(tickers)
  82.     html += '</body></html>'
  83.     return flask.Response(html, mimetype='text/html')
  84.  
  85.  
  86. if __name__ == '__main__':
  87.     print('go to http://localhost or http://127.0.0.1')
  88.     app.run(host='0.0.0.0', port=80)
Add Comment
Please, Sign In to add comment