From f02bcae3881178ff9e16afa57f0f5b82cf2b21f4 Mon Sep 17 00:00:00 2001 From: Paul Walko Date: Fri, 17 Feb 2017 19:43:28 +0000 Subject: [PATCH 01/11] Use 'postalcode' parameter when a zipcode is used to get more accurate results. Added previously removed test. --- modules/test/test_weather.py | 1 + modules/weather.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/modules/test/test_weather.py b/modules/test/test_weather.py index f49b3ab..79dd1e9 100644 --- a/modules/test/test_weather.py +++ b/modules/test/test_weather.py @@ -28,6 +28,7 @@ class TestWeather(unittest.TestCase): ('27959', check_places("Dare County", "North Carolina")), ('48067', check_places("Royal Oak", "Michigan")), ('23606', check_places("Newport News", "Virginia")), + ('23113', check_places("Midlothian", "Virginia")), ('27517', check_places("Chapel Hill", "North Carolina")), ('15213', check_places("Allegheny County", "Pennsylvania")), ('90210', check_places("Los Angeles County", "California")), diff --git a/modules/weather.py b/modules/weather.py index dfdbcfe..89580fd 100644 --- a/modules/weather.py +++ b/modules/weather.py @@ -17,8 +17,14 @@ r_from = re.compile(r'(?i)([+-]\d+):00 from') def location(q): - uri = 'https://nominatim.openstreetmap.org/search/?q={query}&format=json'.\ - format(query=web.quote(q)) + uri = 'https://nominatim.openstreetmap.org/search?%s={query}&format=json' + if q.isdigit(): + uri = uri % 'postalcode' + else: + uri = uri % 'q' + uri = uri . format(query = web.quote(q)) +# uri = 'https://nominatim.openstreetmap.org/search/?q={query}&format=json'.\ +# format(query=web.quote(q)) results = web.get(uri) data = json.loads(results) From 149f02af90143374ed6151ec3bfedbb9096ef73d Mon Sep 17 00:00:00 2001 From: Paul Walko Date: Fri, 17 Feb 2017 19:52:26 +0000 Subject: [PATCH 02/11] Change etymology/definition syntax for parsing json --- modules/wiktionary.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/modules/wiktionary.py b/modules/wiktionary.py index 8770d1d..f85a109 100644 --- a/modules/wiktionary.py +++ b/modules/wiktionary.py @@ -48,29 +48,31 @@ def wiktionary(word): etymology = None definitions = {} for line in result.splitlines(): - if line == '===Etymology===': + if 'Etymology' in line: mode = 'etymology' - elif 'Noun' in line: + elif '==Noun==' in line: mode = 'noun' - elif 'Verb' in line: + elif '==Verb==' in line: mode = 'verb' - elif 'Adjective' in line: + elif '==Adjective==' in line: mode = 'adjective' - elif 'Adverb' in line: + elif '==Adverb==' in line: mode = 'adverb' - elif 'Interjection' in line: + elif '==Interjection==' in line: mode = 'interjection' elif 'Particle' in line: mode = 'particle' - elif 'Preposition' in line: + elif '==Preposition==' in line: mode = 'preposition' - elif len(line) == 0: - mode = None +# elif len(line) == 0: +# mode = None elif mode == 'etymology': etymology = text(line) + mode = None elif mode is not None and '#' in line: definitions.setdefault(mode, []).append(text(line)) + mode = None if '====Synonyms====' in line: break From 1249804f7c89a232b6f6d65b3032b0c2d85973c9 Mon Sep 17 00:00:00 2001 From: Paul Walko Date: Fri, 17 Feb 2017 19:52:47 +0000 Subject: [PATCH 03/11] Change etymology/definition syntax for parsing json --- modules/wiktionary.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/wiktionary.py b/modules/wiktionary.py index f85a109..8b9d82c 100644 --- a/modules/wiktionary.py +++ b/modules/wiktionary.py @@ -64,8 +64,6 @@ def wiktionary(word): mode = 'particle' elif '==Preposition==' in line: mode = 'preposition' -# elif len(line) == 0: -# mode = None elif mode == 'etymology': etymology = text(line) From a10f513dbddb0d21d49cf8f16e25386f33294be3 Mon Sep 17 00:00:00 2001 From: Paul Walko Date: Tue, 21 Feb 2017 19:45:50 +0000 Subject: [PATCH 04/11] Add " to unescape function in wiki.py --- wiki.py | 1 + 1 file changed, 1 insertion(+) diff --git a/wiki.py b/wiki.py index 25aa12d..58dc9b4 100644 --- a/wiki.py +++ b/wiki.py @@ -32,6 +32,7 @@ class Wiki(object): s = s.replace('<', '<') s = s.replace('&', '&') s = s.replace(' ', ' ') + s = s.replace('"', '"') return s @staticmethod From 7c67f090749d6472091123b5791169184f79eecc Mon Sep 17 00:00:00 2001 From: paul Date: Sun, 28 May 2017 00:42:44 -0400 Subject: [PATCH 05/11] scrape calc results from google since ddg is broken --- modules/calc.py | 58 ++++++++++++++++++++++++++++----------- modules/test/test_calc.py | 2 +- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/modules/calc.py b/modules/calc.py index 8f2f409..44e9ad5 100644 --- a/modules/calc.py +++ b/modules/calc.py @@ -10,6 +10,7 @@ http://inamidst.com/phenny/ import re import web +from modules.search import generic_google subs = [ ('£', 'GBP '), @@ -20,30 +21,55 @@ subs = [ (r'\/', '/'), ] +r_google_calc = re.compile(r'calculator-40.gif.*? = (.*?)<') +r_google_calc_exp = re.compile(r'calculator-40.gif.*? = (.*?)(.*?)<') def c(phenny, input): - """DuckDuckGo calculator.""" + """Google calculator.""" if not input.group(2): return phenny.reply("Nothing to calculate.") q = input.group(2) - - try: - r = web.get( - 'https://api.duckduckgo.com/?q={}&format=json&no_html=1' - '&t=mutantmonkey/phenny'.format(web.quote(q))) - except web.HTTPError: - raise GrumbleError("Couldn't parse the result from DuckDuckGo.") - - data = web.json(r) - if data['AnswerType'] == 'calc': - answer = data['Answer'].split('=')[-1].strip() + bytes = generic_google(q) + m = r_google_calc_exp.search(bytes) + if not m: + m = r_google_calc.search(bytes) + + if not m: + num = None + elif m.lastindex == 1: + num = web.decode(m.group(1)) else: - answer = None + num = "^".join((web.decode(m.group(1)), web.decode(m.group(2)))) - if answer: - phenny.say(answer) + if num: + phenny.say(num) else: - phenny.reply('Sorry, no result.') + phenny.reply("Sorry, no result.") + + +# def c(phenny, input): +# """DuckDuckGo calculator.""" +# if not input.group(2): +# return phenny.reply("Nothing to calculate.") +# q = input.group(2) +# +# try: +# r = web.get( +# 'https://api.duckduckgo.com/?q={}&format=json&no_html=1' +# '&t=mutantmonkey/phenny'.format(web.quote(q))) +# except web.HTTPError: +# raise GrumbleError("Couldn't parse the result from DuckDuckGo.") +# +# data = web.json(r) +# if data['AnswerType'] == 'calc': +# answer = data['Answer'].split('=')[-1].strip() +# else: +# answer = None +# +# if answer: +# phenny.say(answer) +# else: +# phenny.reply('Sorry, no result.') c.commands = ['c'] c.example = '.c 5 + 3' diff --git a/modules/test/test_calc.py b/modules/test/test_calc.py index 599cc2b..15d21b9 100644 --- a/modules/test/test_calc.py +++ b/modules/test/test_calc.py @@ -29,7 +29,7 @@ class TestCalc(unittest.TestCase): input = Mock(group=lambda x: '2^64') c(self.phenny, input) - self.phenny.say.assert_called_once_with('1.84467440737096 * 10^19') + self.phenny.say.assert_called_once_with('1.84467441 × 10^19') def test_c_none(self): input = Mock(group=lambda x: 'aif') From a000881c2bcdead7989a645f8e8f9d341308fda3 Mon Sep 17 00:00:00 2001 From: paul Date: Sun, 28 May 2017 00:43:30 -0400 Subject: [PATCH 06/11] mylife html changed again --- modules/mylife.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/mylife.py b/modules/mylife.py index e8d359e..3a0ec21 100644 --- a/modules/mylife.py +++ b/modules/mylife.py @@ -18,7 +18,8 @@ def fml(phenny, input): raise GrumbleError("I tried to use .fml, but it was broken. FML") doc = lxml.html.fromstring(req) - quote = doc.find_class('block')[1][0].text_content() + quote = doc.find_class('block')[0].text_content() + quote = quote.strip() phenny.say(quote) fml.commands = ['fml'] From 55839970f22cc47d3c795bb5fdd981076c58f81c Mon Sep 17 00:00:00 2001 From: paul Date: Sun, 28 May 2017 00:44:23 -0400 Subject: [PATCH 07/11] 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 From 8598fdcf5ecf1bad7a67ea9f3fed574b265fc12b Mon Sep 17 00:00:00 2001 From: Paul Walko Date: Sun, 28 May 2017 00:49:11 -0400 Subject: [PATCH 08/11] forgot to change ddg url back --- modules/search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/search.py b/modules/search.py index 4b97b61..c424d24 100644 --- a/modules/search.py +++ b/modules/search.py @@ -129,7 +129,7 @@ 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&ia=calculator' % query + uri = 'https://duckduckgo.com/html/?q=%s&kl=uk-en' % query bytes = web.get(uri) m = r_duck.search(bytes) if m: From f444326cb04a5f2c2fa5a9078d6d3ebec5b20125 Mon Sep 17 00:00:00 2001 From: Paul Walko Date: Mon, 29 May 2017 17:27:20 -0400 Subject: [PATCH 09/11] remove old code --- modules/calc.py | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/modules/calc.py b/modules/calc.py index 44e9ad5..baa4be0 100644 --- a/modules/calc.py +++ b/modules/calc.py @@ -33,7 +33,7 @@ def c(phenny, input): m = r_google_calc_exp.search(bytes) if not m: m = r_google_calc.search(bytes) - + if not m: num = None elif m.lastindex == 1: @@ -42,34 +42,10 @@ def c(phenny, input): num = "^".join((web.decode(m.group(1)), web.decode(m.group(2)))) if num: + num = num.replace('×', '*') phenny.say(num) else: phenny.reply("Sorry, no result.") - - -# def c(phenny, input): -# """DuckDuckGo calculator.""" -# if not input.group(2): -# return phenny.reply("Nothing to calculate.") -# q = input.group(2) -# -# try: -# r = web.get( -# 'https://api.duckduckgo.com/?q={}&format=json&no_html=1' -# '&t=mutantmonkey/phenny'.format(web.quote(q))) -# except web.HTTPError: -# raise GrumbleError("Couldn't parse the result from DuckDuckGo.") -# -# data = web.json(r) -# if data['AnswerType'] == 'calc': -# answer = data['Answer'].split('=')[-1].strip() -# else: -# answer = None -# -# if answer: -# phenny.say(answer) -# else: -# phenny.reply('Sorry, no result.') c.commands = ['c'] c.example = '.c 5 + 3' From d8eefde17e96c02b6449e455faf64b3831b0dbc1 Mon Sep 17 00:00:00 2001 From: Paul Walko Date: Mon, 29 May 2017 17:27:58 -0400 Subject: [PATCH 10/11] remove unicode --- modules/test/test_calc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/test/test_calc.py b/modules/test/test_calc.py index 15d21b9..13e4839 100644 --- a/modules/test/test_calc.py +++ b/modules/test/test_calc.py @@ -29,7 +29,7 @@ class TestCalc(unittest.TestCase): input = Mock(group=lambda x: '2^64') c(self.phenny, input) - self.phenny.say.assert_called_once_with('1.84467441 × 10^19') + self.phenny.say.assert_called_once_with('1.84467441 * 10^19') def test_c_none(self): input = Mock(group=lambda x: 'aif') From 9e8899bce100917bd5805968cc43a9cc418f391e Mon Sep 17 00:00:00 2001 From: Paul Walko Date: Mon, 29 May 2017 17:28:14 -0400 Subject: [PATCH 11/11] remove old code & use format --- modules/weather.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/modules/weather.py b/modules/weather.py index 89580fd..194ddc4 100644 --- a/modules/weather.py +++ b/modules/weather.py @@ -17,14 +17,11 @@ r_from = re.compile(r'(?i)([+-]\d+):00 from') def location(q): - uri = 'https://nominatim.openstreetmap.org/search?%s={query}&format=json' + uri = 'https://nominatim.openstreetmap.org/search?{type}={query}&format=json' if q.isdigit(): - uri = uri % 'postalcode' + uri = uri . format(type = 'postalcode', query = web.quote(q)) else: - uri = uri % 'q' - uri = uri . format(query = web.quote(q)) -# uri = 'https://nominatim.openstreetmap.org/search/?q={query}&format=json'.\ -# format(query=web.quote(q)) + uri = uri . format(type = 'q', query = web.quote(q)) results = web.get(uri) data = json.loads(results)