From 2f001cc9c1febfde5c8f3b05ed3e3f0240820280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Wed, 17 Apr 2019 11:06:07 +0200 Subject: [PATCH] Try to remove accents if cheese.com query fails --- commands.py | 9 ++++++++- services/cheesecom.py | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/commands.py b/commands.py index f70ad71..90d2ac6 100644 --- a/commands.py +++ b/commands.py @@ -1,4 +1,5 @@ import datetime +import unicodedata import dateutil.parser import requests @@ -171,9 +172,15 @@ class Commands(object): return clip def query_cheese_com(self, query): + def remove_accents(s): + s = unicodedata.normalize('NFKD', s) + return ''.join([c for c in s if not unicodedata.combining(c)]) cc = CheeseCom() try: - result = cc.query(query) + try: + result = cc.query(query) + except CheeseComError: + result = cc.query(remove_accents(query)) except CheeseComError: raise CommandError('couldn\'t find exact match on cheese.com') else: diff --git a/services/cheesecom.py b/services/cheesecom.py index 12354a3..b8c2d4b 100644 --- a/services/cheesecom.py +++ b/services/cheesecom.py @@ -19,6 +19,8 @@ class CheeseCom(object): d = pq(r.content) if not r.history: url = d('div[class~="cheese-item"]').eq(0).find('a').eq(0).attr('href') + if not url: + raise CheeseComError('Not found') r = requests.get(BASE_URL + url) d = pq(r.content) summary = []