2019-01-07 14:53:08 -08:00
|
|
|
#!/usr/bin/python3
|
|
|
|
import argparse
|
2019-01-07 15:35:18 -08:00
|
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
2019-01-04 15:34:42 -08:00
|
|
|
import json
|
2019-01-07 14:53:08 -08:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
2019-01-07 15:35:18 -08:00
|
|
|
import re
|
|
|
|
from typing import List
|
2019-01-04 13:27:24 -08:00
|
|
|
|
2019-01-06 15:57:15 -08:00
|
|
|
from org_tools import append_org_entry
|
2019-01-04 13:27:24 -08:00
|
|
|
|
2019-01-07 14:53:08 -08:00
|
|
|
CAPTURE_PATH_VAR = 'GRASP_CAPTURE_PATH'
|
|
|
|
|
2019-01-04 15:34:42 -08:00
|
|
|
def log(*things):
|
|
|
|
# TODO proper logging
|
|
|
|
print(*things)
|
|
|
|
|
2019-01-07 15:35:18 -08:00
|
|
|
|
2019-01-04 13:27:24 -08:00
|
|
|
def capture(
|
2019-01-07 15:35:18 -08:00
|
|
|
url,
|
|
|
|
title,
|
|
|
|
selection,
|
|
|
|
comment,
|
|
|
|
tag_str,
|
2019-01-04 13:27:24 -08:00
|
|
|
):
|
2019-01-07 15:35:18 -08:00
|
|
|
def empty(s) -> bool:
|
|
|
|
return s is None or len(s.strip()) == 0
|
|
|
|
|
2019-01-04 15:57:34 -08:00
|
|
|
log("capturing", url, title, selection, comment)
|
2019-01-04 15:34:42 -08:00
|
|
|
|
2019-01-07 14:53:08 -08:00
|
|
|
capture_path = Path(os.environ[CAPTURE_PATH_VAR]).expanduser()
|
|
|
|
|
2019-01-04 15:57:34 -08:00
|
|
|
heading = url
|
2019-01-04 15:34:42 -08:00
|
|
|
parts = []
|
2019-01-07 15:35:18 -08:00
|
|
|
tags: List[str] = []
|
2019-01-06 12:44:00 -08:00
|
|
|
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 = {
|
2019-01-07 15:56:06 -08:00
|
|
|
'path': str(capture_path),
|
2019-01-04 15:34:42 -08:00
|
|
|
}
|
2019-01-07 15:56:06 -08:00
|
|
|
# try:
|
|
|
|
append_org_entry(
|
|
|
|
capture_path,
|
|
|
|
heading=heading,
|
|
|
|
body=body,
|
|
|
|
tags=tags,
|
|
|
|
todo=False,
|
|
|
|
)
|
|
|
|
response.update({
|
|
|
|
'status': 'ok',
|
|
|
|
})
|
|
|
|
# just rely on setting 500 instead
|
|
|
|
# except Exception as e:
|
|
|
|
# log(str(e))
|
|
|
|
# response.update({
|
|
|
|
# 'status': 'error',
|
|
|
|
# 'error' : str(e),
|
|
|
|
# })
|
2019-01-04 15:34:42 -08:00
|
|
|
|
|
|
|
return json.dumps(response).encode('utf8')
|
2019-01-07 14:53:08 -08:00
|
|
|
|
2019-01-07 15:35:18 -08:00
|
|
|
|
|
|
|
|
|
|
|
class GraspRequestHandler(BaseHTTPRequestHandler):
|
|
|
|
def handle_POST(self):
|
|
|
|
content_length = int(self.headers['Content-Length'])
|
|
|
|
post_data = self.rfile.read(content_length)
|
|
|
|
payload = json.loads(post_data.decode('utf8'))
|
|
|
|
log("incoming request:")
|
|
|
|
log(payload)
|
|
|
|
res = capture(**payload)
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header('Content-Type', 'application/json')
|
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write(res)
|
|
|
|
|
|
|
|
def respond_error(self, message: str):
|
|
|
|
self.send_response(500)
|
|
|
|
self.send_header('Content-Type', 'text/html')
|
|
|
|
self.end_headers()
|
|
|
|
self.wfile.write(message.encode('utf8'))
|
|
|
|
|
|
|
|
def do_POST(self):
|
|
|
|
try:
|
|
|
|
self.handle_POST()
|
|
|
|
except Exception as e:
|
|
|
|
log("Error during processing")
|
|
|
|
log(str(e))
|
|
|
|
self.respond_error(message=str(e))
|
|
|
|
|
|
|
|
|
2019-01-07 14:53:08 -08:00
|
|
|
def run(port: str, capture_path: str):
|
|
|
|
# not sure if there is a simpler way to communicate with the server...
|
2019-01-07 15:35:18 -08:00
|
|
|
os.environ[CAPTURE_PATH_VAR] = capture_path
|
|
|
|
httpd = HTTPServer(('', int(port)), GraspRequestHandler)
|
|
|
|
log(f"Starting httpd on port {port}")
|
|
|
|
httpd.serve_forever()
|
2019-01-07 14:53:08 -08:00
|
|
|
|
|
|
|
def setup_parser(p):
|
|
|
|
p.add_argument('--port', type=str, default='12212', help='Port for communicating with extension')
|
|
|
|
p.add_argument('--path', type=str, default='~/capture.org', help='File to capture into')
|
|
|
|
|
|
|
|
def main():
|
|
|
|
p = argparse.ArgumentParser('grasp server setup', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
|
|
|
setup_parser(p)
|
|
|
|
args = p.parse_args()
|
|
|
|
run(args.port, args.path)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|