]> jfr.im git - erebus.git/blame - modules/youtube.py
test
[erebus.git] / modules / youtube.py
CommitLineData
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
6modinfo = {
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
14import modlib
15lib = modlib.modlib(__name__)
16modstart = lib.modstart
17modstop = lib.modstop
18
19# module code
20import re
5dc1eacf 21import json
c3502e73 22import urllib2
5dc1eacf 23import urlparse
c3502e73
CS
24import HTMLParser
25from BeautifulSoup import BeautifulSoup
26
27checkfor = "youtube"
dd44c57f 28yturl_regex = re.compile(r'https?://(?:www\.)?youtube\.com/watch\?[a-zA-Z0-9=&]+')
c3502e73
CS
29
30@lib.hooknum("PRIVMSG")
31def privmsg_hook(bot, line):
c3502e73
CS
32 try:
33 linetx = line.split(None, 3)[3][1:]
34 except IndexError:
35 linetx = ''
36
37 if checkfor not in line:
38 return # doesn't concern us
39
dd44c57f 40 for url in yturl_regex.findall(linetx):
41 url_data = urlparse.urlparse(url)
42 query = urlparse.parse_qs(url_data.query)
43 video = query["v"][0]
44 api_url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % video
45 try:
46 respdata = urllib2.urlopen(api_url).read()
47 video_info = json.loads(respdata)
48
49 title = video_info['entry']['title']["$t"]
50 author = video_info['entry']['author'][0]['name']['$t']
51
52 bot.msg(line.split()[2], "Youtube: %s (%s)" % (title, author))
53 except:
54 pass