kindle manager
This commit is contained in:
87
kman.py
87
kman.py
@@ -226,8 +226,8 @@ class kMan:
|
||||
|
||||
return ymd+tm
|
||||
|
||||
def format_data(self,bks, ft='MD'):
|
||||
""" format data for MD & CSV
|
||||
def format_note_data(self, bks, ft='MD'):
|
||||
""" format notes data for MD & CSV
|
||||
|
||||
Args:
|
||||
bks: books dict
|
||||
@@ -255,6 +255,39 @@ class kMan:
|
||||
|
||||
return hd+secs
|
||||
|
||||
def format_word_data(self, wdata, flist, ft='MD'):
|
||||
""" format words data for MD & CSV
|
||||
|
||||
Args:
|
||||
wdata: words data [bookinfo, words, lookups]
|
||||
flist: list to be formatted
|
||||
ft: can be 'MD'/'CSV'
|
||||
|
||||
Return:
|
||||
list [header, content]
|
||||
header and content are lists
|
||||
"""
|
||||
[bookinfo, words, lookups] = wdata
|
||||
hd =[] # header
|
||||
secs =[] # content
|
||||
DELIMITER = '|' if ft=='MD' else ','
|
||||
|
||||
hd.append(DELIMITER.join(['WORD','BOOKNAME','AUTHOR','CATEGORY','USAGE','TIMESTAMP']))
|
||||
if ft=='MD':
|
||||
hd.append(DELIMITER.join(['--' for i in range(6)]))
|
||||
|
||||
for frow in flist:
|
||||
[fword,fbookname,fauthor,fcategory]=frow
|
||||
for lrow in lookups:
|
||||
lbookname = bookinfo[lrow[2]]['bookname']
|
||||
lauthor = bookinfo[lrow[2]]['author']
|
||||
[lusage,ltimestamp] = lrow[4:6]
|
||||
if words[lrow[1]]['word']==fword:
|
||||
secs.append(DELIMITER.join(
|
||||
[fword,lbookname,lauthor,fcategory,lusage,ltimestamp]))
|
||||
|
||||
return hd+secs
|
||||
|
||||
def export_notes(self, bks, fnpref, ft='MD'):
|
||||
"""format output and write to file
|
||||
markdown format:
|
||||
@@ -282,12 +315,46 @@ class kMan:
|
||||
if ft=='JSON':
|
||||
fw.write(json.dumps(bks, indent=4, sort_keys=True, ensure_ascii=False))
|
||||
elif ft in ['MD','CSV']:
|
||||
for s in self.format_data(bks, ft):
|
||||
for s in self.format_note_data(bks, ft):
|
||||
fw.write(s)
|
||||
fw.write('\n')
|
||||
else:
|
||||
fw.write(json.dumps(bks)) # only for load back
|
||||
|
||||
# no good! books & words operation need to be abstracted
|
||||
def export_words(self, wdata, flist, fnpref, ft='MD'):
|
||||
"""format output and write to file
|
||||
markdown format:
|
||||
TYPE | bookname | author | marktime | content
|
||||
--|--|--|--|--
|
||||
xx|xx|xx|xx|xx
|
||||
|
||||
CSV format:
|
||||
TYPE,bookname,author,marktime,content
|
||||
xx,xx,xx,xx,xx
|
||||
|
||||
marktime: 20200403 PM 3:0:3 星期五
|
||||
|
||||
Args:
|
||||
bks: books dict
|
||||
f: can be 'MD'/'JSON'/'CSV'
|
||||
|
||||
Returns: special format of 'bks' dict
|
||||
"""
|
||||
|
||||
suff = {'MD':'.md','CSV':'.csv','JSON':'.json'}
|
||||
op = fnpref+suff[ft]
|
||||
|
||||
with open(op, 'w', encoding='utf8', errors='ignore') as fw:
|
||||
if ft=='JSON':
|
||||
fw.write(json.dumps(flist, indent=4, sort_keys=True, ensure_ascii=False))
|
||||
elif ft in ['MD','CSV']:
|
||||
for s in self.format_word_data(wdata, flist, ft):
|
||||
fw.write(s)
|
||||
fw.write('\n')
|
||||
else:
|
||||
fw.write(json.dumps(flist)) # only for load back
|
||||
|
||||
def drop_duplicate(self,bks):
|
||||
""" drop duplicated section
|
||||
|
||||
@@ -653,7 +720,7 @@ class kMan:
|
||||
lookups.append([row[0],row[1],row[2],row[4],row[5],mtime])
|
||||
|
||||
except Exception as e:
|
||||
print('sql failure {}'.format(e))
|
||||
print('SQL Failure: {}'.format(e))
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
@@ -671,8 +738,8 @@ class kMan:
|
||||
1: word item clicked
|
||||
Return: [filtted words_data, word_list]
|
||||
word_list:
|
||||
[[Bookname,Author,word,category],
|
||||
[Bookname,Author,word,status],
|
||||
[[Word,Bookname,Author,category],
|
||||
[word,Bookname,Author,category],
|
||||
....]
|
||||
"""
|
||||
[bookinfo, words, lookups] = wdata
|
||||
@@ -681,12 +748,12 @@ class kMan:
|
||||
if tp == 0:
|
||||
for row in lookups:
|
||||
word = words[row[1]]['word']
|
||||
category = words[row[1]]['category']
|
||||
category = 'learning' if words[row[1]]['category']==0 else 'mastered'
|
||||
bookname = bookinfo[row[2]]['bookname']
|
||||
author = bookinfo[row[2]]['author']
|
||||
if bookname==info or info == None:
|
||||
# maybe have duplication records
|
||||
word_list.append((bookname,author,word,category))
|
||||
word_list.append((word,bookname,author,category))
|
||||
# word tree clicked
|
||||
elif tp == 1:
|
||||
pass
|
||||
@@ -732,8 +799,8 @@ if __name__=='__main__':
|
||||
searchnote = km.search_clip(books, '巴曙松', 'ALL', 'AUTHOR')
|
||||
if searchnote[0] > 0: km.export_notes(searchnote[1], 'searchauthor', ft='MD')
|
||||
|
||||
print(km.get_bookname_num(books))
|
||||
print(km.get_author_num(books))
|
||||
#print(km.get_bookname_num(books))
|
||||
#print(km.get_author_num(books))
|
||||
|
||||
# add note content to hightlight, then delete note
|
||||
km.add_note_to_highlight(books)
|
||||
|
||||
Reference in New Issue
Block a user