Advertisement
KRDucky

PythonTwitchTiVo

Mar 29th, 2016
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. #This script checks if a user on twitch is currently streaming and then records the stream via livestreamer
  2.  
  3. from urllib.request import urlopen
  4. from urllib.error import URLError
  5. from threading import Timer
  6. import time
  7. import json
  8. import sys
  9. import subprocess
  10. import datetime
  11.  
  12. def check_user(user):
  13.     """ returns 0: online, 1: offline, 2: not found, 3: error """
  14.     global info
  15.     url = 'https://api.twitch.tv/kraken/streams/' + user
  16.     try:
  17.         info = json.loads(urlopen(url, timeout = 15).read().decode('utf-8'))
  18.         if info['stream'] == None:
  19.             status = 1
  20.         else:
  21.             status = 0
  22.     except URLError as e:
  23.         if e.reason == 'Not Found' or e.reason == 'Unprocessable Entity':
  24.             status = 2
  25.         else:
  26.             status = 3
  27.     return status
  28.  
  29. def format_filename(fname):
  30. # Removes invalid characters from filename
  31.     fname = fname.replace("/","")
  32.     fname = fname.replace("?","")
  33.     fname = fname.replace(":","-")
  34.     fname = fname.replace("\\","")
  35.     fname = fname.replace("<","")
  36.     fname = fname.replace(">","")
  37.     fname = fname.replace("*","")
  38.     fname = fname.replace("\"","")
  39.     fname = fname.replace("|","")
  40.     return fname
  41.  
  42.  
  43. def loopcheck():
  44.     while True:
  45.         status = check_user(user)
  46.         if status == 2:
  47.             print("username not found. invalid username?")
  48.         elif status == 3:
  49.             print(datetime.datetime.now().strftime("%Hh%Mm%Ss")," ","unexpected error. will try again in 5 minutes.")
  50.             time.sleep(300)
  51.         elif status == 1:
  52.             print(user,"currently offline, checking again in",refresh,"seconds")
  53.             time.sleep(refresh) # 30 seconds
  54.         elif status == 0:
  55.             print(user,"online. stop.")          
  56.             filename = user+" - "+datetime.datetime.now().strftime("%Y-%m-%d %Hh%Mm%Ss")+" - "+(info['stream']).get("channel").get("status")+".mp4"
  57.             filename = format_filename(filename)
  58.             subprocess.call(["livestreamer","twitch.tv/"+user,quality,"-o",directory+filename])
  59.             print("Stream is done. Going back to checking..")
  60.             time.sleep(15)
  61.  
  62. def main():
  63.     global refresh
  64.     global user
  65.     global quality
  66.     global directory
  67.  
  68.     print("Usage: check.py [refresh] [user] [quality]")
  69.     print("Usage: TwitchChecker.py [refresh] [user] [quality]")
  70.  
  71.     refresh = 30.0
  72.     user = "escapefromtarkov"
  73.     quality = "best"
  74.     directory = "F:\TwitchStreams\\"
  75.    
  76.    
  77.        
  78.     if(refresh<15):
  79.         print("Check interval should not be lower than 15 seconds")
  80.         refresh=15
  81.        
  82.     print("Checking for",user,"every",refresh,"seconds. Record with",quality,"quality.")
  83.     loopcheck()
  84.    
  85.    
  86. if __name__ == "__main__":
  87.     # execute only if run as a script
  88.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement