]> jfr.im git - erebus.git/blob - modules/twitch.py
Fixed header
[erebus.git] / modules / twitch.py
1 # Erebus IRC bot - Author: Conny Sjoblom
2 # Twitch URL Checker
3 # This file is released into the public domain; see http://unlicense.org/
4
5 # module info
6 modinfo = {
7 'author': 'Conny Sjoblom',
8 'license': 'public domain',
9 'compatible': [1], # compatible module API versions
10 'depends': [], # other modules required to work properly?
11 }
12
13 # preamble
14 import modlib
15 lib = modlib.modlib(__name__)
16 modstart = lib.modstart
17 modstop = lib.modstop
18
19 # module code
20 import re
21 import urllib2
22 import json
23
24 checkfor = "twitch"
25 hostmask_regex = re.compile('^(.*)!(.*)@(.*)$')
26 url_regex = re.compile('(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?')
27 def parser_hostmask(hostmask):
28 if isinstance(hostmask, dict):
29 return hostmask
30
31 nick = None
32 user = None
33 host = None
34
35 if hostmask is not None:
36 match = hostmask_regex.match(hostmask)
37
38 if not match:
39 nick = hostmask
40 else:
41 nick = match.group(1)
42 user = match.group(2)
43 host = match.group(3)
44
45 return {
46 'nick': nick,
47 'user': user,
48 'host': host
49 }
50
51 @lib.hooknum("PRIVMSG")
52 def privmsg_hook(bot, line):
53 sender = parser_hostmask(line[1:line.find(' ')])
54
55 try:
56 linetx = line.split(None, 3)[3][1:]
57 except IndexError:
58 linetx = ''
59
60 if checkfor not in line:
61 return # doesn't concern us
62
63 for p, h, c in url_regex.findall(linetx):
64 if checkfor in h:
65 url = 'http://api.justin.tv/api/stream/list.json?channel=%s' % c[1:]
66 respdata = urllib2.urlopen(url).read()
67 twitch = json.loads(respdata)
68 try:
69 bot.msg(line.split()[2], 'Twitch: %s (%s)' % (twitch[0]['channel']['status'], twitch[0]['channel']['meta_game']))
70 except:
71 bot.msg(line.split()[2], 'Twitch: Channel offline.')