Merge pull request #71 from vtluug/master

Fix .c, .fml
master
mutantmonkey 2017-06-03 02:05:59 +00:00 committed by GitHub
commit 74a70e1c5c
8 changed files with 45 additions and 34 deletions

View File

@ -10,6 +10,7 @@ http://inamidst.com/phenny/
import re import re
import web import web
from modules.search import generic_google
subs = [ subs = [
('£', 'GBP '), ('£', 'GBP '),
@ -20,30 +21,31 @@ subs = [
(r'\/', '/'), (r'\/', '/'),
] ]
r_google_calc = re.compile(r'calculator-40.gif.*? = (.*?)<')
r_google_calc_exp = re.compile(r'calculator-40.gif.*? = (.*?)<sup>(.*?)<')
def c(phenny, input): def c(phenny, input):
"""DuckDuckGo calculator.""" """Google calculator."""
if not input.group(2): if not input.group(2):
return phenny.reply("Nothing to calculate.") return phenny.reply("Nothing to calculate.")
q = input.group(2) 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: if not m:
r = web.get( num = None
'https://api.duckduckgo.com/?q={}&format=json&no_html=1' elif m.lastindex == 1:
'&t=mutantmonkey/phenny'.format(web.quote(q))) num = web.decode(m.group(1))
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: else:
answer = None num = "^".join((web.decode(m.group(1)), web.decode(m.group(2))))
if answer: if num:
phenny.say(answer) num = num.replace('×', '*')
phenny.say(num)
else: else:
phenny.reply('Sorry, no result.') phenny.reply("Sorry, no result.")
c.commands = ['c'] c.commands = ['c']
c.example = '.c 5 + 3' c.example = '.c 5 + 3'

View File

@ -18,7 +18,8 @@ def fml(phenny, input):
raise GrumbleError("I tried to use .fml, but it was broken. FML") raise GrumbleError("I tried to use .fml, but it was broken. FML")
doc = lxml.html.fromstring(req) 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) phenny.say(quote)
fml.commands = ['fml'] fml.commands = ['fml']

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 (.*?) ')
@ -130,8 +133,8 @@ def duck_search(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

View File

@ -29,7 +29,7 @@ class TestCalc(unittest.TestCase):
input = Mock(group=lambda x: '2^64') input = Mock(group=lambda x: '2^64')
c(self.phenny, input) 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): def test_c_none(self):
input = Mock(group=lambda x: 'aif') input = Mock(group=lambda x: 'aif')

View File

@ -28,6 +28,7 @@ class TestWeather(unittest.TestCase):
('27959', check_places("Dare County", "North Carolina")), ('27959', check_places("Dare County", "North Carolina")),
('48067', check_places("Royal Oak", "Michigan")), ('48067', check_places("Royal Oak", "Michigan")),
('23606', check_places("Newport News", "Virginia")), ('23606', check_places("Newport News", "Virginia")),
('23113', check_places("Midlothian", "Virginia")),
('27517', check_places("Chapel Hill", "North Carolina")), ('27517', check_places("Chapel Hill", "North Carolina")),
('15213', check_places("Allegheny County", "Pennsylvania")), ('15213', check_places("Allegheny County", "Pennsylvania")),
('90210', check_places("Los Angeles County", "California")), ('90210', check_places("Los Angeles County", "California")),

View File

@ -17,8 +17,11 @@ r_from = re.compile(r'(?i)([+-]\d+):00 from')
def location(q): def location(q):
uri = 'https://nominatim.openstreetmap.org/search/?q={query}&format=json'.\ uri = 'https://nominatim.openstreetmap.org/search?{type}={query}&format=json'
format(query=web.quote(q)) 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) results = web.get(uri)
data = json.loads(results) data = json.loads(results)

View File

@ -48,29 +48,29 @@ def wiktionary(word):
etymology = None etymology = None
definitions = {} definitions = {}
for line in result.splitlines(): for line in result.splitlines():
if line == '===Etymology===': if 'Etymology' in line:
mode = 'etymology' mode = 'etymology'
elif 'Noun' in line: elif '==Noun==' in line:
mode = 'noun' mode = 'noun'
elif 'Verb' in line: elif '==Verb==' in line:
mode = 'verb' mode = 'verb'
elif 'Adjective' in line: elif '==Adjective==' in line:
mode = 'adjective' mode = 'adjective'
elif 'Adverb' in line: elif '==Adverb==' in line:
mode = 'adverb' mode = 'adverb'
elif 'Interjection' in line: elif '==Interjection==' in line:
mode = 'interjection' mode = 'interjection'
elif 'Particle' in line: elif 'Particle' in line:
mode = 'particle' mode = 'particle'
elif 'Preposition' in line: elif '==Preposition==' in line:
mode = 'preposition' mode = 'preposition'
elif len(line) == 0:
mode = None
elif mode == 'etymology': elif mode == 'etymology':
etymology = text(line) etymology = text(line)
mode = None
elif mode is not None and '#' in line: elif mode is not None and '#' in line:
definitions.setdefault(mode, []).append(text(line)) definitions.setdefault(mode, []).append(text(line))
mode = None
if '====Synonyms====' in line: if '====Synonyms====' in line:
break break

View File

@ -32,6 +32,7 @@ class Wiki(object):
s = s.replace('&lt;', '<') s = s.replace('&lt;', '<')
s = s.replace('&amp;', '&') s = s.replace('&amp;', '&')
s = s.replace('&#160;', ' ') s = s.replace('&#160;', ' ')
s = s.replace('&quot;', '"')
return s return s
@staticmethod @staticmethod