kindle manager
This commit is contained in:
66
kman.py
66
kman.py
@@ -8,6 +8,7 @@
|
||||
#########################################################
|
||||
|
||||
import re
|
||||
import sys
|
||||
import os
|
||||
import io
|
||||
import json
|
||||
@@ -60,20 +61,32 @@ books_data =
|
||||
SYS = 'WIN' if platform.system()=='Windows' else \
|
||||
('LINUX' if platform.system()=='LINUX' else 'MAC')
|
||||
|
||||
frozen = 'not'
|
||||
#CURRPATH = os.path.dirname(os.path.realpath(sys.argv[0]))
|
||||
CURRPATH = ''
|
||||
if getattr(sys, 'frozen', False):
|
||||
# we are running in a bundle
|
||||
frozen = 'ever so'
|
||||
CURRPATH = sys._MEIPASS
|
||||
os.chdir(CURRPATH)
|
||||
else:
|
||||
# we are running in a normal Python environment
|
||||
CURRPATH = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
# some constants
|
||||
LASTLINE = '=========='
|
||||
NTPREF = '--CG注:'
|
||||
CLIPFN = 'My Clippings.txt'
|
||||
WORDFN = 'vocab.db'
|
||||
CLIPPATH = './'
|
||||
OUTPREF = './clip'
|
||||
OUTPREF = os.path.join(CURRPATH,'CLIP')
|
||||
DEBUG = 1 # 0 - INFO; 1 - DEBUG
|
||||
LOG2FILE = 1 # 0 - to stdio; 1 - to file
|
||||
LOGFILE = 'log'
|
||||
DELIMITER= '|'
|
||||
BACKUPNOTEFN = './backup/bk.note.data' # kindle notes
|
||||
BACKUPWORDFN = './backup/bk.word.data' # kindle words
|
||||
BACKUPINFOFN = './backup/bk.info.data' # book information from douban/amazon
|
||||
LOGFILE = os.path.join(CURRPATH,'log')
|
||||
BACKUPFOLDER = os.path.join(CURRPATH,'backup')
|
||||
BACKUPNOTEFN = os.path.join(BACKUPFOLDER,'bk.note.data') # kindle notes
|
||||
BACKUPWORDFN = os.path.join(BACKUPFOLDER,'bk.word.data') # kindle words
|
||||
BACKUPINFOFN = os.path.join(BACKUPFOLDER,'bk.info.data') # book information from douban/amazon
|
||||
#HEADER = {0:'type',1:'bookname',2:'author',3:'position',4:'date',5:'content'}
|
||||
|
||||
# log info
|
||||
@@ -119,6 +132,7 @@ r'''
|
||||
class kMan:
|
||||
|
||||
def __init__(self, parent=None):
|
||||
|
||||
self.hlnum = 0
|
||||
self.ntnum = 0
|
||||
self.refleshtime = '2020/10/10 10:00:00'
|
||||
@@ -130,9 +144,9 @@ class kMan:
|
||||
kp = self.get_kindle_path()
|
||||
|
||||
if not kp:
|
||||
s2 = u'Disconnected ({})'.format(CLIPPATH+CLIPFN)
|
||||
s2 = u'Disconnected ({})'.format(os.path.join(CURRPATH,CLIPFN))
|
||||
else:
|
||||
with open(kp+'/system/version.txt' , 'r', encoding='utf8', errors='ignore') as f:
|
||||
with open(os.path.join(kp,'system','version.txt'), 'r', encoding='utf8', errors='ignore') as f:
|
||||
s2 = u'Connected ({}) version {}'.format(kp,f.read().strip())
|
||||
|
||||
return [s1,s2]
|
||||
@@ -310,7 +324,7 @@ class kMan:
|
||||
"""
|
||||
|
||||
suff = {'MD':'.md','CSV':'.csv','JSON':'.json'}
|
||||
op = fnpref+suff[ft]
|
||||
op = os.path.join(CURRPATH, fnpref+suff[ft])
|
||||
|
||||
with open(op, 'w', encoding='utf8', errors='ignore') as fw:
|
||||
if ft=='JSON':
|
||||
@@ -344,7 +358,7 @@ class kMan:
|
||||
"""
|
||||
|
||||
suff = {'MD':'.md','CSV':'.csv','JSON':'.json'}
|
||||
op = fnpref+suff[ft]
|
||||
op = os.path.join(CURRPATH,fnpref+suff[ft])
|
||||
|
||||
with open(op, 'w', encoding='utf8', errors='ignore') as fw:
|
||||
if ft=='JSON':
|
||||
@@ -579,6 +593,10 @@ class kMan:
|
||||
#serr = str(stream_stderr.read())
|
||||
#if sout: print('stdout {}'.format(sout))
|
||||
#if serr: print('stderr {}'.format(serr))
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
#print('os.name {} sout {}'.format(os.name,sout))
|
||||
>>>>>>> 2d53124ab9aa26c9733646348174311d0967c8c0
|
||||
if os.name == 'nt': # windows
|
||||
for d in sout.split('\n'):
|
||||
if 'Kindle' in d:
|
||||
@@ -604,7 +622,7 @@ class kMan:
|
||||
|
||||
return False
|
||||
|
||||
def import_clips(self, fp=(CLIPPATH+CLIPFN)):
|
||||
def import_clips(self, fp=os.path.join(CURRPATH, CLIPFN)):
|
||||
"""import clips from local file or kindle
|
||||
4 lines for each section seperated with '======='
|
||||
so read 4 lines before '======='
|
||||
@@ -623,6 +641,8 @@ class kMan:
|
||||
path = fn
|
||||
"""
|
||||
|
||||
if not os.path.exists(fp): return {}
|
||||
|
||||
# loop to fill books dict
|
||||
with open(fp, 'r', encoding='utf8', errors='ignore') as f:
|
||||
bks = defaultdict(dict)
|
||||
@@ -677,7 +697,7 @@ class kMan:
|
||||
return self.drop_duplicate(bks)
|
||||
|
||||
# words operations
|
||||
def import_words(self, fp=(CLIPPATH+WORDFN)):
|
||||
def import_words(self, fp=(os.path.join(CURRPATH,WORDFN))):
|
||||
"""import words from local file or kindle
|
||||
vocab.db -> tables: BOOK_INFO, LOOKUPS, WORDS
|
||||
|
||||
@@ -791,7 +811,23 @@ class kMan:
|
||||
|
||||
return [nu, bk_wd_num]
|
||||
|
||||
class Util:
|
||||
def __init__(self, parent=None):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def get_app_path():
|
||||
"""Returns the base application path."""
|
||||
if hasattr(sys, 'frozen'):
|
||||
# Handles PyInstaller
|
||||
return os.path.dirname(sys.executable) # 使用pyinstaller打包后的exe目录
|
||||
return os.path.dirname(__file__) # 没打包前的py目录
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
|
||||
"""
|
||||
# XXX move to unitest.kman.py
|
||||
#books = defaultdict(dict)
|
||||
km = kMan()
|
||||
books = km.import_clips()
|
||||
@@ -814,12 +850,14 @@ if __name__=='__main__':
|
||||
km.add_note_to_highlight(books)
|
||||
|
||||
# test dict json convert
|
||||
with open('./xx', 'w', encoding='utf8', errors='ignore') as fw:
|
||||
with open(os.path.join(CURRPATH,'xx'), 'w', encoding='utf8', errors='ignore') as fw:
|
||||
fw.write(km.dict2json(books))
|
||||
if km.json2dict('./xx')==books: print( 'test OK')
|
||||
if km.json2dict(os.path.join(CURRPATH,'xx'))==books: print( 'test OK')
|
||||
|
||||
km.export_notes(books, OUTPREF, ft='MD')
|
||||
|
||||
# print data with json format
|
||||
logger.debug(json.dumps(books, indent=4, sort_keys=True, ensure_ascii=False))
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user