]>
Commit | Line | Data |
---|---|---|
9bd05cf6 | 1 | # Erebus IRC bot - Author: Erebus Team |
c6b4a177 | 2 | # Youtube URL Checker |
c3502e73 CS |
3 | # This file is released into the public domain; see http://unlicense.org/ |
4 | ||
5 | # module info | |
6 | modinfo = { | |
9bd05cf6 | 7 | 'author': 'Erebus Team', |
c3502e73 CS |
8 | 'license': 'public domain', |
9 | 'compatible': [1], # compatible module API versions | |
10 | 'depends': [], # other modules required to work properly? | |
11 | } | |
12 | ||
13 | # preamble | |
14 | import modlib | |
15 | lib = modlib.modlib(__name__) | |
16 | modstart = lib.modstart | |
17 | modstop = lib.modstop | |
18 | ||
19 | # module code | |
20 | import re | |
5dc1eacf | 21 | import json |
c3502e73 | 22 | import urllib2 |
5dc1eacf | 23 | import urlparse |
c3502e73 CS |
24 | import HTMLParser |
25 | from BeautifulSoup import BeautifulSoup | |
26 | ||
27 | checkfor = "youtube" | |
c3502e73 | 28 | url_regex = re.compile('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+') |
5dc1eacf | 29 | yturl_regex = re.compile('(http|ftp|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?') |
c3502e73 CS |
30 | |
31 | @lib.hooknum("PRIVMSG") | |
32 | def privmsg_hook(bot, line): | |
c3502e73 CS |
33 | try: |
34 | linetx = line.split(None, 3)[3][1:] | |
35 | except IndexError: | |
36 | linetx = '' | |
37 | ||
38 | if checkfor not in line: | |
39 | return # doesn't concern us | |
40 | ||
41 | for url in url_regex.findall(linetx): | |
42 | if checkfor in url: | |
5dc1eacf CS |
43 | url_data = urlparse.urlparse(url) |
44 | query = urlparse.parse_qs(url_data.query) | |
45 | video = query["v"][0] | |
46 | api_url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % video | |
472ca064 CS |
47 | try: |
48 | respdata = urllib2.urlopen(api_url).read() | |
49 | video_info = json.loads(respdata) | |
5dc1eacf | 50 | |
472ca064 CS |
51 | title = video_info['entry']['title']["$t"] |
52 | author = video_info['entry']['author'][0]['name']['$t'] | |
5dc1eacf | 53 | |
472ca064 CS |
54 | bot.msg(line.split()[2], "Youtube: %s (%s)" % (title, author)) |
55 | except: | |
56 | pass |