kindle manager

This commit is contained in:
gavin
2020-06-12 23:33:35 +08:00
parent 43c602d8d6
commit 8dbd2357b2
17 changed files with 3570 additions and 103 deletions

141
kman.py
View File

@@ -19,7 +19,7 @@ from collections import defaultdict
# data structure - use dict
'''
books =
books_data =
{
"bookname_xxx": {
"author": "",
@@ -56,16 +56,6 @@ books =
}
'''
'''
# vocab.db -> table: book_info
bookname = {'id':{
'title':'bookname_xxx',
'authors':'xxx'
},}
words = {
"bookname_xxx": {
'''
# modi clippath for different os
SYS = 'WIN' if platform.system()=='Windows' else \
('LINUX' if platform.system()=='LINUX' else 'MAC')
@@ -73,14 +63,16 @@ SYS = 'WIN' if platform.system()=='Windows' else \
# some constants
LASTLINE = '=========='
NTPREF = '--CG注:'
KINDLEFN = 'My Clippings.txt'
CLIPFN = 'My Clippings.txt'
WORDFN = 'vocab.db'
CLIPPATH = './'
OUTPREF = './clip'
DEBUG = 1 # 0 - INFO; 1 - DEBUG
LOG2FILE = 1 # 0 - to stdio; 1 - to file
LOGFILE = 'log'
DELIMITER= '|'
BACKUPFN = './backup/bk.data'
BACKUPNOTEFN = './backup/bk.note.data'
BACKUPWORDFN = './backup/bk.word.data'
#HEADER = {0:'type',1:'bookname',2:'author',3:'position',4:'date',5:'content'}
# log info
@@ -137,7 +129,7 @@ class kMan:
kp = self.get_kindle_path()
if not kp:
s2 = u'Disconnected ({})'.format(CLIPPATH+KINDLEFN)
s2 = u'Disconnected ({})'.format(CLIPPATH+CLIPFN)
else:
with open(kp+'/system/version.txt' , 'r', encoding='utf8', errors='ignore') as f:
s2 = u'Connected ({}) version {}'.format(kp,f.read().strip())
@@ -385,7 +377,8 @@ class kMan:
0: root item clicked
1: bookname item clicked
2: author item clicked
Return: list:
Return: list: [filter books, filter seclist]
filter_seclist:
[[Type,Bookname,Author,Position,Date,content],
[Type,Bookname,Author,Position,Date,content]
....]
@@ -536,14 +529,14 @@ class kMan:
return False
def import_clips(self, fp=(CLIPPATH+KINDLEFN)):
def import_clips(self, fp=(CLIPPATH+CLIPFN)):
"""import clips from local file or kindle
4 lines for each section seperated with '======='
so read 4 lines before '======='
Args: fp - file path
Return: 0 - want to import kindle but kindle is not connected
books dict
books_data dict
"""
# check kindle by user just call get_kindle_path()
"""
@@ -608,6 +601,120 @@ class kMan:
return self.drop_duplicate(bks)
# words operations
def import_words(self, fp=(CLIPPATH+WORDFN)):
"""import words from local file or kindle
vocab.db -> tables: BOOK_INFO, LOOKUPS, WORDS
Args: fp - file path
Return: words_data = [bookinfo, words, lookups]
bookinfo = { 'book_key': {
'bookname':'bookname_xxx',
'author':'author_xxx'},}
words = { 'word_id': {
'word':'梅花鹿',
'category':0},
}
lookups = [['id','word_key','book_key','pos','usage','timestamp'],
['id','word_key','book_key','pos','usage','timestamp'],]
"""
import sqlite3
conn = sqlite3.connect(fp)
#[bookinfo, words, lookups] = [defaultdict(dict),defaultdict(dict),defaultdict(dict)]
[bookinfo, words, lookups] = [defaultdict(dict),defaultdict(dict),[]]
# select records from vocab.db -> tables: BOOK_INFO, LOOKUPS, WORDS
try:
cursor = conn.cursor()
cursor.execute('''select * from BOOK_INFO;''')
for row in cursor.fetchall():
bookinfo[row[0]]['bookname'] = row[4].replace(' ','')
bookinfo[row[0]]['author'] = row[5]
bookinfo[row[0]]['lang'] = row[5]
cursor.execute('''select * from WORDS;''')
for row in cursor.fetchall():
words[row[0]]['word'] = row[1]
#words[row[0]]['lang'] = row[3]
words[row[0]]['category'] = row[4]
cursor.execute('''select * from LOOKUPS;''')
for row in cursor.fetchall():
timearr = time.localtime(row[6]/1000.) # NOTE: length of timestring is 13
mtime = time.strftime("%Y/%m/%d %H:%M:%S", timearr)
"""
lookups[row[0]]['word_key'] = row[1]
lookups[row[0]]['book_key'] = row[2]
lookups[row[0]]['pos'] = row[4]
lookups[row[0]]['usage'] = row[5]
lookups[row[0]]['timestamp'] = mtime
"""
lookups.append([row[0],row[1],row[2],row[4],row[5],mtime])
except Exception as e:
print('sql failure {}'.format(e))
finally:
conn.close()
words_data = [bookinfo, words, lookups]
return words_data
def filter_words(self, wdata, info=None, tp=0):
""" filter words to show the clips in table view
Args:
wdata = [bookinfo, words, lookups]
info: filter by bookname or word information
tp: type to be filter
0: bookname item clicked
1: word item clicked
Return: [filtted words_data, word_list]
word_list:
[[Bookname,Author,word,category],
[Bookname,Author,word,status],
....]
"""
[bookinfo, words, lookups] = wdata
word_list = []
# bookname tree clicked
if tp == 0:
for row in lookups:
word = words[row[1]]['word']
category = words[row[1]]['category']
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 tree clicked
elif tp == 1:
pass
else:
return
# remove duplication
return list(set(word_list))
def get_lookups_word_num(self, wdata):
pass
def get_word_num(self, wdata):
pass
# show in tree view
def get_book_word_num(self,wdata):
[bookinfo, words, lookups] = wdata
bk_wd_num = defaultdict(dict)
nu = 0
for row in lookups:
nu += 1
word = words[row[1]]['word']
bookname = bookinfo[row[2]]['bookname']
bk_wd_num.setdefault(bookname,0)
bk_wd_num[bookname] += 1
return [nu, bk_wd_num]
if __name__=='__main__':
#books = defaultdict(dict)