Skip to content

Instantly share code, notes, and snippets.

@jstvz
Last active December 4, 2018 08:28
Show Gist options
  • Save jstvz/35f0af8f23c740b0ae5cbd30adc5cb8b to your computer and use it in GitHub Desktop.
Save jstvz/35f0af8f23c740b0ae5cbd30adc5cb8b to your computer and use it in GitHub Desktop.
Automatic note download of Sony Digital Paper on Arch Linux
#!/usr/bin/env python3
"""
usage: dpt-notes-sync.py ip_address
"""
import datetime
import json
import os
from dateutil import parser as dateparser
from dptrp1.dptrp1 import DigitalPaper
SYNC_DIR = '/home/je/org/pdf/'
def connect(address):
"""
Loads the key and client ID to authenticate with the DPT-RP1
"""
with open('/home/je/.dpt-client', 'r') as f:
client_id = f.readline().strip()
with open('/home/je/.dpt.key', 'r') as f:
key = f.read()
dpt = DigitalPaper(address)
dpt.authenticate(client_id, key)
return dpt
def download_notes(dpt):
"""
Given an authenticated DigitalPaper instance, download all note files to a
specified directory.
"""
for doc in [f for f in dpt.list_documents() if is_modified_note(f)]:
data = dpt.download(doc['entry_path'])
local_path = SYNC_DIR + os.path.basename(doc['entry_path'])
with open(local_path, 'wb') as f:
f.write(data)
print('Saved {} to {}'.format(doc['entry_path'], local_path))
def is_modified_note(doc):
if doc['document_type'] == 'note':
local_path = SYNC_DIR + os.path.basename(doc['entry_path'])
if not os.path.exists(local_path):
return True
else:
return os.path.getmtime(local_path) < dateparser.parse(
doc['modified_date']).timestamp()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('address', help="IP address of the DPT-RP1")
args = parser.parse_args()
try:
dpt = connect(args.address)
download_notes(dpt)
except OSError:
print(
'Unable to reach device, verify it is connected to the same network segment.'
)
exit(1)
#!/usr/bin/env bash
DPT_IP=`avahi-resolve-host-name digitalpaper.local | cut -f 2`
/home/je/bin/scripts/dpt-notes-sync.py $DPT_IP
[Unit]
Description=Sony DPT-RP1 note synchronization service
After=network.target network-online.target dbus.socket
[Service]
Type=oneshot
ExecStartPre=/usr/bin/bash -c "/usr/bin/systemctl --user set-environment DPT_IP=$(avahi-resolve-host-name digitalpaper.local | cut -f 2)"
ExecStart=-%h/bin/scripts/dpt-notes-sync.py ${DPT_IP}
[Unit]
Description=Sony DPT-RP1 note synchronization service
[Timer]
OnBootSec=2m
OnUnitActiveSec=2h
Unit=dptsync.service
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment