-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdate_links.py
76 lines (65 loc) · 2.38 KB
/
update_links.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
68
69
70
71
72
73
74
75
76
#!/bin/python3
import os
import re
import json
from urllib.request import urlopen
from urllib.parse import urlencode
patternkey = re.compile(r'\[\[zotero://[\d\.:]+/zotxt/select\?(\w*)key=([\w:]+)\|(.*?)\]\]')
zotxtroot = 'http://127.0.0.1:23119/zotxt/items?'
def fetchkey(easykey):
data = {
'easykey':easykey,
'format': 'key'
}
url = zotxtroot + urlencode(data)
resp = json.loads(urlopen(url).read().decode('utf-8'))
return resp[0]
def matchkey(m):
if m.group(1) == '':
key = m.group(2)
elif m.group(1) == 'easy':
key = ''
try:
key = fetchkey(m.group(2))
except Exception as e:
print(f'ERROR: got an exception {e}\n{m.group(0)}')
elif m.group(1) == 'betterbibtex':
key = '@' + m.group(2)
else:
key = ''
print(f'ERROR: found something unspecified: {m.group(0)}')
return f'[[zotero://select/items/{key}|{m.group(3)}]]'
def updatefile(filename, backup=True):
backupfile = filename + '.bak'
os.rename(filename, backupfile)
with open(backupfile, 'r') as bf, open(filename, 'w') as f:
for line in bf.readlines():
line = patternkey.sub(matchkey, line)
f.write(line)
if not backup:
os.remove(backupfile)
def walkdir(dirname, backup=True):
for dirpath, dirnames, files in os.walk(dirname):
print(f'Found directory: {dirpath}')
for filename in files:
filename = os.path.join(dirpath, filename)
if not filename.endswith('.txt'):
print(f'{filename} Skipped')
continue
updatefile(filename, backup)
print(f'{filename} Processed')
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='This program converts the Zotero links '
'of the old format into the Zotero own link. Zotero should run.')
parser.add_argument('-b', '--nobackup', action='store_false',
help='Do not retain the original files as *.bak')
def dirpath(string):
path = os.path.abspath(string)
if not os.path.isdir(path):
msg = 'is not a directory'
raise argparse.ArgumentTypeError(msg)
return path
parser.add_argument('dir', type=dirpath, help='Root directory of the zim wiki')
args = parser.parse_args()
walkdir(args.dir, args.nobackup)