Skip to content

Instantly share code, notes, and snippets.

@cdpath
Last active November 28, 2017 03:39
Show Gist options
  • Select an option

  • Save cdpath/5bc2453a3dbaa521c3454a9372caf627 to your computer and use it in GitHub Desktop.

Select an option

Save cdpath/5bc2453a3dbaa521c3454a9372caf627 to your computer and use it in GitHub Desktop.
Marvin(iOS) annotations to My Clippings.txt(Kindle)
# -*- coding: utf-8 -*-
import csv
import locale
import datetime as dt
from collections import namedtuple
def clean_bom(lines):
encodings = ('utf-8-sig', 'gb2312', 'gbk', 'big5')
for l in lines:
for i, c in enumerate(encodings):
try:
s = l.decode(c)
break
except UnicodeDecodeError:
if i == (len(encodings) - 1):
raise
yield s.encode('utf-8')
def convert_to_chinese_time(t):
locale.setlocale(locale.LC_TIME, "zh_CN")
t = dt.datetime.strptime(t, "%Y-%m-%dT%H:%M:%SZ")
fmt = u"%Y年%m月%d日星期%a %p%I:%M:%S"
return t.strftime(fmt.encode('utf-8'))
def gen_clippings(row):
template = """{Title} ({Author})
- 您在第 {Page} 页(位置 #{Loc})的标注 | 添加于 {Time}
{HighlightText}
==========
"""
return template.format(
Title=row.Title,
Author=row.Author,
Page=0,
Loc='0-0',
Time=convert_to_chinese_time(row.Date),
HighlightText=row.HighlightText
)
def parse_csv(f_name):
with open(f_name) as f:
f_csv = csv.reader(clean_bom(f))
headers = next(f_csv)
Row = namedtuple('Row', headers)
for r in f_csv:
yield Row(*r)
def convert(marvin_f, kindle_f):
with open(kindle_f, 'w') as f:
f.writelines([gen_clippings(row) for row in parse_csv(marvin_f)])
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description="Convert Marvin CSV to My Clippings.txt")
parser.add_argument(
'-i', '--input', required=True, dest='input', action='store')
parser.add_argument(
'-o', '--output', required=True, dest='output', action='store')
args = parser.parse_args()
convert(args.input, args.output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment