-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlbookmarks.py
executable file
·179 lines (151 loc) · 5.78 KB
/
htmlbookmarks.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
import regex
import re
import os
import argparse
import sys
import urllib
from urllib.parse import urlparse
from datetime import datetime, timedelta, timezone
from bs4 import BeautifulSoup
import urllib.request
#
# MIT License
#
# https://opensource.org/licenses/MIT
#
# Copyright 2020 Rene Sugar
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Description:
#
# This program extracts URLs from a browser HTML bookmarks backup file.
#
def remove_line_breakers(s):
if s is None:
return s
t = s.replace('\n', ' ')
t = t.replace('\r', ' ')
t = t.replace('\v', ' ')
t = t.replace('\x0b', ' ')
t = t.replace('\f', ' ')
t = t.replace('\x0c', ' ')
t = t.replace('\x1c', ' ')
t = t.replace('\x1d', ' ')
t = t.replace('\x1e', ' ')
t = t.replace('\x85', ' ')
t = t.replace('\u2028', ' ')
t = t.replace('\u2029', ' ')
return t
def checkExtension(file, exts):
name, extension = os.path.splitext(file)
extension = extension.lstrip(".")
processFile = 0
if len(extension) == 0:
processFile = 0
elif len(exts) == 0:
processFile = 1
elif extension in exts:
processFile = 1
else:
processFile = 0
return processFile
def checkExclusion(dir, rootPath, excludePaths):
processDir = 0
if (dir[0:1] == "."):
processDir = 0
elif os.path.join(rootPath,dir) in excludePaths:
processDir = 0
else:
processDir = 1
return processDir
def filelist(dir, excludePaths, exts):
allfiles = []
for path, subdirs, files in os.walk(dir):
files = [os.path.join(path,x) for x in files if checkExtension(x, exts)]
# "[:]" alters the list of subdirectories walked by os.walk
# https://stackoverflow.com/questions/10620737/efficiently-removing-subdirectories-in-dirnames-from-os-walk
subdirs[:] = [os.path.join(path,x) for x in subdirs if checkExclusion(x, path, excludePaths)]
allfiles.extend(files)
for x in subdirs:
allfiles.extend(filelist(x, excludePaths, exts))
return allfiles
def main():
parser = argparse.ArgumentParser(description="htmlbookmarks")
parser.add_argument("--path", help="Base path of the project to be scanned", default=".")
parser.add_argument("--root", help="Root path of the project to be scanned", default="/")
parser.add_argument("--prefix", help="Replace root path with this prefix", default="/")
parser.add_argument("--extensions", help="File extensions that are processed", default=".html.htm.txt")
parser.add_argument("--exclude", nargs='*', help="Paths of folders to exclude", default=[])
parser.add_argument("--output", help="Path to output file", default="./output.txt")
parser.add_argument('--backup', help="Bookmark backup format", dest='backup', action='store_true', default=False)
args = vars(parser.parse_args())
outputFile = os.path.abspath(os.path.expanduser(args['output']))
if os.path.isfile(outputFile):
# Don't overwrite existing files in case the output file has the same name
# as a project file and the output path is in a project directory.
print("Error: output file '" + outputFile + "' already exists.")
sys.exit(1)
basePath = os.path.abspath(os.path.expanduser(args['path']))
rootPath = args['root']
rootPrefix = args['prefix']
fileExtensions = args['extensions'].lstrip(".").split(".")
excludePaths = args['exclude']
backupFormat = args['backup']
# Remove trailing path separator from each exclude path
excludePaths[:] = [x.rstrip(os.sep) for x in excludePaths]
files = filelist(basePath, excludePaths, fileExtensions)
with open(outputFile, "w") as o:
# Read each file
for file in files:
file_canonical = file.replace(rootPath, rootPrefix, 1)
print("processing %s..." % (str(file_canonical),))
with open(file, 'r') as f:
lines = f.readlines()
data=''.join(lines)
soup = BeautifulSoup(data, features="html.parser")
for a in soup.find_all('a', href=True):
url = a['href']
try:
add_date = datetime.utcfromtimestamp(int(a['add_date']))
except:
add_date = datetime.now()
try:
last_modified = datetime.utcfromtimestamp(int(a['last_modified']))
except:
last_modified = add_date
if a.contents[0] is None:
title = url
else:
title = remove_line_breakers(a.contents[0]).strip()
if len(url) > 0:
if backupFormat:
o.write(title)
o.write(str(os.linesep))
o.write(add_date.strftime('%Y-%m-%d %H:%M:%S.%f'))
o.write(str(os.linesep))
o.write(last_modified.strftime('%Y-%m-%d %H:%M:%S.%f'))
o.write(str(os.linesep))
o.write(url)
o.write(str(os.linesep))
else:
o.write(url)
o.write(str(os.linesep))
if __name__ == "__main__":
main()