-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from breezykermo/topic/abstract-clients
Python and DevonTHINK clients, Docker, and ability to clip with a note
- Loading branch information
Showing
17 changed files
with
233 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
clips.json | ||
assets/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
FROM python:3.9-slim-buster | ||
RUN mkdir -p /app | ||
WORKDIR /app | ||
ADD server.py /app/server.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
(* | ||
Clip contents of existing selection to a hili server. | ||
See https://github.com/breezykermo/hili for more info. | ||
*) | ||
on replace_chars(this_text, search_string, replacement_string) | ||
set AppleScript's text item delimiters to the search_string | ||
set the item_list to every text item of this_text | ||
set AppleScript's text item delimiters to the replacement_string | ||
set this_text to the item_list as string | ||
set AppleScript's text item delimiters to "" | ||
return this_text | ||
end replace_chars | ||
|
||
tell application id "DNtp" | ||
try | ||
set theDoc to the content record of think window 1 | ||
set theTitle to ((the name without extension of theDoc) as string) | ||
set theRefURL to the reference URL of theDoc as string | ||
set thePage to ((the current page of think window 1) as string) | ||
set theUrl to theRefURL & "?page=" & (thePage as string) | ||
|
||
set theCitedText to the (selected text of think window 1 as string) | ||
if theCitedText is "" then | ||
(* Scenario 2: Document is open, but no text is highlighted. *) | ||
set theQuotedText to missing value | ||
else | ||
(* Scenario 3: Document is open, text is highlighted. *) | ||
set theQuotedText to my theCitedText | ||
end if | ||
|
||
-- set theTags to the tags of theDoc | ||
set _note to display dialog "note" default answer "" buttons {"Cancel", "Continue"} default button "Continue" | ||
set _tags to display dialog "tags" default answer "" buttons {"Cancel", "Continue"} default button "Continue" | ||
set theNote to the text returned of _note | ||
set theTags to the text returned of _tags | ||
set theQuote to my replace_chars(theQuotedText, "\\n", "\\\\n") | ||
|
||
do shell script "touch /tmp/args.txt" | ||
do shell script "echo " & quoted form of theTitle & " > /tmp/args.txt" | ||
do shell script "echo '*--STARTQUOTE--*' >> /tmp/args.txt" | ||
do shell script "echo " & quoted form of theQuote & " >> /tmp/args.txt" | ||
do shell script "echo '*--ENDQUOTE--*' >> /tmp/args.txt" | ||
do shell script "echo " & quoted form of theNote & " >> /tmp/args.txt" | ||
do shell script "echo " & quoted form of theTags & " >> /tmp/args.txt" | ||
do shell script "echo " & quoted form of theUrl & " >> /tmp/args.txt" | ||
|
||
(* NOTE: change the following line to suit your system *) | ||
do shell script "cd /absolute/path/to/hili/clients/python && URL='http://localhost:8888' python3 clip.py > /tmp/hili_clip_log.txt" | ||
|
||
on error error_message number error_number | ||
if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning | ||
end try | ||
end tell |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import sys | ||
import os | ||
import json | ||
import requests | ||
import time | ||
|
||
# cheap args | ||
env_args = os.environ.get("ARGS") | ||
env_url = os.environ.get("URL") | ||
env_pw = os.environ.get("PASSWORD") | ||
|
||
if env_url is None: | ||
sys.exit("You must specify a server 'URL' in the environment.") | ||
|
||
# globals | ||
ARGS = env_args if env_args is not None else "/tmp/args.txt" | ||
SERVER_URL = env_url | ||
PASSWORD = env_pw | ||
CACHE = "./cached_clips.json" # not in temp so it isn't removed | ||
|
||
|
||
def send(body): | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Accept": "application/json", | ||
} | ||
if PASSWORD is not None: | ||
headers["Authentication"] = PASSWORD | ||
|
||
requests.post( | ||
SERVER_URL, | ||
headers = headers, | ||
json = body | ||
) | ||
|
||
|
||
def attempt_clip(clip): | ||
try: | ||
send(clip) | ||
|
||
# if clip is successful, flush all cached | ||
if os.path.exists(CACHE): | ||
with open(CACHE, "r") as c: | ||
cached_clips = [json.loads(l) for l in c.readlines()] | ||
|
||
for cached_clip in cached_clips: | ||
send(cached_clip) | ||
|
||
os.remove(CACHE) | ||
|
||
# TODO: only catch the specifics | ||
except requests.ConnectionError: | ||
is_first = not os.path.exists(CACHE) | ||
with open(CACHE, "a") as cache: | ||
if not is_first: cache.write("\n") | ||
json.dump(clip, cache) | ||
print("No internet connection, dumped to cache") | ||
|
||
|
||
def run(): | ||
with open(ARGS, 'r') as f: | ||
data = f.readlines() | ||
tm = int(round(time.time() * 1000)) | ||
|
||
idx = 0 | ||
title = "" | ||
while data[idx] != "*--STARTQUOTE--*\n" and idx < len(data): | ||
title += data[idx] | ||
idx += 1 | ||
|
||
idx += 1 # skip STARTQUOTE | ||
quote = "" | ||
while data[idx] != "*--ENDQUOTE--*\n" and idx < len(data): | ||
quote += data[idx] | ||
idx += 1 | ||
idx += 1 # skip ENDQUOTE | ||
|
||
note = data[idx].rstrip("\n").strip() | ||
tags = data[idx + 1].rstrip("\n").strip().split(",") | ||
if len(tags) == 1 and tags[0] == "": tags = [] | ||
url = data[idx + 2].rstrip("\n").strip() | ||
|
||
clip = { | ||
"time": tm, | ||
"title": title, | ||
"html": quote, | ||
"note": note, | ||
"tags": tags, | ||
"href": url, | ||
} | ||
|
||
attempt_clip(clip) | ||
|
||
|
||
if __name__ == "__main__": | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Title of the document | ||
*--STARTQUOTE--* | ||
This is the quote you want to send to hili, usually drawn from whatever is highlighted in an application at the time of clipping. | ||
*--ENDQUOTE--* | ||
Here is the note that you've written, normally entered through a prompt at clip time. | ||
test,example | ||
https://lachlankermode.com/example-url-for-hili |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Simple python script to send a clip to a hili server. | ||
|
||
## Run | ||
```bash | ||
URL=http://localhost:8888 ARGS=$(pwd)/example.txt python clip.py | ||
``` | ||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
version: "3.9" | ||
services: | ||
hili: | ||
build: . | ||
command: python server.py /app/clips.json /app/assets | ||
volumes: | ||
- ./clips.json:/app/clips.json | ||
- ./assets:/app/assets | ||
ports: | ||
- "8888:8888" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters