-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathexport-chrome-history
executable file
·136 lines (107 loc) · 3.98 KB
/
export-chrome-history
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
#!/usr/bin/env python
# export-chrome-history
#
# A script to convert Google Chrome's history file to the standard HTML-ish
# bookmarks file format.
#
# Copyright (c) 2011, 2017-2018 Benjamin D. Esham. This program is released under the
# ISC license, which you can find in the file LICENSE.md.
from __future__ import print_function
import argparse
from os import environ
from os.path import expanduser, join
from platform import system
from shutil import copy, rmtree
import sqlite3
from sys import argv, stderr
from tempfile import mkdtemp
script_version = "2.0.2"
# html escaping code from http://wiki.python.org/moin/EscapingHtml
html_escape_table = {
"&": "&",
'"': """,
"'": "'",
">": ">",
"<": "<",
}
output_file_template = """<!DOCTYPE NETSCAPE-Bookmark-file-1>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
<title>Bookmarks</title>
<h1>Bookmarks</h1>
<dl><p>
<dl><dt><h3>History</h3>
<dl><p>{items}</dl></p>\n</dl>"""
def html_escape(text):
return ''.join(html_escape_table.get(c,c) for c in text)
def sanitize(string):
res = ''
string = html_escape(string)
for i in range(len(string)):
if ord(string[i]) > 127:
res += '&#x{:x};'.format(ord(string[i]))
else:
res += string[i]
return res
# Parse the command-line arguments
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
description="Convert Google Chrome's history file to the standard HTML-based format.",
epilog="(c) 2011, 2017-2018 Benjamin D. Esham\nhttps://github.com/bdesham/chrome-export")
parser.add_argument("input_file", nargs="?",
help="The location of the Chrome history file to read. If this is omitted then the script will look for the file in Chrome's default location.")
parser.add_argument("output_file", type=argparse.FileType('w'),
help="The location where the HTML bookmarks file will be written.")
parser.add_argument("-v", "--version", action="version",
version="export-chrome-history {}".format(script_version))
args = parser.parse_args()
# Determine where the input file is
if args.input_file:
input_filename = args.input_file
else:
if system() == "Darwin":
input_filename = expanduser("~/Library/Application Support/Google/Chrome/Default/History")
elif system() == "Linux":
input_filename = expanduser("~/.config/google-chrome/Default/History")
elif system() == "Windows":
input_filename = environ["LOCALAPPDATA"] + r"\Google\Chrome\User Data\Default\History"
else:
print('Your system ("{}") is not recognized. Please specify the input file manually.'.format(system()))
exit(1)
try:
input_file = open(input_filename, 'r')
except IOError as e:
if e.errno == 2:
print("The history file could not be found in its default location ({}). ".format(e.filename) +
"Please specify the input file manually.")
exit(1)
else:
input_file.close()
# Make a copy of the database, open it, process its contents, and write the
# output file
temp_dir = mkdtemp(prefix='export-chrome-history-')
copied_file = join(temp_dir, 'History')
copy(input_filename, copied_file)
try:
connection = sqlite3.connect(copied_file)
except sqlite3.OperationalError:
# This error message is a *little* misleading, since the problem actually
# occurred when we tried to open copied_file, but chances are that if we got
# to this point then the problem lies with the file itself (e.g. it isn't a
# valid SQLite database), not with our ability to read the file.
print('The file "{}" could not be opened for reading.'.format(input_filename))
rmtree(temp_dir)
exit(1)
curs = connection.cursor()
try:
curs.execute("SELECT url, title FROM urls")
except sqlite3.OperationalError:
print('There was an error reading data from the file "{}".'.format(args.input_file))
rmtree(temp_dir)
exit(1)
items = ""
for row in curs:
if len(row[1]) > 0:
items += '<dt><a href="{}">{}</a>\n'.format(sanitize(row[0]), sanitize(row[1]))
connection.close()
rmtree(temp_dir)
args.output_file.write(output_file_template.format(items=items))
args.output_file.close()