-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_netscape_bookmarks.py
executable file
·103 lines (86 loc) · 2.47 KB
/
generate_netscape_bookmarks.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
#!/usr/bin/env python3
"""
Generates Netscape bookmark dumps
See:
- https://github.com/shaarli/netscape-bookmark-parser
"""
from argparse import ArgumentParser
from random import randint
from faker import Faker
HEADER = '''<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is an automatically generated file.
It will be read and overwritten.
Do Not Edit! -->
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>'''
START_TAG = '<DL><p>'
END_TAG = '</DL><p>'
T_BOOKMARK_DT = (
'<DT><A HREF="{url}" ADD_DATE="{date}" PRIVATE="{private}"'
' TAGS="{tags}">{title}</A>'
)
T_BOOKMARK_DD = '<DD>{description}'
class FakeBookmark():
"""Bookmark entry generated by Faker"""
# pylint: disable=too-few-public-methods
def __init__(self, fake):
# pylint: disable=no-member
self.fake = fake
self.url = fake.uri()
self.date = fake.unix_time()
self.private = randint(0, 1)
self.tags = fake.words(nb=randint(0, 5))
self.title = fake.sentence(nb_words=randint(1, 10))
self.description = fake.paragraphs(nb=randint(0, 2))
def netscape_str(self):
"""Netscape entry representation"""
bkm = T_BOOKMARK_DT.format(
url=self.url,
date=self.date,
private=self.private,
tags=' '.join(self.tags),
title=self.title
)
if self.description:
bkm = '{dd}\n{dt}'.format(
dd=bkm,
dt=T_BOOKMARK_DD.format(description='\n'.join(self.description))
)
return bkm
def generate_bookmarks(locale, number):
"""Generate a fake Netscape bookmark list"""
fake = Faker(locale)
bookmarks = [FakeBookmark(fake).netscape_str() for _ in range(number)]
return '\n'.join([
HEADER,
START_TAG,
'\n'.join(bookmarks),
END_TAG
])
def main():
"""Main entrypoint"""
parser = ArgumentParser()
parser.add_argument(
'-l',
'--locale',
default=None,
help="Locale for the generated content"
)
parser.add_argument(
'-n',
'--number',
type=int,
default=1000,
help="Number of bookmarks to generate"
)
parser.add_argument(
'-o',
'--output',
default='bookmarks.htm',
help="Output file"
)
args = parser.parse_args()
with open(args.output, 'w') as f_out:
f_out.write(generate_bookmarks(args.locale, args.number))
if __name__ == '__main__':
main()