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