From 5f5fd0e1324e3849c0725bd196fc85eea665ee79 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Fri, 4 Jan 2019 23:34:42 +0000 Subject: [PATCH] Implement server bit to actually capture --- server.py | 58 ++++++++++++++++++++++++++++++++++++++------ src/js/background.js | 17 +++++++------ 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/server.py b/server.py index 99869c1..7a45c5b 100644 --- a/server.py +++ b/server.py @@ -1,9 +1,21 @@ """First API, local access only""" +import json + import hug # type: ignore import hug.types as T # type: ignore +from kython.org import append_org_entry +from config import CAPTURE_PATH +def empty(s) -> bool: + return s is None or len(s.strip()) == 0 + +def log(*things): + # TODO proper logging + print(*things) + +# TODO allow to pass tags?? @hug.local() @hug.post('/capture') def capture( @@ -11,10 +23,42 @@ def capture( selection: T.Nullable(T.text), comment: T.Nullable(T.text), ): - # TODO can even configure the path in browser? but fine for now.. - print("HELOOO") - """Says happy birthday to a user""" - print(url, selection, comment) - return {} - # return {'message': 'Happy {0} Birthday {1}!'.format(age, name), - # 'took': float(hug_timer)} + log("capturing", url, selection, comment) + + parts = [] + # TODO not sure, maybe add as org quote? + if not empty(selection): + parts.extend([ + 'Selection:', + selection, + ]) + 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, + heading=url, + body=body, + tags=['grasp'], + 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') diff --git a/src/js/background.js b/src/js/background.js index 8a8f53d..e8cdeef 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -10,20 +10,20 @@ function makeCaptureRequest( comment: ?string=null, // TODO anything alse?? ) { - const data = JSON.stringify({ - 'url': url, - 'selection': selection, - 'comment': comment, - }); - if (url == null) { showNotification('ERROR: trying to capture null'); return; } - var request = new XMLHttpRequest(); + const data = JSON.stringify({ + 'url': url, + 'selection': selection, + 'comment': comment, + }); console.log(`[background] capturing ${data}`); + + var request = new XMLHttpRequest(); request.open('POST', capture_url(), true); request.onreadystatechange = () => { if (request.readyState != 4) { @@ -31,7 +31,7 @@ function makeCaptureRequest( } console.log('[background] status:', request.status); if (request.status >= 200 && request.status < 400) { // success - var response = JSON.parse(request.response); + var response = JSON.parse(request.responseText); console.log(`[background] success: ${response}`); showNotification(`OK: ${response}`); } else { @@ -62,6 +62,7 @@ function makeCaptureRequest( // } // }); +// TODO hmm. I suppose we wanna title + url instead... // TODO handle cannot access chrome:// url?? // TODO ok, need to add comment popup? chrome.browserAction.onClicked.addListener(tab => {