]>
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', | |
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 |
16 | import modlib | |
17 | lib = modlib.modlib(__name__) | |
18 | modstart = lib.modstart | |
19 | modstop = lib.modstop | |
20 | ||
21 | # module code | |
22 | import re, json, urllib2, urlparse, HTMLParser | |
23 | from BeautifulSoup import BeautifulSoup | |
24 | ||
25 | hostmask_regex = re.compile(r'^(.*)!(.*)@(.*)$') | |
26 | ||
27 | spotify_regex = ( | |
28 | re.compile(r'spotify:(?P<type>\w+):(?P<track_id>\w{22})'), | |
29 | re.compile(r'http://open.spotify.com/(?P<type>\w+)/(?P<track_id>\w{22})') | |
30 | ) | |
31 | youtube_regex = ( | |
32 | re.compile(r'https?://(?:www\.)?youtube\.com/watch\?[a-zA-Z0-9=&_\-]+'), | |
33 | ) | |
34 | twitch_regex = ( | |
35 | re.compile('(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?'), #TODO | |
36 | ) | |
37 | ||
38 | def parser_hostmask(hostmask): | |
39 | if isinstance(hostmask, dict): | |
40 | return hostmask | |
41 | ||
42 | nick = None | |
43 | user = None | |
44 | host = None | |
45 | ||
46 | if hostmask is not None: | |
47 | match = hostmask_regex.match(hostmask) | |
48 | ||
49 | if not match: | |
50 | nick = hostmask | |
51 | else: | |
52 | nick = match.group(1) | |
53 | user = match.group(2) | |
54 | host = match.group(3) | |
55 | ||
56 | return { | |
57 | 'nick': nick, | |
58 | 'user': user, | |
59 | 'host': host | |
60 | } | |
61 | ||
62 | @lib.hooknum("PRIVMSG") | |
63 | def privmsg_hook(bot, line): | |
64 | sender = parser_hostmask(line[1:line.find(' ')]) | |
65 | ||
66 | try: | |
67 | linetx = line.split(None, 3)[3][1:] | |
68 | except IndexError: | |
69 | linetx = '' | |
70 | ||
71 | chan = line.split()[2] | |
72 | ||
73 | if 'open.spotify.com' in line or 'spotify:' in line: | |
74 | for r in spotify_regex: | |
75 | for sptype, track in r.findall(linetx): | |
76 | bot.msg(chan, gotspotify(sptype, track)) | |
77 | ||
78 | elif 'youtube.com' in line or 'youtu.be' in line: | |
79 | print "got youtube!" | |
80 | for r in youtube_regex: | |
81 | for url in r.findall(linetx): | |
82 | bot.msg(chan, gotyoutube(url)) | |
83 | ||
84 | elif 'twitch.tv' in line: pass #TODO fix twitch | |
85 | ||
86 | else: pass #TODO generic <title> checker | |
87 | ||
88 | ||
89 | def gotspotify(type, track): | |
90 | url = 'http://ws.spotify.com/lookup/1/?uri=spotify:%s:%s' % (type, track) | |
91 | xml = urllib2.urlopen(url).read() | |
92 | soup = BeautifulSoup(xml) | |
93 | lookup_type = soup.contents[2].name | |
94 | ||
95 | if lookup_type == 'track': | |
96 | name = soup.find('name').string | |
97 | album_name = soup.find('album').find('name').string | |
98 | artist_name = soup.find('artist').find('name').string | |
99 | popularity = soup.find('popularity') | |
100 | if popularity: | |
101 | popularity = float(popularity.string)*100 | |
102 | length = float(soup.find('length').string) | |
103 | minutes = int(length)/60 | |
104 | seconds = int(length)%60 | |
105 | ||
106 | return 'Track: %s - %s / %s %s:%.2d %2d%%' % (artist_name, name, album_name, minutes, seconds, popularity) | |
107 | ||
108 | elif lookup_type == 'album': | |
109 | album_name = soup.find('album').find('name').string | |
110 | artist_name = soup.find('artist').find('name').string | |
111 | released = soup.find('released').string | |
112 | return 'Album: %s - %s - %s' % (artist_name, album_name, released) | |
113 | ||
114 | else: | |
115 | return 'Unsupported type.' | |
116 | ||
117 | def gotyoutube(url): | |
118 | url_data = urlparse.urlparse(url) | |
119 | query = urlparse.parse_qs(url_data.query) | |
120 | video = query["v"][0] | |
121 | api_url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % video | |
122 | try: | |
123 | respdata = urllib2.urlopen(api_url).read() | |
124 | video_info = json.loads(respdata) | |
125 | ||
126 | title = video_info['entry']['title']["$t"] | |
127 | author = video_info['entry']['author'][0]['name']['$t'] | |
128 | ||
129 | return "Youtube: %s (%s)" % (title, author) | |
130 | except: | |
131 | pass | |
132 | ||
133 | ||
134 | def gottwitch(url): | |
135 | return "" | |
136 | #FIXME: | |
137 | try: | |
138 | linetx = line.split(None, 3)[3][1:] | |
139 | except IndexError: | |
140 | linetx = '' | |
141 | ||
142 | if checkfor not in line: | |
143 | return # doesn't concern us | |
144 | ||
145 | for p, h, c in url_regex.findall(linetx): | |
146 | if checkfor in h: | |
147 | url = 'http://api.justin.tv/api/stream/list.json?channel=%s' % c[1:] | |
148 | respdata = urllib2.urlopen(url).read() | |
149 | twitch = json.loads(respdata) | |
150 | try: | |
151 | bot.msg(line.split()[2], 'Twitch: %s (%s playing %s)' % (twitch[0]['channel']['status'], twitch[0]['channel']['login'], twitch[0]['channel']['meta_game'])) | |
152 | except: | |
153 | bot.msg(line.split()[2], 'Twitch: Channel offline.') | |
99366200 | 154 |