-
Notifications
You must be signed in to change notification settings - Fork 3
/
onetab_to_bookmarks.py
89 lines (73 loc) · 2.4 KB
/
onetab_to_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
#!/usr/bin/env python3
#
# Onetab to HTML bookmark converter
# Brian Landers <brian@packetslave.com>
#
# See http://one-tab.com for more information about OneTab. This tool
# is not affiliated with them in any way.
#
# usage: onetab_to_bookmarks.py [-h] [--input_file [INPUT_FILE]]
# [--output_file [OUTPUT_FILE]]
#
# Convert OneTab export to HTML bookmarks
#
# optional arguments:
# -h, --help show this help message and exit
# --input_file [INPUT_FILE]
# Path to OneTab file. STDIN if omitted.
# --output_file [OUTPUT_FILE]
# Path to bookmarks file. STDOUT if omitted
#
import argparse
import sys
import time
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>
<DL>"""
FOOTER = "<DL>\n"
def main(args):
if args.output_file:
out = open(args.output_file, "w")
else:
out = sys.stdout
if args.input_file:
with open(args.input_file) as f:
onetabs = f.readlines()
else:
onetabs = sys.stdin.readlines()
# Pinboard (for example) doesn't like it when you have a ton of bookmarks
# all with the same timestamp. Hack around that by setting each entry to
# be N seconds in the past.
now = int(time.time() - (len(onetabs) * 65))
out.write(HEADER)
for onetab in onetabs:
if not onetab.startswith("http"):
continue
fields = onetab.strip().split(" | ", 1)
site = fields[0]
name = fields[1] if len(fields) > 1 else site
out.write(
f'<DT><A HREF="{site}" LAST_VISIT="{now}" LAST_MODIFIED="{now}">'
f'{name}</A></DT>\n'
)
now += 65
out.write(FOOTER)
if args.output_file:
out.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="Convert OneTab export to HTML bookmarks",
epilog="(c) Brian Landers <brian@packetslave.com>")
parser.add_argument(
"--input_file", nargs="?",
help="Path to OneTab file. STDIN if omitted.")
parser.add_argument(
"--output_file", nargs="?",
help="Path to bookmarks file. STDOUT if omitted")
args = parser.parse_args()
main(args)