Advertisement
Guest User

Untitled

a guest
Dec 26th, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 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 os
  7. import time
  8. import json
  9. import sys
  10. import subprocess
  11. import datetime
  12.  
  13. def check_user(user):
  14. """ returns 0: online, 1: offline, 2: not found, 3: error """
  15. global info
  16. url = 'https://api.twitch.tv/kraken/streams/' + user + "/?client_id=2dh6sbibqn2f6vopirge0ytzr8xw44"
  17. try:
  18. info = json.loads(urlopen(url, timeout = 15).read().decode('utf-8'))
  19. if info['stream'] == None:
  20. status = 1
  21. else:
  22. status = 0
  23. except URLError as e:
  24. if e.reason == 'Not Found' or e.reason == 'Unprocessable Entity':
  25. status = 2
  26. else:
  27. status = 3
  28. return status
  29.  
  30. def format_filename(fname):
  31. # Removes invalid characters from filename
  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. fname = fname.replace("|","")
  41. return fname
  42.  
  43.  
  44. def loopcheck():
  45. while True:
  46. status = check_user(user)
  47. if status == 2:
  48. print("username not found. invalid username?")
  49. elif status == 3:
  50. print(datetime.datetime.now().strftime("%Hh%Mm%Ss")," ","unexpected error. will try again in 5 minutes.")
  51. time.sleep(300)
  52. elif status == 1:
  53. print(user,"currently offline, checking again in",refresh,"seconds")
  54. time.sleep(refresh) # 30 seconds
  55. elif status == 0:
  56. print(user,"online. stop.")
  57. filename = user+" - "+datetime.datetime.now().strftime("%Y-%m-%d %Hh%Mm%Ss")+" - "+(info['stream']).get("channel").get("status")+".mp4"
  58. filename = format_filename(filename)
  59. subprocess.call(["livestreamer", "twitch.tv/"+user,quality,"-o",directory+filename])
  60. print("Stream is done. Going back to checking..")
  61. time.sleep(15)
  62.  
  63. def main():
  64. global refresh
  65. global user
  66. global quality
  67. global directory
  68.  
  69. refresh = 30.0
  70. user = "billowy"
  71. quality = "source"
  72. directory = "//home//Stream//"
  73.  
  74. client = False
  75. clientID = "http-header=Client-ID=jzkbprff40iqj646a697cyrvl0zt2m6"
  76. dir_path = '%s\livestreamer\livestreamerrc' % os.environ['APPDATA']
  77.  
  78. file = open(dir_path, 'r')
  79. for line in file:
  80. if line == clientID:
  81. client = True
  82. file = open(dir_path, 'a')
  83. if client != True:
  84. file.write(clientID)
  85. file.close()
  86.  
  87. if(refresh<15):
  88. print("Check interval should not be lower than 15 seconds")
  89. refresh=15
  90.  
  91. print("Checking for",user,"every",refresh,"seconds. Record with",quality,"quality.")
  92. loopcheck()
  93.  
  94.  
  95. if __name__ == "__main__":
  96. # execute only if run as a script
  97. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement