-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbkm-org.py
264 lines (226 loc) · 6.34 KB
/
bkm-org.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import argparse
from pathlib import Path
from modules import log
from modules import config
from bookmark import Bookmark, BookmarkCollection
config = config.get_config('config')
log_path = Path(config['global']['log_path'])
logger = log.get_logger('bkm-org', log_path=log_path)
def main(args):
if args['bookmarkfile']:
collection_fpath = Path(args['bookmarkfile'])
if not collection_fpath.exists():
collection_fpath.touch()
else:
collection_fpath = Path(config['bkm-org']['collection_fpath'])
if args['sync']:
utype = args['sync']
bc = BookmarkCollection(collection_fpath)
sync_bookmarks(bc, utype)
return
if args['import']:
itype = args['import'][0]
fpath = args['import'][1]
bc = BookmarkCollection()
if collection_fpath.stat().st_size > 0:
bc.load(collection_fpath)
import_bookmarks(bc, itype, fpath, collection_fpath)
return
urls = None
if args['list']:
bc = BookmarkCollection(collection_fpath)
ltype = args['list'][0]
value = args['list'][1] if len(args['list']) > 1 else None
if value:
urls = bc.get_urls(ltype, value)
if should_print(args):
print_list(urls)
return
else:
grouped_urls = bc.get_grouped_urls(ltype)
if should_print(args):
print_dict(grouped_urls)
return
if args['validate']:
bc = BookmarkCollection(collection_fpath)
if urls:
for u in urls:
validate_url(bc, u)
print()
else:
url = args['validate']
if url == 'collection':
bc.validate()
save_bookmarks(bc)
else:
validate_url(bc, url)
return
if args['add']:
bc = BookmarkCollection(collection_fpath)
if urls:
tags = args['add'][0].split(',')
for u in urls:
if bc.add_tags(u, tags):
print(f"{u}: successfully added tags '{tags}'")
else:
print(f"{u}: not able to add tags '{tags}'")
save_bookmarks(bc)
else:
url = args['add'][0]
tags = args['add'][1].split(',') if len(args['add']) > 1 else None
add_urls_andor_tags(bc, url, tags)
save_bookmarks(bc)
return
if args['delete']:
bc = BookmarkCollection(collection_fpath)
if urls:
for u in urls:
delete_url(bc, u)
save_bookmarks(bc)
else:
url = args['delete'][0]
tag = args['delete'][1] if len(args['delete']) > 1 else None
if tag:
delete_tag(bc, url, tag)
else:
delete_url(bc, url)
save_bookmarks(bc)
return
def should_print(args):
return args['validate'] is None and args['delete'] is None and args['add'] is None
def validate_url(bc, url):
b = Bookmark(url)
if b.verify():
print_bookmark(b)
else:
print('not able to validate')
def print_bookmark(b):
print(f"""url: {b.url}
connected: {b.last_request.connected}
status: {b.last_request.status}
media-type: {b.mtype}
title: {b.last_request.title}
redirect: {b.last_request.redirect}""")
def print_list(li):
print('\n'.join(li))
def print_dict(d):
for key in sorted(d.keys()):
print(f'{key}:')
for url in d[key]:
print(f' {url}')
def sync_bookmarks(bc, utype):
if utype == 'url':
bc.sync_urls()
bc.write()
elif utype == 'title':
bc.sync_titles()
bc.write()
elif utype == 'md':
bc.write_md()
elif utype == 'json':
bc.import_md()
bc.write_json()
def import_bookmarks(bc, itype, input, output):
if itype == 'nbff':
bc.import_nbff(input)
elif itype == 'insta':
bc.import_instapaper(input)
bc.write(output)
def add_urls_andor_tags(bc, url, tags):
if bc.add_url(url, tags=tags):
print(f'{url}: successfully added to collection and saved at {bc.fpath}')
elif bc.add_tags(url, tags):
print(f"{url}: successfully added tags '{tags}' to url and saved at {bc.fpath}")
else:
print(f'{url}: not able to add url to collection')
def delete_tag(bc, url, tag):
if bc.delete_tag(url, tag):
print(f"{url}: successfully deleted tag '{tag}' and saved at {bc.fpath}")
else:
print(f"{url}: not able to delete tag '{tag}'")
def delete_url(bc, url):
if bc.delete_url(url):
print(f'{url}: successfully deleted from collection and saved at {bc.fpath}')
else:
print(f'{url}: not able to delete url from collection')
def save_bookmarks(bc):
bc.write()
print(f'saved at {bc.fpath}')
def get_parser():
parser = argparse.ArgumentParser(
description='Bookmark file manager',
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument(
'-f',
'--bookmark-file',
dest='bookmarkfile',
action='store',
help='Specify bookmark file'
),
parser.add_argument(
'-v',
'--validate',
action='store',
nargs='?',
const='collection',
help='Validate urls. If not url is given, validate bookmark collection'
),
parser.add_argument(
'-l',
'--list',
action='store',
nargs='+',
help="""List urls by property. If no parameter is given to the property, it returns all urls grouped by the property:
'status':
0: returns urls which failed to connect
10: returns urls with unknown status
<code>: returns urls with status code <code>
'tag':
<tag_name>: return urls by tag <tag_name>
'created':
<date>: return urls by creation date <date> with format 'yyyy-mm-dd'
'domain':
<domain>: return urls by <domain>'
'media':
<media_type>: return urls by <media-type>'"""
),
parser.add_argument(
'-s',
'--sync',
action='store',
choices=['url', 'title', 'md', 'json'],
help='Sync json file'
),
parser.add_argument(
'-i',
'--import',
action='store',
nargs=2,
help="""Import bookmarks from:
nbff: Netscape Bookmark File format
insta: Instapaper"""
),
parser.add_argument(
'-a',
'--add',
nargs='+',
action='store',
help='Add url and tags if provided (tags are separated by comma and without space). If url exists, adds tags to url'
),
parser.add_argument(
'-d',
'--delete',
nargs='?',
const='piped_urls',
action='store',
help='Delete url or tag (if tag provided, url exists and has the tag)'
)
return parser
if __name__ == "__main__":
parser = get_parser()
args = vars(parser.parse_args())
try:
main(args)
except Exception as e:
logger.exception(e)