added pygooglesearch.py and commit modify
[archive.git] / Apkawa / pygooglesearch.py
blob1b8267ccd4408a2f2d14ccb15ab1aa0b48264627
1 # -*- coding: utf-8 -*-
2 import urllib
3 from json import loads
5 class GoogleSearchApi:
6 def __init__(self):
7 self.api_url = 'http://ajax.googleapis.com/ajax/services/search/%(type)s?%(query)s'
8 self.query = {'v':1.0, 'rsz':'small','hl':'en'}
9 pass
10 def __get_result(self, type):
11 search_url = self.api_url%{'type':type,'query': urllib.urlencode( self.query) }
12 resp = urllib.urlopen( search_url ).read()
13 self.__parse( resp)
15 if self.status == 200:
16 return True
17 else:
18 return False
19 def __parse(self, resp):
20 response = loads( resp)
21 self.status = response['responseStatus']
22 self.cursor = response['responseData']['cursor']
23 self.results = response['responseData']['results']
24 def __check_args(self, args, const):
25 c_a_key = const.keys()
26 result_args = {}
28 for a in args.keys():
29 if a in c_a_key:
30 val = const[a]['val']
31 if val:
32 if args[a] in val.keys():
33 result_args.update( {a, args[a]} )
36 def search(self, word, lang='en'):
37 self.query['q'] = urllib.quote(word)
38 self.query['hl'] = lang
39 return True
40 def web(self, help=True, **args):
41 const_arguments = {
42 'cx': {
43 'desc':'This optional argument supplies the unique id for the Custom Search Engine that should be used for this request (e.g., cx=000455696194071821846:reviews).'
44 'val': None,}
45 'cref': {
46 'desc':'This optional argument supplies the url of a linked Custom Search Engine specification that should be used to satisfy this request (e.g., cref=http%3A%2F%2Fwww.google.com%2Fcse%2Fsamples%2Fvegetarian.xml).',
47 'val':None}
48 'safe':{
49 'desc':'This optional argument supplies the search safety level which may be one of:',
50 'val':{'active':'enables the highest level of safe search filtering',
51 'moderate':'enables moderate safe search filtering (default)',
52 'off':'disables safe search filtering'},}
53 'lr':{
54 'desc':'This optional argument allows the caller to restrict the search to documents written in a particular language (e.g., lr=lang_ja). This list contains the permissible set of values.',
55 'val':None}
58 self.check_args( args, const_arguments )
61 self.query.update( {'lr':'lang_'+lr, 'safe': safe })
62 return self.__get_result('web')
64 def image(self, help=False,**args )# imgz = None, imgc=None, imgtype=None, as_filetype=None, as_sitesearch=None):
65 arguments = {
66 'imgz':{
67 'desc':'This optional argument tells the image search system to restrict the search to images of the specified size, where size can be one of',
68 'val':{
69 'icon':'restrict to small images',
70 'small|medium|large|xlarge':'restrict to medium images',
71 'xxlarge':'restrict to large images',
72 'huge':'restrict to extra large images'
74 'imgc':{},
75 'imgtype':{},
76 'as_filetype':{},
77 'as_sitesearch':{},
78 'lr':{}}
79 self.query.update( {'lr':'lang_'+lr, 'safe': safe })
80 return self.__get_result('image')
83 def google(word):
84 def web(results):
85 if results:
86 url = urllib.unquote(results[0]['url'])
87 title1 = results[0]['titleNoFormatting']
88 title2 = results[0]['title']
89 content = re.sub('(<b>)|(</b>)','',results[0]['content'])
90 text = '%s\n%s\n%s'%(title1,content,url)
91 text = re.sub('&#39;','\'',unescape(text))
92 #print text
94 extra = '''<a href="%s">%s</a>
95 <p>%s</p>
96 '''%(url,title2,content)
97 return True, text, extra
98 else: return True, 'Nyaaa... Ничего не нашла.', ''
99 def images(results):
100 if results:
101 imgurl = results[0]['unescapedUrl']
102 return imgurl
103 else: return None
104 #http://code.google.com/apis/ajaxsearch/documentation/reference.html#_intro_fonje
105 _type = 'images'
106 #word = urllib.quote(word.encode('utf-8'))
107 word = urllib.quote(word)
108 src = urllib.urlopen('http://ajax.googleapis.com/ajax/services/search/%s?v=1.0&q=%s'%(_type,word)).read()
109 convert = loads(src)
110 results = convert['responseData']['results']
111 return images(results)
112 #if type == 'web':
113 #return web(results)
114 #if type == 'images':
115 # return images(results)
117 if __name__ == '__main__':
118 search = GoogleSearchApi()
119 search.search('nya')
120 search.web()
121 print search.results