-
Notifications
You must be signed in to change notification settings - Fork 0
/
spga.py
62 lines (51 loc) · 2.8 KB
/
spga.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import argparse
import requests
from timeit import default_timer as timer
from time import sleep
import pyttsx3
ttsEngine = pyttsx3.init()
parser = argparse.ArgumentParser(description="Periodically checks whether a given streamer is playing a given game. At the first sight that they are, an audio notification is played and the script exits.")
parser.add_argument("-streamerName", type=str, required=True, help="Public name of the streamer", default="Robbaz")
parser.add_argument("-gameName", type=str, required=True, help="Name of the game to watch for", default="Wreckfest")
parser.add_argument("-oauth", type=str, required=True, help="Your user request OAuth token")
parser.add_argument("-checkInterval", type=int, help="Number of minutes between each check", default=1)
parser.add_argument("-timeout", type=int, help="Number of minutes before exiting the script", default=30)
args = parser.parse_args()
# Get the broadcaster_id from the given streamer name
params:dict = {'login': args.streamerName}
headers:dict = {'Authorization': 'Bearer {0}'.format(args.oauth), 'Client-Id': '83mrwk5de48splmqpbylf43nya59fg'}
req:requests.Response = requests.get('https://api.twitch.tv/helix/users', params=params, headers=headers)
json:dict = req.json()
# Error check
if(json.get("data",-1) == -1):
raise ValueError("Can't find streamer with that name!")
if(len(json["data"]) < 1):
raise ValueError("Can't find streamer with that name!")
if(len(json["data"]) > 1):
raise ValueError("Found multiple streamers with that name!")
broadcaster_id:int = req.json()["data"][0]["id"]
params:dict = {'broadcaster_id': broadcaster_id}
# Start a timer for the timeout to check against
start:float = timer()
while(timer() - start < (args.timeout * 60)):
# Check what game the streamer is playing
req = requests.get('https://api.twitch.tv/helix/channels', params=params, headers=headers)
json = req.json()
# Error check
if(json.get("data",-1) == -1):
raise ValueError("Can't find streamer with that name!")
for idx in range(len(json["data"])):
if(json["data"][idx]["broadcaster_id"] != broadcaster_id):
continue
if(json["data"][idx]["game_name"] == args.gameName):
success:str = "{0} is now playing {1}!".format(json["data"][idx]["broadcaster_name"], json["data"][idx]["game_name"])
print(success)
ttsEngine.say(success)
ttsEngine.say(success)
ttsEngine.say("Goodbye :)")
ttsEngine.runAndWait()
exit(0)
print("\"{0}\" is currently playing \"{1}\". Checking for \"{2}\" again in {3} minute(s)...".format(json["data"][idx]["broadcaster_name"], json["data"][idx]["game_name"], args.gameName, args.checkInterval))
sleep(args.checkInterval * 60)
print("Timed out after {0} minute(s)".format(args.timeout))
exit(0)