2019-01-04 13:27:24 -08:00
|
|
|
"""First API, local access only"""
|
2019-01-04 15:34:42 -08:00
|
|
|
import json
|
2019-01-06 12:44:00 -08:00
|
|
|
import re
|
2019-01-04 15:34:42 -08:00
|
|
|
|
2019-01-04 13:27:24 -08:00
|
|
|
import hug # type: ignore
|
|
|
|
import hug.types as T # type: ignore
|
|
|
|
|
2019-01-06 15:57:15 -08:00
|
|
|
from config import CAPTURE_PATH # TODO mm, maybe configure it during server setup?
|
|
|
|
from org_tools import append_org_entry
|
2019-01-04 13:27:24 -08:00
|
|
|
|
2019-01-04 15:34:42 -08:00
|
|
|
def empty(s) -> bool:
|
|
|
|
return s is None or len(s.strip()) == 0
|
2019-01-04 13:27:24 -08:00
|
|
|
|
2019-01-04 15:34:42 -08:00
|
|
|
def log(*things):
|
|
|
|
# TODO proper logging
|
|
|
|
print(*things)
|
|
|
|
|
|
|
|
# TODO allow to pass tags??
|
2019-01-04 13:27:24 -08:00
|
|
|
@hug.local()
|
|
|
|
@hug.post('/capture')
|
|
|
|
def capture(
|
|
|
|
url: T.text,
|
2019-01-04 15:57:34 -08:00
|
|
|
title: T.Nullable(T.text),
|
2019-01-04 13:27:24 -08:00
|
|
|
selection: T.Nullable(T.text),
|
|
|
|
comment: T.Nullable(T.text),
|
2019-01-06 12:44:00 -08:00
|
|
|
tag_str: T.Nullable(T.text),
|
2019-01-04 13:27:24 -08:00
|
|
|
):
|
2019-01-04 15:57:34 -08:00
|
|
|
log("capturing", url, title, selection, comment)
|
2019-01-04 15:34:42 -08:00
|
|
|
|
2019-01-04 15:57:34 -08:00
|
|
|
heading = url
|
2019-01-04 15:34:42 -08:00
|
|
|
parts = []
|
2019-01-06 12:44:00 -08:00
|
|
|
tags = []
|
|
|
|
if not empty(tag_str):
|
|
|
|
tags = re.split('[\s,]', tag_str)
|
|
|
|
tags = [t for t in tags if not empty(t)] # just in case
|
2019-01-04 15:57:34 -08:00
|
|
|
|
|
|
|
if not empty(title):
|
|
|
|
heading = title
|
|
|
|
parts.append(url)
|
|
|
|
|
2019-01-04 15:34:42 -08:00
|
|
|
# TODO not sure, maybe add as org quote?
|
|
|
|
if not empty(selection):
|
|
|
|
parts.extend([
|
|
|
|
'Selection:',
|
2019-01-04 15:57:34 -08:00
|
|
|
selection, # TODO tabulate?
|
2019-01-04 15:34:42 -08:00
|
|
|
])
|
|
|
|
if not empty(comment):
|
|
|
|
parts.extend([
|
|
|
|
'Comment:',
|
|
|
|
comment
|
|
|
|
])
|
|
|
|
body = None if len(parts) == 0 else '\n'.join(parts)
|
|
|
|
|
|
|
|
|
|
|
|
response = {
|
|
|
|
'file': str(CAPTURE_PATH),
|
|
|
|
}
|
|
|
|
try:
|
|
|
|
append_org_entry(
|
|
|
|
CAPTURE_PATH,
|
2019-01-04 15:57:34 -08:00
|
|
|
heading=heading,
|
2019-01-04 15:34:42 -08:00
|
|
|
body=body,
|
2019-01-06 12:44:00 -08:00
|
|
|
tags=tags,
|
2019-01-04 15:34:42 -08:00
|
|
|
todo=False,
|
|
|
|
)
|
|
|
|
response.update({
|
|
|
|
'status': 'ok',
|
|
|
|
})
|
|
|
|
except Exception as e:
|
|
|
|
log(str(e))
|
|
|
|
response.update({
|
|
|
|
'status': 'error',
|
|
|
|
'error' : str(e),
|
|
|
|
})
|
|
|
|
|
|
|
|
return json.dumps(response).encode('utf8')
|