From c14810fe9f663d46b5477088d06047fea66d1524 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 2 Apr 2020 15:31:46 +0200 Subject: ability to skip words if you dont like the connection --- cli/app/settings/app_cfg.py | 1 + cli/app/thesaurus/api.py | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) (limited to 'cli/app') diff --git a/cli/app/settings/app_cfg.py b/cli/app/settings/app_cfg.py index 09f557b..ee1be51 100644 --- a/cli/app/settings/app_cfg.py +++ b/cli/app/settings/app_cfg.py @@ -10,6 +10,7 @@ from app.settings import types CLICK_GROUPS = { 'api': 'commands/api', + 'bridge': 'commands/bridge', } # ----------------------------------------------------------------------------- diff --git a/cli/app/thesaurus/api.py b/cli/app/thesaurus/api.py index 98e0210..467d5fb 100644 --- a/cli/app/thesaurus/api.py +++ b/cli/app/thesaurus/api.py @@ -1,4 +1,6 @@ import os +import re +import time import requests from hashlib import sha256 @@ -13,10 +15,18 @@ class Thesaurus: sha = sha256(word) hash_path = os.path.join(base_path, sha[0:2]) os.makedirs(hash_path, exist_ok=True) - path = os.path.join(hash_path, word + '.json') + clean_word = re.sub('[^0-9a-zA-Z]+', '*', word) + path = os.path.join(hash_path, clean_word + '.json') if os.path.exists(path): return read_json(path) - data = api_fn(word) + data = None + while data is None: + try: + data = api_fn(word) + except Exception as e: + print("Got HTTP error, sleeping for 5 seconds") + time.sleep(5) + pass write_json(path, data) return data @@ -34,6 +44,11 @@ class ThesaurusAPI: } def search(self, word): + word = word.split('<')[0] + word = word.split('/')[0] + word = word.replace('(', '').replace(')', '') + if len(word) < 1: + return { 'word': word, 'categories': [] } query = { 'qsearch': word, } @@ -42,8 +57,18 @@ class ThesaurusAPI: return [] data = resp.text data = data.split('
')[1].split('
')[0] - # print(data) - rows = json.loads(data) + data = data.replace('', '') + data = data.replace('', '') + try: + rows = json.loads(data) + except Exception as e: + print(f"Error loading JSON for {word}") + print(data) + # raise e + return { + 'word': word, + 'categories': [], + } cats = [] for row in rows: cat, years = row['popup'].split(']: ') @@ -72,8 +97,10 @@ class ThesaurusAPI: raw_words = raw.split('">')[1:] words = [] for word in raw_words: - word, rest = word.split('') - years = word.split(' ') + word = word_partz[0] + years = word_partz[1].split('