|
#!/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) |