Try to remove accents if cheese.com query fails

master
Nikola Forró 6 years ago
parent 2081b334a8
commit 2f001cc9c1

@ -1,4 +1,5 @@
import datetime import datetime
import unicodedata
import dateutil.parser import dateutil.parser
import requests import requests
@ -171,9 +172,15 @@ class Commands(object):
return clip return clip
def query_cheese_com(self, query): 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() cc = CheeseCom()
try:
try: try:
result = cc.query(query) result = cc.query(query)
except CheeseComError:
result = cc.query(remove_accents(query))
except CheeseComError: except CheeseComError:
raise CommandError('couldn\'t find exact match on cheese.com') raise CommandError('couldn\'t find exact match on cheese.com')
else: else:

@ -19,6 +19,8 @@ class CheeseCom(object):
d = pq(r.content) d = pq(r.content)
if not r.history: if not r.history:
url = d('div[class~="cheese-item"]').eq(0).find('a').eq(0).attr('href') 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) r = requests.get(BASE_URL + url)
d = pq(r.content) d = pq(r.content)
summary = [] summary = []

Loading…
Cancel
Save