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 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.*? = (.*?)<sup>(.*?)<')
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'

View File

@ -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']

View File

@ -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

View File

@ -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')

View File

@ -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")),

View File

@ -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)

View File

@ -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

View File

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