-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathKOReaderJsonALLtoEachBook.py
65 lines (59 loc) · 1.9 KB
/
KOReaderJsonALLtoEachBook.py
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
# encoding:utf-8
# Highlights format support in 20210904
# tested koreader android 2021.09
# Programmed by Andy
# v0.4
from pathlib import Path
import sys
import os
import json
import time
import datetime
def readfile(filename):
# readfile
with open(filename, mode='r', encoding='UTF-8') as file:
filereadlines = file.readlines()
# remove blank lines
for i in filereadlines:
if i == '\n':
filereadlines.remove(i)
# remove '\n' in line end
for i in range(len(filereadlines)):
filereadlines[i] = filereadlines[i].rstrip()
return filereadlines
def writefile(filename,filereadlines):
# write file
newfile = open(filename +'.md', mode='w', encoding='UTF-8')
newfile.writelines(filereadlines)
newfile.close()
def converterFromKOReaderJson(filename):
# read json file
allBooks = readfile(filename)
for eachBook in allBooks:
outputfile = []
jsonData = json.loads(eachBook)
outputfile.append('# ' + jsonData['title'] + '\n\n---\n\n')
for key in jsonData:
try:
outputfile.append('> ' + jsonData[key][0]['text'] + '\n\n')
outputfile.append('*' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(jsonData[key][0]['time'])) + '*\n\n---\n\n')
except TypeError:
continue
except KeyError:
continue
writefile(jsonData['title'],outputfile)
def main(inputPath):
del inputPath[0]
for aPath in inputPath:
if Path.is_dir(Path(aPath)):
for file in Path(aPath).glob('*.json'):
converterFromKOReaderJson(file)
if Path.is_file(Path(aPath)):
if (Path(aPath).suffix == '.json'):
converterFromKOReaderJson(aPath)
if __name__ == '__main__':
try:
if len(sys.argv) >= 2:
main(sys.argv)
except IndexError:
pass