]> jfr.im git - irc/rizon/acid.git/blob - pyva/pyva/src/main/python/internets/api/google.py
Split pyva plugin into pyva.core and pyva.pyva
[irc/rizon/acid.git] / pyva / pyva / src / main / python / internets / api / google.py
1 import json
2 import re
3 from urllib import urlencode
4 from feed import HtmlFeed, XmlFeed, get_json
5 from utils import unescape
6
7 class Google(object):
8 def __init__(self):
9 pass
10
11 # Google shutdown their iGoogle "API",
12 #def calc(self, expr):
13 # url = 'http://www.google.com/ig/calculator?'
14 # url += urlencode({'q': expr})
15 # page = HtmlFeed(url) #, fake_ua=True)
16 # text = page.html()
17 # # Convert to valid JSON: {foo: "1"} -> {"foo" : "1"},
18 # # {"foo": "\x25"} -> {"foo": "\u0025"}
19 # result = re.sub("([a-z]+):", '"\\1" :', text)
20 # # XXX: HACK; using two substitutes instead of one, but I can't into regex
21 # result = re.sub('\\\\x([0-9a-f]{2})', '\\u00\\1', result)
22 # result = re.sub('\\\\x([0-9a-f]{1})', '\\u000\\1', result)
23 #
24 # result = unicode(result, 'unicode_escape')
25 # result = unescape(result)
26 # # Special case: fractions; those are <sup></sup> unicode-slash <sub></sub>
27 # result = re.sub(u'<sup>([^<]*)</sup>\u2044<sub>([^<]*)</sub>', ' \\1/\\2', result)
28 # # Also un-html-ify <sup>/</sup> because google doesn't send proper plaintext
29 # result = re.sub('<sup>([^<]*)</sup>', '^\\1', result)
30 # # Same with <sub>.
31 # result = re.sub('<sub>([^<]*)</sub>', '_\\1', result)
32 # result = json.loads(result.encode('utf-8'))
33 # return result
34
35 def search(self, query, userip=None):
36 url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&'
37 parameters = {'q': query}
38 if userip:
39 parameters['userip'] = userip
40 url += urlencode(parameters)
41 json = get_json(url)
42 return json
43
44 def yt_search(self, query, num=1):
45 url = 'https://gdata.youtube.com/feeds/api/videos?v=2&max-results=5&'
46 url += urlencode({'q': query})
47 xml = XmlFeed(url)
48 entry = xml.elements('/feed/entry[%d]' % num)
49 if not entry:
50 return None
51
52 v = entry[0]
53 return {
54 'title': v.text('title'),
55 'duration': v.int('media:group/yt:duration/@seconds'),
56 'uploaded': v.text('media:group/yt:uploaded'),
57 'id': v.text('media:group/yt:videoid'),
58 'rating': v.decimal('gd:rating/@average'),
59 'rate_count': v.int('gd:rating/@numRaters'),
60 'favorite_count': v.int('yt:statistics/@favoriteCount'),
61 'view_count': v.int('yt:statistics/@viewCount'),
62 'liked': v.int('yt:rating/@numLikes'),
63 'disliked': v.int('yt:rating/@numDislikes')
64 }