]> jfr.im git - erebus.git/blame - modules/twitch.py
Merge branch 'master' of kronos.jfr.im:erebus
[erebus.git] / modules / twitch.py
CommitLineData
9bd05cf6 1# Erebus IRC bot - Author: Erebus Team
c6b4a177 2# Twitch URL Checker
485a2596
CS
3# This file is released into the public domain; see http://unlicense.org/
4
5# module info
6modinfo = {
9bd05cf6 7 'author': 'Erebus Team',
485a2596
CS
8 'license': 'public domain',
9 'compatible': [1], # compatible module API versions
10 'depends': [], # other modules required to work properly?
11}
12
13# preamble
14import modlib
15lib = modlib.modlib(__name__)
16modstart = lib.modstart
17modstop = lib.modstop
18
19# module code
20import re
21import urllib2
22import json
485a2596
CS
23
24checkfor = "twitch"
485a2596 25url_regex = re.compile('(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?')
485a2596
CS
26
27@lib.hooknum("PRIVMSG")
28def privmsg_hook(bot, line):
485a2596
CS
29 try:
30 linetx = line.split(None, 3)[3][1:]
31 except IndexError:
32 linetx = ''
33
34 if checkfor not in line:
35 return # doesn't concern us
36
37 for p, h, c in url_regex.findall(linetx):
38 if checkfor in h:
39 url = 'http://api.justin.tv/api/stream/list.json?channel=%s' % c[1:]
40 respdata = urllib2.urlopen(url).read()
41 twitch = json.loads(respdata)
42 try:
233f40c3 43 bot.msg(line.split()[2], 'Twitch: %s (%s playing %s)' % (twitch[0]['channel']['status'], twitch[0]['channel']['login'], twitch[0]['channel']['meta_game']))
485a2596
CS
44 except:
45 bot.msg(line.split()[2], 'Twitch: Channel offline.')