]>
Commit | Line | Data |
---|---|---|
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 | |
6 | modinfo = { | |
7 | 'author': 'Erebus Team', | |
8 | 'license': 'public domain', | |
f5aec865 | 9 | 'compatible': [2], |
a62d0d18 | 10 | 'depends': [], |
11 | 'softdeps': [], | |
a83e1f9c | 12 | } |
13 | ||
99366200 CS |
14 | # http://embed.ly/tools/generator |
15 | ||
a83e1f9c | 16 | # preamble |
17 | import modlib | |
18 | lib = modlib.modlib(__name__) | |
19 | modstart = lib.modstart | |
20 | modstop = lib.modstop | |
21 | ||
22 | # module code | |
390fbad4 | 23 | import re, urllib2, urlparse, json, HTMLParser |
a83e1f9c | 24 | from BeautifulSoup import BeautifulSoup |
25 | ||
390fbad4 | 26 | html_parser = HTMLParser.HTMLParser() |
a83e1f9c | 27 | |
390fbad4 CS |
28 | hostmask_regex = re.compile(r'^(.*)!(.*)@(.*)$') |
29 | url_regex = re.compile(r'((?:https?://|spotify:)[^\s]+)') | |
a83e1f9c | 30 | spotify_regex = ( |
31 | re.compile(r'spotify:(?P<type>\w+):(?P<track_id>\w{22})'), | |
390fbad4 | 32 | re.compile(r'https?://open.spotify.com/(?P<type>\w+)/(?P<track_id>\w{22})') |
a83e1f9c | 33 | ) |
34 | youtube_regex = ( | |
35 | re.compile(r'https?://(?:www\.)?youtube\.com/watch\?[a-zA-Z0-9=&_\-]+'), | |
36 | ) | |
37 | twitch_regex = ( | |
01a6184a | 38 | re.compile(r'https?:\/\/(?:www\.)?twitch.tv\/([A-Za-z0-9]*)'), |
a83e1f9c | 39 | ) |
40 | ||
41 | def 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 |
65 | class 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 |
79 | def 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: | |
390fbad4 CS |
90 | if 'open.spotify.com' in match or 'spotify:' in match: |
91 | for r in spotify_regex: | |
92 | for sptype, track in r.findall(match): | |
dafa38fc | 93 | bot.msg(chan, gotspotify(sptype, track)) |
a83e1f9c | 94 | |
390fbad4 CS |
95 | elif 'youtube.com' in match or 'youtu.be' in match: |
96 | for r in youtube_regex: | |
97 | for url in r.findall(match): | |
dafa38fc | 98 | bot.msg(chan, gotyoutube(url)) |
a83e1f9c | 99 | |
390fbad4 CS |
100 | elif 'twitch.tv' in match: |
101 | for r in twitch_regex: | |
102 | for uri in r.findall(match): | |
dafa38fc | 103 | bot.msg(chan, gottwitch(uri)) |
a83e1f9c | 104 | |
390fbad4 | 105 | else: |
dafa38fc | 106 | bot.msg(chan, goturl(match)) |
a83e1f9c | 107 | |
390fbad4 CS |
108 | def unescape(line): |
109 | return html_parser.unescape(line) | |
a83e1f9c | 110 | |
111 | def gotspotify(type, track): | |
112 | url = 'http://ws.spotify.com/lookup/1/?uri=spotify:%s:%s' % (type, track) | |
113 | xml = urllib2.urlopen(url).read() | |
390fbad4 | 114 | soup = BeautifulSoup(xml, convertEntities=BeautifulSoup.HTML_ENTITIES) |
a83e1f9c | 115 | lookup_type = soup.contents[2].name |
390fbad4 | 116 | |
a83e1f9c | 117 | if lookup_type == 'track': |
118 | name = soup.find('name').string | |
119 | album_name = soup.find('album').find('name').string | |
120 | artist_name = soup.find('artist').find('name').string | |
121 | popularity = soup.find('popularity') | |
122 | if popularity: | |
123 | popularity = float(popularity.string)*100 | |
124 | length = float(soup.find('length').string) | |
125 | minutes = int(length)/60 | |
126 | seconds = int(length)%60 | |
390fbad4 | 127 | |
dafa38fc | 128 | return unescape('Track: %s - %s / %s %s:%.2d %2d%%' % (artist_name, name, album_name, minutes, seconds, popularity)) |
390fbad4 | 129 | |
a83e1f9c | 130 | elif lookup_type == 'album': |
131 | album_name = soup.find('album').find('name').string | |
132 | artist_name = soup.find('artist').find('name').string | |
133 | released = soup.find('released').string | |
dafa38fc | 134 | return unescape('Album: %s - %s - %s' % (artist_name, album_name, released)) |
390fbad4 | 135 | |
a83e1f9c | 136 | else: |
137 | return 'Unsupported type.' | |
138 | ||
139 | def gotyoutube(url): | |
140 | url_data = urlparse.urlparse(url) | |
141 | query = urlparse.parse_qs(url_data.query) | |
142 | video = query["v"][0] | |
143 | api_url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % video | |
144 | try: | |
145 | respdata = urllib2.urlopen(api_url).read() | |
146 | video_info = json.loads(respdata) | |
147 | ||
148 | title = video_info['entry']['title']["$t"] | |
149 | author = video_info['entry']['author'][0]['name']['$t'] | |
150 | ||
dafa38fc | 151 | return unescape("Youtube: %s (%s)" % (title, author)) |
a83e1f9c | 152 | except: |
153 | pass | |
154 | ||
390fbad4 CS |
155 | def gottwitch(uri): |
156 | url = 'http://api.justin.tv/api/stream/list.json?channel=%s' % uri.split('/')[0] | |
157 | respdata = urllib2.urlopen(url).read() | |
158 | twitch = json.loads(respdata) | |
159 | try: | |
dafa38fc | 160 | return unescape('Twitch: %s (%s playing %s)' % (twitch[0]['channel']['status'], twitch[0]['channel']['login'], twitch[0]['channel']['meta_game'])) |
390fbad4 CS |
161 | except: |
162 | return 'Twitch: Channel offline.' | |
163 | ||
164 | def goturl(url): | |
394a7b69 CS |
165 | request = urllib2.Request(url) |
166 | opener = urllib2.build_opener(SmartRedirectHandler()) | |
993046cc | 167 | try: |
394a7b69 | 168 | soup = BeautifulSoup(opener.open(request, timeout=2)) |
dafa38fc | 169 | return unescape('Title: %s' % (soup.title.string)) |
993046cc | 170 | except: |
394a7b69 | 171 | return 'Invalid URL/Timeout' |