Skip to content

Instantly share code, notes, and snippets.

@Lemonszz
Created January 30, 2017 19:23
Show Gist options
  • Save Lemonszz/c16bcf61934088e52f8faa5b501811fa to your computer and use it in GitHub Desktop.
Save Lemonszz/c16bcf61934088e52f8faa5b501811fa to your computer and use it in GitHub Desktop.
import praw
import time
from urllib.request import urlopen
import urllib.request
import json
username = [Reddit Name]
password = [Reddit Password]
userAgent = [User Agent]
subreddit = [Subredit]
twitch_ClientID = [Twitch Api Key]
channels =["adlingtontplays","anderzel","mc_arkas",
"aureylian","avidyazen","coestar",
"docm77live","guude","jsano19",
"kurtjmac","supermcgamer","mhykol",
"millbee","nebris","omgchad",
"pakratt0013","pauseunpause","pyropuncher",
"sethbling","sevadus","vechs",
"vintagebeef","w92baj","zisteau",
"mindcrack"]
def TurnOn(twitchname, sidetext):
replaceString = "(http://twitch.tv/" + twitchname + "/#l" + ")"
regString = "(http://twitch.tv/" + twitchname + ")"
sidetext = sidetext.replace(regString, replaceString)
return sidetext
def TurnOff(twitchname, sidetext):
regString = "(http://twitch.tv/" + twitchname + "/#l" + ")"
replaceString = "(http://twitch.tv/" + twitchname + ")"
sidetext = sidetext.replace(regString, replaceString)
return sidetext
def IsTwitchLive(twitchname):
try:
url = str('https://api.twitch.tv/kraken/streams/' + twitchname)
streamID = -1
req = urllib.request.Request(url)
req.add_header("Client-ID",twitch_ClientID)
response = urllib.request.urlopen(req)
html = response.read()
data = json.loads(html.decode())
try:
streamID = data['stream']['_id']
except:
streamID = -1
return int(streamID)
except:
return -1
return -1;
def CheckChannels(sidetext):
for ch in channels:
if(IsTwitchLive(ch) != -1):
sidetext = TurnOn(ch, sidetext)
else:
sidetext = TurnOff(ch, sidetext)
return sidetext
try:
r = praw.Reddit(user_agent=userAgent)
r.login(username, password)
settings = r.get_settings(subreddit)
sidetext = settings['description']
original = sidetext
except:
print("reddit is broke")
while True:
try:
settings = r.get_settings(subreddit)
sidetext = settings['description']
original = sidetext
sidetext = CheckChannels(sidetext)
if(sidetext != original):
r.update_settings(r.get_subreddit(subreddit), description=sidetext)
time.sleep(120)
except:
print("something broke")
@l3d00m
Copy link

l3d00m commented Feb 23, 2017

Hey,

thanks again for the code. I've updated it to use PRAW4 and did some other minor things for my subreddit.

In case you want to update to PRAW4 as well I hope I can save you some time. :D

https://gist.github.com/l3d00m/d757c5444545162b8823114d521297db

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment