]>
Commit | Line | Data |
---|---|---|
a83e1f9c | 1 | # Erebus IRC bot - Author: Erebus Team |
4477123d | 2 | # vim: fileencoding=utf-8 |
a83e1f9c | 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', | |
fa93b933 | 10 | 'compatible': [0], |
a62d0d18 | 11 | 'depends': [], |
12 | 'softdeps': [], | |
a83e1f9c | 13 | } |
14 | ||
99366200 CS |
15 | # http://embed.ly/tools/generator |
16 | ||
a83e1f9c | 17 | # preamble |
18 | import modlib | |
19 | lib = modlib.modlib(__name__) | |
20 | modstart = lib.modstart | |
21 | modstop = lib.modstop | |
22 | ||
23 | # module code | |
a28e2ae9 | 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 | |
a83e1f9c | 37 | |
390fbad4 | 38 | html_parser = HTMLParser.HTMLParser() |
a83e1f9c | 39 | |
390fbad4 | 40 | hostmask_regex = re.compile(r'^(.*)!(.*)@(.*)$') |
c6880712 | 41 | url_regex = re.compile(r'https?://[^/\s]+\.[^/\s]+(?:/\S+)?') |
a83e1f9c | 42 | spotify_regex = ( |
43 | re.compile(r'spotify:(?P<type>\w+):(?P<track_id>\w{22})'), | |
c6880712 | 44 | re.compile(r'https?://open.spotify.com/(?P<type>\w+)/(?P<track_id>\w+)') |
a83e1f9c | 45 | ) |
46 | youtube_regex = ( | |
47 | re.compile(r'https?://(?:www\.)?youtube\.com/watch\?[a-zA-Z0-9=&_\-]+'), | |
48 | ) | |
49 | twitch_regex = ( | |
01a6184a | 50 | re.compile(r'https?:\/\/(?:www\.)?twitch.tv\/([A-Za-z0-9]*)'), |
a83e1f9c | 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 | ||
394a7b69 CS |
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 | ||
a83e1f9c | 90 | @lib.hooknum("PRIVMSG") |
390fbad4 CS |
91 | def privmsg_hook(bot, textline): |
92 | user = parser_hostmask(textline[1:textline.find(' ')]) | |
93 | chan = textline.split()[2] | |
a83e1f9c | 94 | |
95 | try: | |
390fbad4 | 96 | line = textline.split(None, 3)[3][1:] |
a83e1f9c | 97 | except IndexError: |
390fbad4 | 98 | line = '' |
a83e1f9c | 99 | |
390fbad4 CS |
100 | for match in url_regex.findall(line): |
101 | if match: | |
c6880712 | 102 | response = goturl(match) |
103 | if response is not None: | |
104 | bot.msg(chan, response) | |
a83e1f9c | 105 | |
390fbad4 CS |
106 | def unescape(line): |
107 | return html_parser.unescape(line) | |
a83e1f9c | 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() | |
390fbad4 | 112 | soup = BeautifulSoup(xml, convertEntities=BeautifulSoup.HTML_ENTITIES) |
a83e1f9c | 113 | lookup_type = soup.contents[2].name |
390fbad4 | 114 | |
a83e1f9c | 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 | |
390fbad4 | 125 | |
dafa38fc | 126 | return unescape('Track: %s - %s / %s %s:%.2d %2d%%' % (artist_name, name, album_name, minutes, seconds, popularity)) |
390fbad4 | 127 | |
a83e1f9c | 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 | |
dafa38fc | 132 | return unescape('Album: %s - %s - %s' % (artist_name, album_name, released)) |
390fbad4 | 133 | |
a83e1f9c | 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 | ||
dafa38fc | 149 | return unescape("Youtube: %s (%s)" % (title, author)) |
a83e1f9c | 150 | except: |
151 | pass | |
152 | ||
390fbad4 CS |
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: | |
dafa38fc | 158 | return unescape('Twitch: %s (%s playing %s)' % (twitch[0]['channel']['status'], twitch[0]['channel']['login'], twitch[0]['channel']['meta_game'])) |
390fbad4 CS |
159 | except: |
160 | return 'Twitch: Channel offline.' | |
161 | ||
162 | def goturl(url): | |
394a7b69 CS |
163 | request = urllib2.Request(url) |
164 | opener = urllib2.build_opener(SmartRedirectHandler()) | |
993046cc | 165 | try: |
394a7b69 | 166 | soup = BeautifulSoup(opener.open(request, timeout=2)) |
dafa38fc | 167 | return unescape('Title: %s' % (soup.title.string)) |
993046cc | 168 | except: |
c6880712 | 169 | return None |