rewrite youtube plugin again to use jsonc interface and report more information

This commit is contained in:
Ryan Hitchman 2010-02-13 18:11:42 -07:00
parent 51e24dadb5
commit 56c1119443
1 changed files with 25 additions and 16 deletions

View File

@ -1,39 +1,48 @@
import json import json
import locale import locale
import re import re
import time
import urllib2 import urllib2
from util import hook from util import hook
locale.setlocale(locale.LC_ALL, '') locale.setlocale(locale.LC_ALL, '')
youtube_re = re.compile(r'.*youtube.*v=([-_a-z0-9]+)', flags=re.I) youtube_re = re.compile(r'youtube.*?v=([-_a-z0-9]+)', flags=re.I)
url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json' url = 'http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc'
#@hook.command(hook=r'(.*)', prefix=False) #@hook.command(hook=r'(.*)', prefix=False)
def youtube(inp): def youtube(inp):
m = youtube_re.match(inp) m = youtube_re.search(inp)
if m: if m:
j = json.load(urllib2.urlopen(url % m.group(1)))['entry'] j = json.load(urllib2.urlopen(url % m.group(1)))
out = '\x02%s\x02' % j['title']['$t'] if j.get('error'):
return
rating = j.get('gd$rating') j = j['data']
if rating:
out += ' - rated \x02%.2f/%.1f\x02 (%d)' % (rating['average'],
rating['max'], rating['numRaters'])
stats = j.get('yt$statistics') out = '\x02%s\x02' % j['title']
if stats:
view_count = int(stats['viewCount'])
out += ' - views \x02%s\x02' % locale.format('%d', view_count, 1)
out += ' - length \x02' out += ' - length \x02'
length = int(j['media$group']['yt$duration']['seconds']) length = j['duration']
if length / 3600: # > 1 hour if length / 3600: # > 1 hour
out += '%dh ' % (length / 3600) out += '%dh ' % (length / 3600)
if length / 60: if length / 60:
out += '%dm ' % (length / 60 % 60) out += '%dm ' % (length / 60 % 60)
out += "%ds" % (length % 60) out += "%ds\x02" % (length % 60)
if 'rating' in j:
out += ' - rated \x02%.2f/5.0\x02 (%d)' % (j['rating'],
j['ratingCount'])
if 'viewCount' in j:
out += ' - \x02%s\x02 views' % locale.format('%d',
j['viewCount'], 1)
upload_time = time.strptime(j['uploaded'], "%Y-%m-%dT%H:%M:%S.000Z")
out += ' - \x02%s\x02 on \x02%s\x02' % (j['uploader'],
time.strftime("%Y.%m.%d", upload_time))
return out return out