monchovarela
7/16/2018 - 7:41 PM

Generate html file of Chrome bookmarks (only test on windows)

Generate html file of Chrome bookmarks (only test on windows)

import os
import json
import time

os.system('cls')

# get user
user = os.getlogin()
# route of bookmarks
fileurl = 'C:\\Users\\{}\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Bookmarks'.format(user)

# http://patorjk.com/software/taag/#p=display&h=3&v=2&f=ANSI%20Shadow&t=Hello
print('''

████████╗  ██╗██████╗███╗   ██████████╗                             
██╔════██║  ████╔═══██████╗ ██████╔════╝                             
██║    █████████║   ████╔████╔███████╗                               
██║    ██╔══████║   ████║╚██╔╝████╔══╝                               
╚████████║  ██╚██████╔██║ ╚═╝ █████████╗                             
 ╚═════╚═╝  ╚═╝╚═════╝╚═╝     ╚═╚══════╝                             
██████╗ ██████╗ ██████╗██╗  █████╗   ███╗█████╗██╗  ████████╗███████╗
██╔══████╔═══████╔═══████║ ██╔████╗ ██████╔══████║ ██╔██╔══████╔════╝
██████╔██║   ████║   ███████╔╝██╔████╔██████████████╔╝██████╔███████╗
██╔══████║   ████║   ████╔═██╗██║╚██╔╝████╔══████╔═██╗██╔══██╚════██║
██████╔╚██████╔╚██████╔██║  ████║ ╚═╝ ████║  ████║  ████║  █████████║
╚═════╝ ╚═════╝ ╚═════╝╚═╝  ╚═╚═╝     ╚═╚═╝  ╚═╚═╝  ╚═╚═╝  ╚═╚══════╝
 ██████╗██████████╗   ███████████████╗ █████╗████████╗██████╗██████╗ 
██╔════╝██╔════████╗  ████╔════██╔══████╔══██╚══██╔══██╔═══████╔══██╗
██║  ████████╗ ██╔██╗ ███████╗ ██████╔███████║  ██║  ██║   ████████╔╝
██║   ████╔══╝ ██║╚██╗████╔══╝ ██╔══████╔══██║  ██║  ██║   ████╔══██╗
╚██████╔█████████║ ╚█████████████║  ████║  ██║  ██║  ╚██████╔██║  ██║
 ╚═════╝╚══════╚═╝  ╚═══╚══════╚═╝  ╚═╚═╝  ╚═╝  ╚═╝   ╚═════╝╚═╝  ╚═╝
''')

time.sleep(2)
os.system('cls')

# ask the name of html file
while True:
	try: 
		name_of_file = str(input('Write the name of html file: '))
		if name_of_file != ' ': break
	except: 
		print('Please write a name')

# ask the name of browser
while True:
	try: 
		name_of_browser = int(input('Open on browser 1:firefox 2:Chrome: '))
		if name_of_browser > 0 and name_of_browser < 3: break
	except: 
		print('Please choose a number 1 or 2')


# convert variable 
if name_of_browser == 1: 
	name_of_browser = 'firefox'
elif name_of_browser == 2: 
	name_of_browser = 'chrome'

name_json_file = '{}.json'.format(name_of_file)



'''
	Loop children of bookmarks
	return key value
'''
def tree_json(elem='children'):
	for i in elem:
		if i['type'] == 'url':
			if i['name']: yield i['name'],i['url']
			else: yield i['url'],i['url']
		elif i['type'] == 'folder':
			for _i in i['children']:
				if _i['type'] == 'url':
					if _i['name']: yield _i['name'],_i['url']
					else: yield _i['url'],_i['url']
'''
	Read json file and convert to html file
'''
def read_json():
	# open bookmarks.json 
	with open(name_json_file,'r') as f: 
		data = json.load(f)

	# init html doctype
	html = '''
		<Doctype html>
		<html lang="es">
			<head>
				<meta charset="UTF-8">
				<title>Chrome Bookmarks</title>
				<style rel="stylesheet">
					body {
					  font-size:16px;
					  font-family: monospace;
					  font-weight: normal;
					  background: black;
					  color: aqua;
					}
					a {
					  color: aquamarine;
					}
				</style>
			</head>
			<body>
				<h2>My Chrome Bookmarks </h2>
				<ol>
	'''

	# loop children and generate ordened list
	for k in tree_json(data['roots']['bookmark_bar']['children']):
		html +='''<li><a target="_blank" title="{}" href="{}">{}</a></li>'''.format(k[0], k[1], k[0])
		pass


	# end of html
	html += '''
				</ol>
			</body>
		</html>
	'''


	# save to htm file
	save_to_html = open('{}.html'.format(name_of_file),'w')
	save_to_html.write(html)
	save_to_html.close()
	print('File {}.html saved!'.format(name_of_file))


# Read bookmarks of chrome
def read_file():
	# Read file 
	with open(fileurl,'r',encoding='ascii', errors='replace') as f:
		out = f.read()
		f.close()
	# save file 
	with open(name_json_file,'w',encoding='ascii', errors='replace') as s:
		s.write(out)
		s.close()


# check if exists
if os.path.isfile(name_json_file):
	# read json 
	read_json()
	# remove json file i not need this now
	os.system('del {}.json'.format(name_of_file))
	print('The html file is generated!')
	time.sleep(1)
	print('Open on browser asuming you have {} ?'.format(name_of_browser))
	time.sleep(1)
	os.system('start {} file:///{}/{}.html'.format(name_of_browser, os.getcwd(), name_of_file))
else:
	# read file and generate json
	read_file()
	# now read json
	read_json()
	# remove json file i not need this now
	os.system('del {}.json'.format(name_of_file))
	print('The html file is generated!')
	time.sleep(1)
	print('Open on browser asuming you have {} ?'.format(name_of_browser))
	time.sleep(1)
	os.system('start {} file:///{}/{}.html'.format(name_of_browser, os.getcwd(), name_of_file))