From 51e5744d2617871be2253a0e31a8cbab3d2351fe Mon Sep 17 00:00:00 2001 From: melonhead Date: Mon, 1 Mar 2010 01:08:54 +0000 Subject: [PATCH] Moved youtube search method inside youtube.py --- plugins/youtube.py | 22 ++++++++++++++++++++++ plugins/youtubesearch.py | 31 ------------------------------- 2 files changed, 22 insertions(+), 31 deletions(-) delete mode 100644 plugins/youtubesearch.py diff --git a/plugins/youtube.py b/plugins/youtube.py index 4a516c9..e1c4373 100644 --- a/plugins/youtube.py +++ b/plugins/youtube.py @@ -5,12 +5,17 @@ import time import urllib2 from util import hook +from urllib import quote_plus + locale.setlocale(locale.LC_ALL, '') youtube_re = re.compile(r'youtube.*?v=([-_a-z0-9]+)', flags=re.I) url = 'http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc' +search_api_url = "http://gdata.youtube.com/feeds/api/videos?q=%s&max-results=1&alt=json" +video_url = "http://youtube.com/watch?v=%s" + def get_video_description(vid): print vid j = json.load(urllib2.urlopen(url % vid)) @@ -54,3 +59,20 @@ def youtube(inp): m = youtube_re.search(inp) if m: return get_video_description(m.group(1)) + +@hook.command +@hook.command('y') +def youtube(inp): + '.youtube -- returns the first YouTube search result for ' + inp = quote_plus(inp) + j = json.load(urllib2.urlopen(search_api_url % (inp))) + if j.get('error'): + return + + try: + vid = j['feed']['entry'][0]['id']['$t'] + #youtube returns a gdata url for this some reason. The videoid has to be stripped out + vid = vid[vid.rfind('/')+1:] + return get_video_description(vid) + " " + video_url%vid + except: + return diff --git a/plugins/youtubesearch.py b/plugins/youtubesearch.py deleted file mode 100644 index d5916c9..0000000 --- a/plugins/youtubesearch.py +++ /dev/null @@ -1,31 +0,0 @@ -import json -import locale -import re -import time -import urllib2 -from urllib import quote_plus - -from util import hook -from youtube import get_video_description - -locale.setlocale(locale.LC_ALL, '') - -url = "http://gdata.youtube.com/feeds/api/videos?q=%s&max-results=1&alt=json" -video_url = "http://youtube.com/watch?v=%s" - -@hook.command -@hook.command('y') -def youtube(inp): - '.youtube -- returns the first YouTube search result for ' - inp = quote_plus(inp) - j = json.load(urllib2.urlopen(url % (inp))) - if j.get('error'): - return - - try: - vid = j['feed']['entry'][0]['id']['$t'] - #youtube returns a gdata url for this some reason. The videoid has to be stripped out - vid = vid[vid.rfind('/')+1:] - return get_video_description(vid) + " " + video_url%vid - except: - return