summaryrefslogtreecommitdiff
path: root/cli/app
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2020-04-01 01:05:53 +0200
committerJules Laplace <julescarbon@gmail.com>2020-04-01 01:05:53 +0200
commitbb5f8a9fe7f3968588b45eeaa0c993ef314ca904 (patch)
tree2535fae43c4937ae45357a4e8108ad355220e5c4 /cli/app
parent2b407d1f4a608d0ac23592ff16def77797e4fa41 (diff)
begin bridge
Diffstat (limited to 'cli/app')
-rw-r--r--cli/app/settings/app_cfg.py35
-rw-r--r--cli/app/settings/types.py15
-rw-r--r--cli/app/thesaurus/api.py33
-rw-r--r--cli/app/utils/util.py4
4 files changed, 74 insertions, 13 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):