code cleanup
This commit is contained in:
parent
1092b20bb5
commit
d3bbea2265
2 changed files with 20 additions and 12 deletions
30
__init__.py
30
__init__.py
|
@ -21,6 +21,13 @@ from urllib.parse import quote_plus
|
|||
class KagiSkill(FallbackSkill):
|
||||
|
||||
def initialize(self):
|
||||
# compile replacement patterns
|
||||
self.pattern_ptag = re.compile(r'<[^>]*$')
|
||||
self.pattern_refs = re.compile(r'<div[^>]*>.*$', re.DOTALL)
|
||||
self.pattern_sups = re.compile(r'<sup[^>]*>.*?</sup>', re.DOTALL)
|
||||
self.pattern_tags = re.compile(r'<.*?>')
|
||||
self.pattern_brak = re.compile(r'【\d+】')
|
||||
|
||||
self.register_fallback(self.handle_fallback_kagi, 80)
|
||||
|
||||
def handle_fallback_kagi(self, message):
|
||||
|
@ -72,7 +79,7 @@ class KagiSkill(FallbackSkill):
|
|||
if client:
|
||||
try:
|
||||
client.close()
|
||||
except:
|
||||
except: # noqa: E722
|
||||
pass
|
||||
|
||||
def get_using_api(self, message):
|
||||
|
@ -108,21 +115,22 @@ class KagiSkill(FallbackSkill):
|
|||
return False
|
||||
|
||||
def clean_session_string(self, raw_text):
|
||||
# sometimes it bails before finishing the json string
|
||||
# if that happens let's fix it and read what we managed to get
|
||||
jsonstr = raw_text if raw_text.endswith('"') else raw_text + '"'
|
||||
jsonstr = raw_text
|
||||
if not jsonstr.endswith('"'):
|
||||
# sometimes it bails before finishing the json string
|
||||
# if that happens let's fix it and read what we managed to get
|
||||
self.log.warn('got partial response from kagi, attempting to salvage')
|
||||
# clean up partial html if it got cut off mid-tag and add the closing quote
|
||||
jsonstr = re.sub(self.pattern_ptag, '', jsonstr) + '"'
|
||||
text = json.loads(jsonstr)
|
||||
pattern_refs = re.compile(r'<div[^>]*>.*$', re.DOTALL)
|
||||
pattern_sups = re.compile(r'<sup[^>]*>.*?</sup>', re.DOTALL)
|
||||
pattern_tags = re.compile(r'<.*?>')
|
||||
text = re.sub(pattern_refs, '', text)
|
||||
text = re.sub(pattern_sups, '', text)
|
||||
text = re.sub(pattern_tags, '', text)
|
||||
text = re.sub(self.pattern_refs, '', text)
|
||||
text = re.sub(self.pattern_sups, '', text)
|
||||
text = re.sub(self.pattern_tags, '', text)
|
||||
return html.unescape(text)
|
||||
|
||||
def clean_api_string(self, text):
|
||||
text = text.replace('*', '').replace('_', '')
|
||||
text = re.sub(r'【\d+】', '', text)
|
||||
text = re.sub(self.pattern_brak, '', text)
|
||||
return text
|
||||
|
||||
def get_api_response(self, json_data):
|
||||
|
|
2
setup.py
2
setup.py
|
@ -4,7 +4,7 @@ import os
|
|||
from os import walk, path
|
||||
|
||||
PYPI_NAME = "skill-ovos-fallback-kagi" # pip install PYPI_NAME
|
||||
VERSION = "0.1.9"
|
||||
VERSION = "0.1.10"
|
||||
URL = f"https://code.sitosis.com/rudism/{PYPI_NAME}"
|
||||
SKILL_CLAZZ = "KagiSkill" # needs to match __init__.py class name
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue