Instantly share code, notes, and snippets.
Created
March 30, 2017 14:43
-
Star
(1)
1
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save journey-ad/cf111da015409e578727cc71946caa11 to your computer and use it in GitHub Desktop.
转换你的 chrome 书签为 HTML 格式并分享给我
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# share-your-bookmarks-with-me | |
# | |
# 转换你的 chrome 书签为 HTML 格式并分享给我 | |
# | |
# Copyright (c) 2017 journey.ad | |
# This program is released under the ISC license. | |
from __future__ import print_function | |
import argparse | |
from json import loads | |
from os import environ | |
from os.path import expanduser | |
from platform import system | |
from re import match | |
from sys import argv, stderr | |
import socket | |
import requests | |
script_version = "1.0" | |
# 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>{bookmark_bar}</dl> | |
<dl>{other}</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 | |
def html_for_node(node): | |
if 'url' in node: | |
return html_for_url_node(node) | |
elif 'children' in node: | |
return html_for_parent_node(node) | |
else: | |
return '' | |
def html_for_url_node(node): | |
if not match("javascript:", node['url']): | |
return '<dt><a href="{}">{}</a>\n'.format(sanitize(node['url']), sanitize(node['name'])) | |
else: | |
return '' | |
def html_for_parent_node(node): | |
return '<dt><h3>{}</h3>\n<dl><p>{}</dl><p>\n'.format(sanitize(node['name']), | |
''.join([html_for_node(n) for n in node['children']])) | |
# Parse the command-line arguments | |
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, | |
description="转换你的 chrome 书签为 HTML 格式并分享给我", | |
epilog="(c) 2017 journey.ad\nBase on https://github.com/bdesham/py-chrome-bookmarks") | |
parser.add_argument("input_filename", type=str, nargs="?", | |
help="Chrome 的书签文件路径,如果省略了该参数,脚本将在 Chrome 的默认用户目录内查找") | |
parser.add_argument("-v", "--version", action="version", | |
version="share-your-bookmarks-with-me {}".format(script_version)) | |
args = parser.parse_args() | |
# Determine where the input file is | |
if args.input_filename: | |
input_filename = args.input_filename | |
else: | |
if system() == "Darwin": | |
input_filename = expanduser("~/Library/Application Support/Google/Chrome/Default/Bookmarks") | |
elif system() == "Linux": | |
input_filename = expanduser("~/.config/google-chrome/Default/Bookmarks") | |
elif system() == "Windows": | |
input_filename = environ["LOCALAPPDATA"] + r"\Google\Chrome\User Data\Default\Bookmarks" | |
else: | |
print('无法识别你所用的操作系统 ("{}") ,请手动指定书签文件路径'.format(system())) | |
exit(1) | |
try: | |
input_file = open(input_filename, 'r', encoding='utf-8') | |
except IOError as e: | |
if e.errno == 2: | |
print("默认目录 ({}) ".format(e.filename) + | |
"中未找到书签文件,请手动指定") | |
exit(1) | |
# Read, convert, and write the bookmarks | |
flag = input('确定要将你的 chrome 书签分享给我吗?为了方便统计,当前计算机名也将一并被上传(yes/no)') | |
if flag.lower()=='yes': | |
contents = loads(input_file.read()) | |
input_file.close() | |
bookmark_bar = html_for_node(contents['roots']['bookmark_bar']) | |
other = html_for_node(contents['roots']['other']) | |
filename = 'bookmarks_' + socket.gethostname() | |
try: | |
print('正在上传…') | |
r = requests.post ('https://api.imjad.cn/interface/upload/upload.php', files={'file': (filename, output_file_template.format(bookmark_bar=bookmark_bar, other=other), 'text/html')}) | |
if r.status_code == 200: | |
res = loads(r.content) | |
if res['code'] == 0: | |
print('感谢!一点小礼物:\r\n', res['meta'], '\r\n', res['uri'], '\r\n最后祝你身体健康,再见') | |
elif res['code'] == -1: | |
print('你已经上传过啦!') | |
except Exception as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment