Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import praw
- import re
- ### \/ Config \/ ###
- REDDIT_USERNAME = ''
- REDDIT_PASSWORD = ''
- REDDIT_CLIENT_ID = ''
- REDDIT_CLIENT_SECRET = ''
- REDDIT_SUBREDDIT = 'yoursubredit'
- REDDIT_HEADERCOLOR = '#FF0000'
- REDDIT_BACKCOLOR = '#FFCCCC'
- REDDIT_ALSO_OLD = True
- REDDIT_OVERRIDE_STYLE = False
- # If false, will only use above colors when first making the widgets
- # Get ID from https://glass.twitch.tv/console/apps
- TWITCH_CLIENT_ID = ''
- TWITCH_OAUTH_ID = '' # from https://twitchapps.com/tokengen/
- TWITCH_BLACKLIST = ('mongraal',) # An example only. LOWERCASE. If blank put ()
- TWITCH_MAXSTREAMS = 2 # X streams per game
- TWITCH_GAME_IDS = (33214,) # Use id, name, or both. If blank put ()
- TWITCH_GAME_NAMES = ("summoners war: sky arena",'dota 2')
- # must be exact match! case doesn't matter
- ### /\ Config /\ ###
- ### \/ Code \/ ###
- print("Authenticating...",end='')
- reddit = praw.Reddit(
- client_id = REDDIT_CLIENT_ID,
- client_secret = REDDIT_CLIENT_SECRET,
- password = REDDIT_PASSWORD,
- username = REDDIT_USERNAME,
- user_agent = 'TopStream by /u/The_White_Light')
- print(f"Logged in as /u/{reddit.user.me()}")
- sub = reddit.subreddit(REDDIT_SUBREDDIT)
- gamedata = {}
- req = requests.get('https://api.twitch.tv/helix/games',
- params = {'name': TWITCH_GAME_NAMES,
- 'id': TWITCH_GAME_IDS},
- headers = {'Client-ID': TWITCH_CLIENT_ID,
- 'Authorization': f"Bearer {TWITCH_OAUTH_ID}"})
- for game in req.json()['data']:
- gamedata[game['name']] = {'id':int(game['id'])}
- widgets = sub.widgets
- sidebar = widgets.sidebar
- style = {'backgroundColor': REDDIT_BACKCOLOR, 'headerColor': REDDIT_HEADERCOLOR}
- for game in gamedata:
- for w in sidebar:
- if w.shortName == game:
- gamedata[game]['widget'] = w
- break
- else:
- gamedata[game]['widget'] = widgets.mod.add_text_area(game, 'FILL', style)
- if REDDIT_OVERRIDE_STYLE:
- kwargs = {'styles':style}
- else:
- kwargs = {}
- subdesc = sub.description
- for name, game in gamedata.items():
- print(f"Game: {name}")
- req = requests.get('https://api.twitch.tv/helix/streams',
- params={'game_id':game['id'], 'first':20, 'language':'en'},
- headers={'Client-ID': TWITCH_CLIENT_ID,
- 'Authorization': f"Bearer {TWITCH_OAUTH_ID}"})
- data = req.json()['data']
- out = ['|**Streamer**|**Title**|**Viewers**|\n|:-|:-|:-|']
- i = 0
- for stream in data:
- if stream['user_name'].lower() in TWITCH_BLACKLIST: continue
- title = stream['title'].strip()
- for c in "[]()":
- title = title.replace(c, f'\{c}')
- pass
- title = title.replace('\n','')
- title = title.replace('|','')
- i += 1
- out.append((f"|{stream['user_name']}|[{title}](https://twitch.tv/"
- f"{stream['user_name']})|{stream['viewer_count']}|"))
- if i == TWITCH_MAXSTREAMS: break
- game['widget'].mod.update(text='\n'.join(out), **kwargs)
- if REDDIT_ALSO_OLD:
- n = '\n'
- pos = re.search(f'(?<=#{name}\n).+?(?=\n#####)', subdesc, flags=re.S)
- if pos:
- subdesc = f"{subdesc[:pos.start()]}{n.join(out)}{subdesc[pos.end():]}"
- else:
- subdesc += f"\n#{name}\n{n.join(out)}\n#####"
- if REDDIT_ALSO_OLD:
- sub.mod.update(description=subdesc)
- print("Done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement