]> jfr.im git - erebus.git/blame - modules/urls.py
control - added !INJECT
[erebus.git] / modules / urls.py
CommitLineData
a83e1f9c 1# Erebus IRC bot - Author: Erebus Team
2# URL Checker
3# This file is released into the public domain; see http://unlicense.org/
4
5# module info
6modinfo = {
7 'author': 'Erebus Team',
8 'license': 'public domain',
fa93b933 9 'compatible': [0],
a62d0d18 10 'depends': [],
11 'softdeps': [],
a83e1f9c 12}
13
99366200
CS
14# http://embed.ly/tools/generator
15
a83e1f9c 16# preamble
17import modlib
18lib = modlib.modlib(__name__)
19modstart = lib.modstart
20modstop = lib.modstop
21
22# module code
390fbad4 23import re, urllib2, urlparse, json, HTMLParser
a83e1f9c 24from BeautifulSoup import BeautifulSoup
25
390fbad4 26html_parser = HTMLParser.HTMLParser()
a83e1f9c 27
390fbad4 28hostmask_regex = re.compile(r'^(.*)!(.*)@(.*)$')
c6880712 29url_regex = re.compile(r'https?://[^/\s]+\.[^/\s]+(?:/\S+)?')
a83e1f9c 30spotify_regex = (
31 re.compile(r'spotify:(?P<type>\w+):(?P<track_id>\w{22})'),
c6880712 32 re.compile(r'https?://open.spotify.com/(?P<type>\w+)/(?P<track_id>\w+)')
a83e1f9c 33)
34youtube_regex = (
35 re.compile(r'https?://(?:www\.)?youtube\.com/watch\?[a-zA-Z0-9=&_\-]+'),
36)
37twitch_regex = (
01a6184a 38 re.compile(r'https?:\/\/(?:www\.)?twitch.tv\/([A-Za-z0-9]*)'),
a83e1f9c 39)
40
41def parser_hostmask(hostmask):
42 if isinstance(hostmask, dict):
43 return hostmask
44
45 nick = None
46 user = None
47 host = None
48
49 if hostmask is not None:
50 match = hostmask_regex.match(hostmask)
51
52 if not match:
53 nick = hostmask
54 else:
55 nick = match.group(1)
56 user = match.group(2)
57 host = match.group(3)
58
59 return {
60 'nick': nick,
61 'user': user,
62 'host': host
63 }
64
394a7b69
CS
65class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
66 def http_error_301(self, req, fp, code, msg, headers):
67 result = urllib2.HTTPRedirectHandler.http_error_301(
68 self, req, fp, code, msg, headers)
69 result.status = code
70 return result
71
72 def http_error_302(self, req, fp, code, msg, headers):
73 result = urllib2.HTTPRedirectHandler.http_error_302(
74 self, req, fp, code, msg, headers)
75 result.status = code
76 return result
77
a83e1f9c 78@lib.hooknum("PRIVMSG")
390fbad4
CS
79def privmsg_hook(bot, textline):
80 user = parser_hostmask(textline[1:textline.find(' ')])
81 chan = textline.split()[2]
a83e1f9c 82
83 try:
390fbad4 84 line = textline.split(None, 3)[3][1:]
a83e1f9c 85 except IndexError:
390fbad4 86 line = ''
a83e1f9c 87
390fbad4
CS
88 for match in url_regex.findall(line):
89 if match:
c6880712 90 response = goturl(match)
91 if response is not None:
92 bot.msg(chan, response)
a83e1f9c 93
390fbad4
CS
94def unescape(line):
95 return html_parser.unescape(line)
a83e1f9c 96
97def gotspotify(type, track):
98 url = 'http://ws.spotify.com/lookup/1/?uri=spotify:%s:%s' % (type, track)
99 xml = urllib2.urlopen(url).read()
390fbad4 100 soup = BeautifulSoup(xml, convertEntities=BeautifulSoup.HTML_ENTITIES)
a83e1f9c 101 lookup_type = soup.contents[2].name
390fbad4 102
a83e1f9c 103 if lookup_type == 'track':
104 name = soup.find('name').string
105 album_name = soup.find('album').find('name').string
106 artist_name = soup.find('artist').find('name').string
107 popularity = soup.find('popularity')
108 if popularity:
109 popularity = float(popularity.string)*100
110 length = float(soup.find('length').string)
111 minutes = int(length)/60
112 seconds = int(length)%60
390fbad4 113
dafa38fc 114 return unescape('Track: %s - %s / %s %s:%.2d %2d%%' % (artist_name, name, album_name, minutes, seconds, popularity))
390fbad4 115
a83e1f9c 116 elif lookup_type == 'album':
117 album_name = soup.find('album').find('name').string
118 artist_name = soup.find('artist').find('name').string
119 released = soup.find('released').string
dafa38fc 120 return unescape('Album: %s - %s - %s' % (artist_name, album_name, released))
390fbad4 121
a83e1f9c 122 else:
123 return 'Unsupported type.'
124
125def gotyoutube(url):
126 url_data = urlparse.urlparse(url)
127 query = urlparse.parse_qs(url_data.query)
128 video = query["v"][0]
129 api_url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % video
130 try:
131 respdata = urllib2.urlopen(api_url).read()
132 video_info = json.loads(respdata)
133
134 title = video_info['entry']['title']["$t"]
135 author = video_info['entry']['author'][0]['name']['$t']
136
dafa38fc 137 return unescape("Youtube: %s (%s)" % (title, author))
a83e1f9c 138 except:
139 pass
140
390fbad4
CS
141def gottwitch(uri):
142 url = 'http://api.justin.tv/api/stream/list.json?channel=%s' % uri.split('/')[0]
143 respdata = urllib2.urlopen(url).read()
144 twitch = json.loads(respdata)
145 try:
dafa38fc 146 return unescape('Twitch: %s (%s playing %s)' % (twitch[0]['channel']['status'], twitch[0]['channel']['login'], twitch[0]['channel']['meta_game']))
390fbad4
CS
147 except:
148 return 'Twitch: Channel offline.'
149
150def goturl(url):
394a7b69
CS
151 request = urllib2.Request(url)
152 opener = urllib2.build_opener(SmartRedirectHandler())
993046cc 153 try:
394a7b69 154 soup = BeautifulSoup(opener.open(request, timeout=2))
dafa38fc 155 return unescape('Title: %s' % (soup.title.string))
993046cc 156 except:
c6880712 157 return None