/
twitch_streaminfo.py
156 lines (115 loc) · 4.76 KB
/
twitch_streaminfo.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
import json
from datetime import datetime, timedelta, date, time
import urllib2
import hexchat
__module_name__ = 'Twitch streaminfo'
__module_version__ = '0.1'
__module_description__ = 'Prints jtv messages, provides stream information'
__module_author__ = 'Kibur'
liveChannels = []
firstRun = True
def unload_cb(userdata):
hexchat.prnt('\003' + __module_name__ + ' ' + __module_version__ + ' unloaded\003')
def format(string, special=0):
if (special):
string = string.replace(string[:1], '')
string = '\002\035' + string
hexchat.prnt(string.encode('utf-8'))
def loadJSON(url):
try:
data = urllib2.urlopen(url, timeout=3)
obj = json.load(data)
return obj
except Exception, ex:
return json.dumps(None)
def getSender(word):
return word.split('!')[0].replace(':', '', 1)
def checkmessage_cb(word, word_eol, userdata):
string = ' '.join(word[0:3])
string = string.replace(string[0:1], '')
if (string == 'jtv!jtv@jtv.tmi.twitch.tv PRIVMSG ' + hexchat.get_info('nick')):
format(word_eol[3], special=1)
return hexchat.EAT_ALL
elif word[3] == ':\x01ACTION':
format('* ' + getSender(word[0]) + ' ' + word_eol[4].strip('\x01'))
return hexchat.EAT_ALL
elif word[3] == ':/w':
whisper = getSender(word[0]) + ' >> ' + word[4] + ': ' + word_eol[5]
hexchat.prnt('\002\00326' + whisper.encode('utf-8') + '\002\00326')
return hexchat.EAT_ALL
return hexchat.EAT_NONE
def servererr_cb(word, word_eol, userdata):
if word[3] in ('STREAM', 'STATS', 'W'):
return hexchat.EAT_ALL
def stream():
CHANNEL = hexchat.get_info('channel')
if (hexchat.get_info('host') != 'irc.twitch.tv'):
hexchat.prnt('/STREAM works only in irc.twitch.tv chats')
return
obj = loadJSON('https://api.twitch.tv/kraken/streams/' + CHANNEL.strip('#'))
if (obj['stream'] == None):
format(CHANNEL.title() + ' is not live on twitch.tv.', special=1)
else:
format(obj['stream']['channel']['status'] + '(' + obj['stream']['game'] + ')')
format(CHANNEL.title() + ' is streaming for ' + str(obj['stream']['viewers']) + ' viewers on ' + obj['stream']['channel']['url'], special=1)
def uptime():
user = hexchat.get_info('channel').strip('#')
url = 'https://api.twitch.tv/kraken/channels/' + user + '/videos?limit=1&broadcasts=true'
try:
obj = loadJSON('https://api.twitch.tv/kraken/streams/' + user)
if (obj['stream'] == None):
raise Exception
latestbroadcast = loadJSON(url)['videos'][0]['_id']
secondurl = 'https://api.twitch.tv/kraken/videos/' + latestbroadcast
starttimestring = loadJSON(secondurl)['recorded_at']
timeFormat = '%Y-%m-%dT%H:%M:%SZ'
startdate = datetime.strptime(starttimestring, timeFormat)
currentdate = datetime.utcnow()
combineddate = currentdate - startdate - timedelta(microseconds=currentdate.microsecond)
format(user.title() + ' has been streaming for ' + str(combineddate))
except Exception, ex:
format(user.title() + ' is not streaming.')
def checkStreams():
global firstRun
if (firstRun):
hexchat.unhook(timerHook)
hexchat.hook_timer(300000, checkStreams_cb)
firstRun = False
channels = hexchat.get_list('channels')
realChannels = []
for channel in channels:
if (channel.server == 'tmi.twitch.tv' and channel.channel[0] == '#'):
realChannels.append(channel.channel.strip('#'))
for channel in realChannels:
obj = loadJSON('https://api.twitch.tv/kraken/streams/' + channel)
if (obj['stream'] == None and channel in liveChannels):
liveChannels.remove(channel)
format(channel.title() + ' is not live anymore.')
elif (obj['stream'] != None and channel not in liveChannels):
liveChannels.append(channel)
format(channel.title() + ' is live!')
format(obj['stream']['channel']['status'] + '(' + obj['stream']['game'] + ')')
elif (obj['stream'] == None and channel not in liveChannels):
format(channel.title() + ' is offline.')
def stream_cb(word, word_eol, userdata):
stream()
def uptime_cb(word, word_eol, userdata):
uptime()
def checkStreams_cb(userdata):
checkStreams()
def checkStream_cb(word, word_eol, userdata):
timerHook = hexchat.hook_timer(3000, checkStreams_cb)
def whisper_cb(word, word_eol, userdata):
hexchat.command('PRIVMSG %s :. /w %s' % (hexchat.get_info('channel'), word_eol[1].encode('utf-8')))
whisper = hexchat.get_info('nick') + ' >> ' + word[1] + ': ' + word_eol[2]
hexchat.prnt('\002\00326' + whisper.encode('utf-8') + '\002\00326')
return hexchat.EAT_ALL
hexchat.hook_server('PRIVMSG', checkmessage_cb)
hexchat.hook_server('421', servererr_cb)
hexchat.hook_command('STREAM', stream_cb, help='/STREAM Use in twitch.tv chats to check if the stream is online')
hexchat.hook_command('UPTIME', uptime_cb)
hexchat.hook_command('JOIN', checkStream_cb)
hexchat.hook_command('W', whisper_cb)
hexchat.hook_unload(unload_cb)
timerHook = 0
hexchat.prnt('\003' + __module_name__ + ' ' + __module_version__ + ' loaded\003')