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