diff options
Diffstat (limited to 'cli')
| -rw-r--r-- | cli/app/settings/app_cfg.py | 35 | ||||
| -rw-r--r-- | cli/app/settings/types.py | 15 | ||||
| -rw-r--r-- | cli/app/thesaurus/api.py | 33 | ||||
| -rw-r--r-- | cli/app/utils/util.py | 4 | ||||
| -rw-r--r-- | cli/commands/api/category.py | 2 | ||||
| -rw-r--r-- | cli/commands/api/search.py | 2 | ||||
| -rw-r--r-- | cli/commands/bridge/words.py | 30 |
7 files changed, 106 insertions, 15 deletions
diff --git a/cli/app/settings/app_cfg.py b/cli/app/settings/app_cfg.py index 952e76b..09f557b 100644 --- a/cli/app/settings/app_cfg.py +++ b/cli/app/settings/app_cfg.py @@ -1,10 +1,41 @@ import os +import logging +from pathlib import Path + +from app.settings import types + +# ----------------------------------------------------------------------------- +# Logging options exposed for custom click Params +# ----------------------------------------------------------------------------- CLICK_GROUPS = { 'api': 'commands/api', } -DATA_STORE = 'data_store' +# ----------------------------------------------------------------------------- +# Paths +# ----------------------------------------------------------------------------- + +DIR_SELF = os.path.dirname(os.path.realpath(__file__)) +DIR_ROOT = Path(DIR_SELF).parent.parent.parent + +DATA_STORE = os.path.join(DIR_ROOT, 'data_store') SEARCH_PATH = os.path.join(DATA_STORE, "search") -CATEGORIES_PATH = os.path.join(DATA_STORE, "categories") +CATEGORY_PATH = os.path.join(DATA_STORE, "categories") + +# ----------------------------------------------------------------------------- +# Logging options exposed for custom click Params +# ----------------------------------------------------------------------------- + +LOGGER_NAME = 'CLI' +LOG = logging.getLogger(LOGGER_NAME) +LOGLEVELS = { + types.LogLevel.DEBUG: logging.DEBUG, + types.LogLevel.INFO: logging.INFO, + types.LogLevel.WARN: logging.WARN, + types.LogLevel.ERROR: logging.ERROR, + types.LogLevel.CRITICAL: logging.CRITICAL +} +LOGLEVEL_OPT_DEFAULT = types.LogLevel.DEBUG.name +LOGFILE_FORMAT = "%(log_color)s%(levelname)-8s%(reset)s %(cyan)s%(filename)s:%(lineno)s:%(bold_cyan)s%(funcName)s() %(reset)s%(message)s" diff --git a/cli/app/settings/types.py b/cli/app/settings/types.py new file mode 100644 index 0000000..7afdf7f --- /dev/null +++ b/cli/app/settings/types.py @@ -0,0 +1,15 @@ +from enum import Enum + +def find_type(name, enum_type): + for enum_opt in enum_type: + if name == enum_opt.name.lower(): + return enum_opt + return None + +# --------------------------------------------------------------------- +# Logger, monitoring +# -------------------------------------------------------------------- + +class LogLevel(Enum): + """Loger vebosity""" + DEBUG, INFO, WARN, ERROR, CRITICAL = range(5) diff --git a/cli/app/thesaurus/api.py b/cli/app/thesaurus/api.py index ad0dd92..98e0210 100644 --- a/cli/app/thesaurus/api.py +++ b/cli/app/thesaurus/api.py @@ -1,5 +1,6 @@ import os import requests +from hashlib import sha256 from app.utils.util import * from app.settings import app_cfg @@ -40,9 +41,23 @@ class ThesaurusAPI: if resp.status_code != 200: return [] data = resp.text - data = data.split('<div id="resultsTimelineData">') - data = data[0].split('</div>') - return json.loads(data) + data = data.split('<div id="resultsTimelineData">')[1].split('</div>')[0] + # print(data) + rows = json.loads(data) + cats = [] + for row in rows: + cat, years = row['popup'].split(']: ') + cat = cat.split('[')[1] + cats.append({ + 'catid': row['catid'], + 'catnum': row['catnum'], + 'category': cat, + 'years': years, + }) + return { + 'word': word, + 'categories': cats, + } def category(self, id): query = { @@ -52,20 +67,20 @@ class ThesaurusAPI: if resp.status_code != 200: return "" raw = resp.text - classification = raw.split("<span style='font-size: 0.6em'>")[1].split('</span>')[0] - category = raw.split("<br />")[1].split('</h2>')[0] - raw_words = raw.split('<b>')[1:] + catnum = raw.split("<span style='font-size: 0.6em'>")[1].split('</span>')[0] + category = raw.split("<br />")[1].split('</h2>')[0].replace("<span style='font-size: 0.6em'>", "").replace("</span>", "") + raw_words = raw.split('"><b>')[1:] words = [] for word in raw_words: word, rest = word.split('</b>') - years = word.split(' <span')[0].trim() + years = word.split(' <span')[0].strip() words.append({ 'word': word, 'years': years, }) return { - 'id': id, + 'catid': id, + 'catnum': catnum, 'category': category, - 'classification': classification, 'words': words, }
\ No newline at end of file diff --git a/cli/app/utils/util.py b/cli/app/utils/util.py index 5f72088..08992c7 100644 --- a/cli/app/utils/util.py +++ b/cli/app/utils/util.py @@ -1,9 +1,9 @@ import simplejson as json -from hashlib import sha256 +import hashlib def sha256(s): sha256 = hashlib.sha256() - sha256.update(s) + sha256.update(str.encode(s)) return sha256.hexdigest() def read_json(fn): diff --git a/cli/commands/api/category.py b/cli/commands/api/category.py index 5b688f0..b5d1b84 100644 --- a/cli/commands/api/category.py +++ b/cli/commands/api/category.py @@ -8,7 +8,7 @@ import simplejson as json from app.thesaurus.api import Thesaurus @click.command() -@click.option('-c', '--id', 'opt_cat_id', required=True, +@click.option('-i', '--id', 'opt_cat_id', required=True, help='Category ID') @click.pass_context def cli(ctx, opt_cat_id): diff --git a/cli/commands/api/search.py b/cli/commands/api/search.py index 3c0dc0e..cdff053 100644 --- a/cli/commands/api/search.py +++ b/cli/commands/api/search.py @@ -8,7 +8,7 @@ import simplejson as json from app.thesaurus.api import Thesaurus @click.command() -@click.option('-w', '--word', 'opt_word', required=True, +@click.option('-i', '--word', 'opt_word', required=True, help='Word to search') @click.pass_context def cli(ctx, opt_word): diff --git a/cli/commands/bridge/words.py b/cli/commands/bridge/words.py new file mode 100644 index 0000000..277b069 --- /dev/null +++ b/cli/commands/bridge/words.py @@ -0,0 +1,30 @@ +""" +Find connections between two words +""" + +import click +import simplejson as json + +from app.thesaurus.api import Thesaurus + +@click.command() +@click.option('-a', '--a', 'opt_word_a', required=True, + help='Starting word') +@click.option('-b', '--b', 'opt_word_b', required=True, + help='Ending word') +@click.pass_context +def cli(ctx, opt_word_a, opt_word_b): + """Find connections between two words + """ + thesaurus = Thesaurus() + print(f"Starting word: {opt_word_a}") + print(f"Ending word: {opt_word_b}") + results_a = thesaurus.search(opt_word_a) + results_b = thesaurus.search(opt_word_b) + # use sets + # make set of results_a + # find overlap with results_b + # if there's no match... + # search for first word in results_a + # loop over results... + # print(json.dumps(results, indent=2)) |
