This repository was archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitch.py
192 lines (175 loc) · 8.43 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#As of 09-14-2016, twitch API now requires Client-ID with all requests. You will need to create one from here:
# https://www.twitch.tv/settings/connections
# complete twitch.py rewrite coming soon™
import requests
import sopel
import re
from sopel.tools import SopelMemory
import datetime
# TODO: Make these config options c:
twitchclientid = "YOUR_API_KEY"
announce_chan = "#english"
logchannel = "#croissant" #used for live logging certain issues that plague this module
streamers = [
"39351493", #ridelore
"22510310", #gamesdonequick
"66426292", #freddurst
"8822", #blizzard
"7700271", #quakecon
"153281938", #jonmakesbeats
"37516578", #warcraft
"40243526", #metaltoss
"24657677", #back2warcraft
"133879160", #moopietime
"81660425", #bruce1
"230571071" #3arrows
]
#end config
twitchregex = re.compile('.*https?:\/\/(?:(?:www\.)|(?:go\.))?twitch.tv\/(.*?)\/?(?:(?=[\s|\/])|$)')
mixerregex = re.compile('.*https?:\/\/(?:www\.)?mixer.com\/(.*?)\/?(?:(?=[\s])|$)')
twitchclipsregex = re.compile('.*https?:\/\/clips\.twitch.tv\/(.*?)\/?(?:(?=[\s])|$)')
def setup(bot):
if not bot.memory.contains('url_callbacks'):
bot.memory['url_callbacks'] = SopelMemory()
bot.memory['url_callbacks'][twitchregex] = twitchirc
bot.memory['url_callbacks'][mixerregex] = mixerirc
bot.memory['url_callbacks'][twitchclipsregex] = twitchclipsirc
def shutdown(bot):
del bot.memory['url_callbacks'][twitchregex]
del bot.memory['url_callbacks'][mixerregex]
del bot.memory['url_callbacks'][twitchclipsregex]
currently_streaming = {}
currently_ystreaming = {}
tsep = lambda a : "{:,}".format(int(a))
def twitch_api(stream_list):
""" Returns the result of the http request from Twitch's api """
return requests.get('https://api.twitch.tv/kraken/streams', params={"channel": ",".join(stream_list)}, headers={"Client-ID": twitchclientid,"Accept":"application/vnd.twitchtv.v5+json"}).json()
def gettwitchuserid(tuser):
tuid = requests.get("https://api.twitch.tv/kraken/users?login={}".format(tuser),headers={"Client-ID":twitchclientid,"Accept":"application/vnd.twitchtv.v5+json"}).json()
if not tuid['users']:
return None
else:
return [tuid['users'][0]['_id']]
def twitch_generator(streaming):
for streamer in streaming["streams"]:
streamer_dict = {}
streamer_dict["name"] = streamer["channel"]["name"]
streamer_dict["game"] = streamer["channel"]["game"]
streamer_dict["status"] = streamer["channel"]["status"]
streamer_dict["url"] = streamer["channel"]["url"]
streamer_dict["starttime"] = datetime.datetime.strptime(streamer['created_at'], '%Y-%m-%dT%H:%M:%SZ')
streamer_dict["viewers"] = streamer["viewers"]
yield streamer_dict
@sopel.module.interval(20)
def monitor_streamers(bot):
streaming_names = []
try:
streaming = twitch_api(streamers)
except Exception as e:
return print("There was an error reading twitch API {0}".format(e))
results = []
if streaming.get("streams"):
twitch_gen = twitch_generator(streaming)
for streamer in twitch_gen:
if streamer["name"] not in currently_streaming:
currently_streaming[streamer["name"]] = streamer["game"], {'cooldown': 0, 'starttime': streamer["starttime"]}
results.append("📺 %s just went live playing %s! [%s] ( %s - %s viewer%s )" % (streamer["name"],
streamer["game"],
streamer["status"],
streamer["url"],
tsep(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 list(currently_streaming):
if streamer not in streaming_names:
currently_streaming[streamer][1]['cooldown'] += 10
if currently_streaming[streamer][1]['cooldown'] > 130:
#bot.msg(logchannel,'{0} was removed from currently_streaming for reaching a cooldown of {1}'.format(streamer,currently_streaming[streamer][1]['cooldown']))
del currently_streaming[streamer]
@sopel.module.commands('twitchtv','twitch','tv')
@sopel.module.example('.twitch or .twitch twitchusername')
def streamer_status(bot, trigger):
streamer_name = trigger.group(2)
query = streamers if streamer_name is None else gettwitchuserid(streamer_name)
if query is None:
return bot.say("Nobody is currently streaming")
try:
streaming = twitch_api(query)
except Exception as e:
return print("There was an error reading twitch API {0}".format(e))
results = []
if streaming.get("streams"):
twitch_gen = twitch_generator(streaming)
for streamer in twitch_gen:
results.append("📺 %s is playing %s [%s] ( %s - %s viewer%s )" % (streamer["name"],
streamer["game"],
streamer["status"],
streamer["url"],
tsep(streamer["viewers"]),
"s" if streamer["viewers"] != 1 else ""))
if results:
bot.say(", ".join(results))
else:
bot.say("Nobody is currently streaming.")
@sopel.module.rule('.*https?:\/\/(?:(?:www\.)|(?:go\.))?twitch.tv\/(.*?)\/?(?:(?=[\s|\/])|$)')
def twitchirc(bot, trigger, match = None):
match = match or trigger
streamer_name = match.group(1)
query = streamers if streamer_name is None else gettwitchuserid(streamer_name)
try:
streaming = twitch_api(query)
except Exception as e:
return print("There was an error reading twitch API {0}".format(e))
results = []
if streaming.get("streams"):
twitch_gen = twitch_generator(streaming)
for streamer in twitch_gen:
results.append("📺 %s is playing %s [%s] ( %s - %s viewer%s )" % (streamer["name"],
streamer["game"],
streamer["status"],
streamer["url"],
tsep(streamer["viewers"]),
"s" if streamer["viewers"] != 1 else ""))
if results:
bot.say(", ".join(results))
else:
pass
@sopel.module.rule('.*https?:\/\/(?:www\.)?mixer.com\/(.*?)\/?(?:(?=[\s])|$)')
def mixerirc(bot, trigger, match = None):
match = match or trigger
streamer_name = match.group(1)
streaming = requests.get('https://mixer.com/api/v1/channels/{}'.format(streamer_name)).json()
results = []
if streaming:
streamer_name = streaming["token"]
if streaming.get("type"):
streamer_game = streaming["type"]["name"]
else:
streamer_game = "a game"
streamer_status = streaming["name"]
streamer_viewers = streaming["viewersCurrent"]
results.append("📺 %s is playing %s [%s] - %s viewer%s" % (streamer_name,
streamer_game,
streamer_status,
tsep(streamer_viewers),
"s" if streamer_viewers != 1 else "" ))
if results:
bot.say(", ".join(results))
else:
pass
@sopel.module.rule('.*(?:https?:\/\/clips\.twitch.tv\/(.*?)\/?(?:(?=[\s])|$))|.*(?:https?:\/\/(?:www)?\.twitch\.tv\/.*?\/clip\/(.*?)\/?(?:(?=[\s])|$))')
def twitchclipsirc(bot,trigger, match = None):
match = match or trigger
slug = match.group(1) or match.group(2)
clips = requests.get("https://api.twitch.tv/kraken/clips/{}".format(slug), headers={"Client-ID":twitchclientid,"Accept":"application/vnd.twitchtv.v5+json"}).json()
name = clips['broadcaster']['display_name']
title = clips['title']
game = clips['game']
views = clips['views']
bot.say("{} [{}] | {} | {} views".format(title, game, name, tsep(views)))
@sopel.module.commands('debugtv')
def debug(bot, trigger):
bot.msg(logchannel,"currently_streaming: {}".format(", ".join(currently_streaming)))