diff --git a/modules/calc.py b/modules/calc.py index 8f2f409..baa4be0 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,31 @@ 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) + bytes = generic_google(q) + m = r_google_calc_exp.search(bytes) + if not m: + m = r_google_calc.search(bytes) - 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() + 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: + num = num.replace('×', '*') + phenny.say(num) else: - phenny.reply('Sorry, no result.') + phenny.reply("Sorry, no result.") c.commands = ['c'] c.example = '.c 5 + 3' 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'] diff --git a/modules/search.py b/modules/search.py index 18d13af..c424d24 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 (.*?) ') @@ -130,8 +133,8 @@ def duck_search(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 diff --git a/modules/test/test_calc.py b/modules/test/test_calc.py index 599cc2b..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.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') 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..194ddc4 100644 --- a/modules/weather.py +++ b/modules/weather.py @@ -17,8 +17,11 @@ 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?{type}={query}&format=json' + if q.isdigit(): + uri = uri . format(type = 'postalcode', query = web.quote(q)) + else: + uri = uri . format(type = 'q', query = web.quote(q)) results = web.get(uri) data = json.loads(results) diff --git a/modules/wiktionary.py b/modules/wiktionary.py index 8770d1d..8b9d82c 100644 --- a/modules/wiktionary.py +++ b/modules/wiktionary.py @@ -48,29 +48,29 @@ 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 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 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