Implement server bit to actually capture

This commit is contained in:
Dima Gerasimov 2019-01-04 23:34:42 +00:00
parent 5b871fed90
commit 5f5fd0e132
2 changed files with 60 additions and 15 deletions

View file

@ -1,9 +1,21 @@
"""First API, local access only""" """First API, local access only"""
import json
import hug # type: ignore import hug # type: ignore
import hug.types as T # 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.local()
@hug.post('/capture') @hug.post('/capture')
def capture( def capture(
@ -11,10 +23,42 @@ def capture(
selection: T.Nullable(T.text), selection: T.Nullable(T.text),
comment: T.Nullable(T.text), comment: T.Nullable(T.text),
): ):
# TODO can even configure the path in browser? but fine for now.. log("capturing", url, selection, comment)
print("HELOOO")
"""Says happy birthday to a user""" parts = []
print(url, selection, comment) # TODO not sure, maybe add as org quote?
return {} if not empty(selection):
# return {'message': 'Happy {0} Birthday {1}!'.format(age, name), parts.extend([
# 'took': float(hug_timer)} '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')

View file

@ -10,20 +10,20 @@ function makeCaptureRequest(
comment: ?string=null, comment: ?string=null,
// TODO anything alse?? // TODO anything alse??
) { ) {
const data = JSON.stringify({
'url': url,
'selection': selection,
'comment': comment,
});
if (url == null) { if (url == null) {
showNotification('ERROR: trying to capture null'); showNotification('ERROR: trying to capture null');
return; return;
} }
var request = new XMLHttpRequest(); const data = JSON.stringify({
'url': url,
'selection': selection,
'comment': comment,
});
console.log(`[background] capturing ${data}`); console.log(`[background] capturing ${data}`);
var request = new XMLHttpRequest();
request.open('POST', capture_url(), true); request.open('POST', capture_url(), true);
request.onreadystatechange = () => { request.onreadystatechange = () => {
if (request.readyState != 4) { if (request.readyState != 4) {
@ -31,7 +31,7 @@ function makeCaptureRequest(
} }
console.log('[background] status:', request.status); console.log('[background] status:', request.status);
if (request.status >= 200 && request.status < 400) { // success 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}`); console.log(`[background] success: ${response}`);
showNotification(`OK: ${response}`); showNotification(`OK: ${response}`);
} else { } else {
@ -62,6 +62,7 @@ function makeCaptureRequest(
// } // }
// }); // });
// TODO hmm. I suppose we wanna title + url instead...
// TODO handle cannot access chrome:// url?? // TODO handle cannot access chrome:// url??
// TODO ok, need to add comment popup? // TODO ok, need to add comment popup?
chrome.browserAction.onClicked.addListener(tab => { chrome.browserAction.onClicked.addListener(tab => {