create generic google function to share with calculator

master
paul 2017-05-28 00:44:23 -04:00
parent a000881c2b
commit 55839970f2
1 changed files with 10 additions and 7 deletions

View File

@ -13,14 +13,17 @@ import web
r_google = re.compile(r'href="\/url\?q=(http.*?)&') r_google = re.compile(r'href="\/url\?q=(http.*?)&')
def google_search(query): def generic_google(query):
query = web.quote(query) query = web.quote(query)
uri = 'https://google.co.uk/search?q=%s' % query uri = 'https://google.co.uk/search?q=%s' % query
bytes = web.get(uri) return web.get(uri)
def google_search(query):
bytes = generic_google(query)
m = r_google.search(bytes) m = r_google.search(bytes)
if m: if m:
result = web.decode(m.group(1)) uri = web.decode(m.group(1))
return web.unquote(result) return web.unquote(uri)
r_google_count = re.compile(r'id="resultStats">About (.*?) ') r_google_count = re.compile(r'id="resultStats">About (.*?) ')
@ -126,12 +129,12 @@ r_duck = re.compile(r'nofollow" class="[^"]+" href=".+?(http.*?)">')
def duck_search(query): def duck_search(query):
query = query.replace('!', '') query = query.replace('!', '')
query = web.quote(query) query = web.quote(query)
uri = 'https://duckduckgo.com/html/?q=%s&kl=uk-en' % query uri = 'https://duckduckgo.com/html/?q=%s&kl=uk-en&ia=calculator' % query
bytes = web.get(uri) bytes = web.get(uri)
m = r_duck.search(bytes) m = r_duck.search(bytes)
if m: if m:
result = web.decode(m.group(1)) uri = web.decode(m.group(1))
return web.unquote(result) return web.unquote(uri)
def duck_api(query): def duck_api(query):
uri = 'https://api.duckduckgo.com/?q=%s&format=json&no_redirect=1' % query uri = 'https://api.duckduckgo.com/?q=%s&format=json&no_redirect=1' % query