-
Notifications
You must be signed in to change notification settings - Fork 2
/
twitch.py
80 lines (66 loc) · 3.19 KB
/
twitch.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import requests
import sopel
# TODO: Make these config options c:
announce_chan = "#boardgamers"
streamers = [
"sc00ty",
"pacifist117",
"markiskingm",
"supersocks",
"agentxleavening",
"anothrnick"
]
currently_streaming = {}
@sopel.module.interval(10)
def monitor_streamers(bot):
global currently_streaming
streaming_names = []
streaming = requests.get('https://api.twitch.tv/kraken/streams', params={"channel": ",".join(streamers)}).json()
results = []
for streamer in streaming["streams"]:
streamer_name = streamer["channel"]["name"]
streamer_game = streamer["channel"]["game"]
streamer_url = streamer["channel"]["url"]
streamer_viewers = streamer["viewers"]
if streamer_name not in currently_streaming:
currently_streaming[streamer_name] = streamer_game
results.append("%s just went live playing %s! (%s - %s viewer%s)" % (streamer_name,
streamer_game,
streamer_url,
streamer_viewers,
"s" if streamer_viewers != 1 else ""))
elif streamer_game != currently_streaming[streamer_name]:
currently_streaming[streamer_name] = streamer_game
results.append("%s just started playing %s! (%s - %s viewer%s)" % (streamer_name,
streamer_game,
streamer_url,
streamer_viewers,
"s" if streamer_viewers != 1 else ""))
streaming_names.append(streamer_name)
if results:
bot.msg(announce_chan, ", ".join(results))
# Remove people who stopped streaming
for streamer in currently_streaming.keys():
if streamer not in streaming_names:
del currently_streaming[streamer]
@sopel.module.commands('twitchtv','tv','twitch', 'teevee')
@sopel.module.example('.tv or .tv twitchusername')
def streamer_status(bot, trigger):
streamer_name = trigger.group(2)
query = streamers if streamer_name is None else streamer_name.split(" ")
streaming = requests.get('https://api.twitch.tv/kraken/streams', params={"channel": ",".join(query)}).json()
results = []
for streamer in streaming["streams"]:
streamer_name = streamer["channel"]["name"]
streamer_game = streamer["channel"]["game"]
streamer_url = streamer["channel"]["url"]
streamer_viewers = streamer["viewers"]
results.append("%s is playing %s (%s - %s viewer%s)" % (streamer_name,
streamer_game,
streamer_url,
streamer_viewers,
"s" if streamer_viewers != 1 else "" ))
if results:
bot.say(", ".join(results))
else:
bot.say("Nobody is currently streaming.")