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 = """
| Ticker | Count | Yahoo | Nasdaq | Fintel | |
|---|---|---|---|---|---|
| %s | %s | ' % (ticker['ticker'], ticker['count']) result += '%s | ' % (ticker['ticker'], 'Yahoo') result += '%s | ' % (ticker['ticker'], 'Nasdaq') result += '%s | ' % (ticker['ticker'], 'Fintel') result += '%s |