Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.42 KB | None | 0 0
  1. import requests
  2. import praw
  3. import re
  4.  
  5. ### \/ Config \/ ###
  6.  
  7. REDDIT_USERNAME       = ''
  8. REDDIT_PASSWORD       = ''
  9. REDDIT_CLIENT_ID      = ''
  10. REDDIT_CLIENT_SECRET  = ''
  11. REDDIT_SUBREDDIT      = 'yoursubredit'
  12. REDDIT_HEADERCOLOR    = '#FF0000'
  13. REDDIT_BACKCOLOR      = '#FFCCCC'
  14. REDDIT_ALSO_OLD       = True
  15. REDDIT_OVERRIDE_STYLE = False
  16. # If false, will only use above colors when first making the widgets
  17.  
  18. # Get ID from https://glass.twitch.tv/console/apps
  19. TWITCH_CLIENT_ID  = ''
  20. TWITCH_OAUTH_ID   = '' # from https://twitchapps.com/tokengen/
  21. TWITCH_BLACKLIST  = ('mongraal',) # An example only. LOWERCASE. If blank put ()
  22. TWITCH_MAXSTREAMS = 2             # X streams per game
  23. TWITCH_GAME_IDS   = (33214,)      # Use id, name, or both. If blank put ()
  24. TWITCH_GAME_NAMES = ("summoners war: sky arena",'dota 2')
  25.                                   # must be exact match! case doesn't matter
  26.  
  27. ### /\ Config /\ ###
  28.  
  29.  
  30. ### \/  Code  \/ ###
  31. print("Authenticating...",end='')
  32. reddit = praw.Reddit(
  33.   client_id     = REDDIT_CLIENT_ID,
  34.   client_secret = REDDIT_CLIENT_SECRET,
  35.   password      = REDDIT_PASSWORD,
  36.   username      = REDDIT_USERNAME,
  37.   user_agent    = 'TopStream by /u/The_White_Light')
  38. print(f"Logged in as /u/{reddit.user.me()}")
  39. sub = reddit.subreddit(REDDIT_SUBREDDIT)
  40. gamedata = {}
  41. req = requests.get('https://api.twitch.tv/helix/games',
  42.                    params  = {'name':      TWITCH_GAME_NAMES,
  43.                               'id':        TWITCH_GAME_IDS},
  44.                    headers = {'Client-ID': TWITCH_CLIENT_ID,
  45.                               'Authorization': f"Bearer {TWITCH_OAUTH_ID}"})
  46. for game in req.json()['data']:
  47.   gamedata[game['name']] = {'id':int(game['id'])}
  48.  
  49. widgets = sub.widgets
  50. sidebar = widgets.sidebar
  51. style = {'backgroundColor': REDDIT_BACKCOLOR, 'headerColor': REDDIT_HEADERCOLOR}
  52. for game in gamedata:
  53.   for w in sidebar:
  54.     if w.shortName == game:
  55.       gamedata[game]['widget'] = w
  56.       break
  57.   else:
  58.     gamedata[game]['widget'] = widgets.mod.add_text_area(game, 'FILL', style)
  59. if REDDIT_OVERRIDE_STYLE:
  60.   kwargs = {'styles':style}
  61. else:
  62.   kwargs = {}
  63.  
  64. subdesc = sub.description
  65.  
  66. for name, game in gamedata.items():
  67.   print(f"Game: {name}")
  68.   req = requests.get('https://api.twitch.tv/helix/streams',
  69.                      params={'game_id':game['id'], 'first':20, 'language':'en'},
  70.                      headers={'Client-ID': TWITCH_CLIENT_ID,
  71.                               'Authorization': f"Bearer {TWITCH_OAUTH_ID}"})
  72.   data = req.json()['data']
  73.   out = ['|**Streamer**|**Title**|**Viewers**|\n|:-|:-|:-|']
  74.   i = 0
  75.   for stream in data:
  76.     if stream['user_name'].lower() in TWITCH_BLACKLIST: continue
  77.     title = stream['title'].strip()
  78.     for c in "[]()":
  79.       title = title.replace(c, f'\{c}')
  80.       pass
  81.     title = title.replace('\n','')
  82.     title = title.replace('|','')
  83.     i += 1
  84.     out.append((f"|{stream['user_name']}|[{title}](https://twitch.tv/"
  85.                 f"{stream['user_name']})|{stream['viewer_count']}|"))
  86.     if i == TWITCH_MAXSTREAMS: break
  87.   game['widget'].mod.update(text='\n'.join(out), **kwargs)
  88.   if REDDIT_ALSO_OLD:
  89.     n = '\n'
  90.     pos = re.search(f'(?<=#{name}\n).+?(?=\n#####)', subdesc, flags=re.S)
  91.     if pos:
  92.       subdesc = f"{subdesc[:pos.start()]}{n.join(out)}{subdesc[pos.end():]}"
  93.     else:
  94.       subdesc += f"\n#{name}\n{n.join(out)}\n#####"
  95. if REDDIT_ALSO_OLD:
  96.   sub.mod.update(description=subdesc)
  97. print("Done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement