summaryrefslogtreecommitdiff
path: root/cli/app/utils
diff options
context:
space:
mode:
Diffstat (limited to 'cli/app/utils')
-rw-r--r--cli/app/utils/cortex_utils.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/cli/app/utils/cortex_utils.py b/cli/app/utils/cortex_utils.py
new file mode 100644
index 0000000..328b93a
--- /dev/null
+++ b/cli/app/utils/cortex_utils.py
@@ -0,0 +1,64 @@
+import os
+from os.path import join
+import requests
+import urllib3
+urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
+
+from app.settings import app_cfg
+
+def api_url(path):
+ return "https://lens.neural.garden/api/{}/".format(path)
+
+def fetch_cortex_folder(opt_folder_id):
+ rows = fetch_json(api_url('file'), folder_id=opt_folder_id)
+ fp_out_dir = join(app_cfg.DIR_INPUTS, "cortex", str(opt_folder_id))
+ os.makedirs(fp_out_dir, exist_ok=True)
+ for row in rows:
+ if row['generated'] == 0 and row['processed'] != 1:
+ fn, ext = os.path.splitext(row['name'])
+ fp_out_image = join(fp_out_dir, "{}{}".format(row['id'], ext))
+ if not os.path.exists(fp_out_image):
+ fetch_file(row['url'], fp_out_image)
+
+def fetch_json(url, **kwargs):
+ resp = requests.get(url, params=kwargs, verify=False, timeout=10)
+ return None if resp.status_code != 200 else resp.json()
+
+def fetch_file(url, fn, **kwargs):
+ print("Fetch {} => {}".format(url, fn))
+ try:
+ resp = requests.get(url, params=kwargs, verify=False, timeout=10)
+ if resp.status_code != 200:
+ return None
+ except:
+ return None
+ size = 0
+ with open(fn, 'wb') as f:
+ for chunk in resp.iter_content(chunk_size=1024):
+ if chunk:
+ size += len(chunk)
+ f.write(chunk)
+ return size
+
+def upload_fp_to_cortex(opt_folder_id, fp):
+ files = {
+ 'file': fp
+ }
+ data = {
+ 'folder_id': opt_folder_id,
+ 'generated': 'true',
+ 'module': 'biggan',
+ 'activity': 'invert',
+ 'datatype': 'image',
+ }
+ url = os.path.join(api_url('folder'), opt_folder_id, 'upload/')
+ print(url)
+ r = requests.post(url, files=files, data=data)
+ print(r.json())
+
+def upload_bytes_to_cortex(opt_folder_id, fn, fp, mimetype):
+ upload_fp_to_cortex(opt_folder_id, (fn, fp.getvalue(), mimetype,))
+
+def upload_file_to_cortex(opt_folder_id, fn):
+ with open(fn, 'rb') as fp:
+ upload_fp_to_cortex(opt_folder_id, fp)