From ab8f316eb9702ad8781ba629bffe8ed5919df71c Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Sun, 28 Feb 2010 18:39:29 -0700 Subject: [PATCH] prettify gcalc, make youtube search use jsonc interface --- plugins/gcalc.py | 47 +++++++++++++++++++++++----------------------- plugins/youtube.py | 32 ++++++++++++++++--------------- 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/plugins/gcalc.py b/plugins/gcalc.py index 521b12e..d92260c 100644 --- a/plugins/gcalc.py +++ b/plugins/gcalc.py @@ -1,27 +1,28 @@ +import urllib2 +import re + +from lxml import html + from util import hook -import urllib, httplib, sys - -def doquery(argv): - query=urllib.urlencode({'q':argv}) - - start='

' - end='' - - google=httplib.HTTPConnection("www.google.com") - google.request("GET","/search?"+query) - search=google.getresponse() - data=search.read() - - if data.find(start)==-1: return "Could not calculate " + argv - else: - begin=data.index(start) - result=data[begin+len(start):begin+data[begin:].index(end)] - result = result.replace(" ",",").replace(" × 10","E").replace("","").replace("\xa0",",") - return result @hook.command def calc(inp): - '''.calc -- returns Google Calculator result''' - if not inp or not inp.strip(): - return calc.__doc__ - return doquery(inp) + '''.calc -- returns Google Calculator result''' + if not inp: + return calc.__doc__ + + url = "http://www.google.com/search?q=" + request = urllib2.Request(url + urllib2.quote(inp, '')) + request.add_header('User-Agent', 'skybot') + page = urllib2.build_opener().open(request).read() + + # ugh, scraping HTML with regexes + m = re.search(r'

(.*?)', page) + + if m is None: + return "could not calculate " + inp + + result = m.group(1).replace(" ",",") + result = result.replace(" × 10","E").replace("","") + result = result.replace("\xa0",",") + return result diff --git a/plugins/youtube.py b/plugins/youtube.py index 8561f8b..a3a3502 100644 --- a/plugins/youtube.py +++ b/plugins/youtube.py @@ -11,13 +11,14 @@ 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" +base_url = 'http://gdata.youtube.com/feeds/api/' +url = base_url + 'videos/%s?v=2&alt=jsonc' +search_api_url = base_url + 'videos?v=2&alt=jsonc&max-results=1&q=%s' video_url = "http://youtube.com/watch?v=%s" -def get_video_description(vid): - j = json.load(urllib2.urlopen(url % vid)) +def get_video_description(vid_id): + j = json.load(urllib2.urlopen(url % vid_id)) if j.get('error'): return @@ -59,19 +60,20 @@ def youtube_url(inp): if m: return get_video_description(m.group(1)) -@hook.command + @hook.command('y') +@hook.command 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 + + if 'error' in j: + return 'error performing search' + + if j['data']['totalItems'] == 0: + return 'no results found' + + vid_id = j['data']['items'][0]['id'] + + return get_video_description(vid_id) + " - " + video_url % vid_id