Created
May 8, 2017 06:37
-
-
Save gujiaxi/590136556c86b25f6e914c7717ac4f69 to your computer and use it in GitHub Desktop.
Convert instapaper html to bookmarks.html
This file contains hidden or 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/env python3 | |
# -*- coding: utf-8 -*- | |
import os | |
import sys | |
import csv | |
import itertools | |
from operator import itemgetter | |
if len(sys.argv) < 2: | |
print("{} <instappaer.html> <bmfile.html>".format(sys.argv[0])) | |
exit(0) | |
# process instapaper | |
bm_list = [] | |
infile = os.path.expanduser(sys.argv[1]) | |
with open(infile) as f: | |
f_csv = csv.reader(f) | |
headers = next(f_csv) | |
for row in f_csv: | |
bm = {} | |
bm['url'] = row[0] | |
bm['title'] = row[1] | |
bm['folder'] = row[3] | |
bm_list.append(bm) | |
# classify bookmarks | |
bms = sorted(bm_list, key=itemgetter('folder')) | |
# write to bm_html | |
outfile = os.path.expanduser(sys.argv[2]) | |
with open(outfile, 'w', encoding='utf-8') as f: | |
f.write('<!DOCTYPE NETSCAPE-Bookmark-file-1>\n') | |
f.write('<HTML>\n') | |
f.write('<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">\n') | |
f.write('<Title>Bookmarks</Title>\n') | |
f.write('<H1>Bookmarks</H1>\n') | |
for key, value in itertools.groupby(bms, key=itemgetter('folder')): | |
f.write('<DT><H3 FOLDED>{}</H3>\n'.format(key)) | |
f.write('<DL><p>\n') | |
for i in value: | |
f.write('<DT><A HREF="{}">{}</A>\n'.format(i.get('url'), i.get('title'))) | |
f.write('</DL><p>\n') | |
f.write('</HTML>\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment