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