From 55839970f22cc47d3c795bb5fdd981076c58f81c Mon Sep 17 00:00:00 2001 From: paul Date: Sun, 28 May 2017 00:44:23 -0400 Subject: [PATCH] create generic google function to share with calculator --- modules/search.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/modules/search.py b/modules/search.py index 18d13af..4b97b61 100644 --- a/modules/search.py +++ b/modules/search.py @@ -13,14 +13,17 @@ import web r_google = re.compile(r'href="\/url\?q=(http.*?)&') -def google_search(query): +def generic_google(query): query = web.quote(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) if m: - result = web.decode(m.group(1)) - return web.unquote(result) + uri = web.decode(m.group(1)) + return web.unquote(uri) 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): query = query.replace('!', '') 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) m = r_duck.search(bytes) if m: - result = web.decode(m.group(1)) - return web.unquote(result) + uri = web.decode(m.group(1)) + return web.unquote(uri) def duck_api(query): uri = 'https://api.duckduckgo.com/?q=%s&format=json&no_redirect=1' % query