-
Notifications
You must be signed in to change notification settings - Fork 1
/
updater.py
67 lines (49 loc) · 1.49 KB
/
updater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import sys
import json
import requests
import datetime
from hashlib import md5
from wfapi import Workflowy, error
COUCHDB_URL = os.environ['COUCHDB_URL']
def main():
res = requests.get(COUCHDB_URL + '/_design/apps/_view/apps').json()
wfshids = {row['key'][1]: row['key'][4] for row in res['rows']}
for wfshid, hash in wfshids.items():
# TODO deal with deleted or unshared wfitems
process(wfshid, hash)
def process(wfshid, hash=None):
print(datetime.datetime.now(), 'processing', wfshid, 'hash', hash)
try:
wf = Workflowy(wfshid)
except error.WFLoginError:
print('login error.')
return
root = build(wf.root)
newhash = md5(json.dumps(root, sort_keys=True).encode('utf-8')).hexdigest()
print('newhash', newhash)
if hash and hash == newhash:
print("hasn't changed.\n")
return
r = requests.put(COUCHDB_URL + '/_design/apps/_update/update-data/' +
wfshid, data=json.dumps(root), params={'hash': newhash})
print(r.text, r.headers)
def build(node):
data = extract(node)
data['children'] = []
for child in node:
data['children'].append(build(child))
return data
def extract(node):
return {
'name': node.name,
'note': node.description,
'last': node.last_modified,
'id': node.projectid,
'cp': node.is_completed
}
if __name__ == '__main__':
if len(sys.argv) >= 2:
process(sys.argv[1])
else:
main()