forked from cdhigh/KindleEar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
283 lines (252 loc) · 9.97 KB
/
helper.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python
# -*- coding:utf-8 -*-
""" uploader helper for KindleEar <https://github.com/cdhigh/KindleEar>
It will modify AppId and some other items for you automatically.
Configure file 'custom.txt' format (encoding of the file must be ascii):
application: YourAppId
email: YourEmail
timezone: 8
If it not exist, this script will create it in same directory of __file__.
"""
import os, sys, re, codecs, locale, shutil
__Author__ = 'cdhigh <https://github.com/cdhigh>'
__Version__ = '1.4'
__Date__ = '2017-09-03'
CUSTOM_FILE = 'custom.txt'
KE_DIR = 'KindleEar'
KE_MASTER_DIR = 'KindleEar-master'
PAT_APP = r"^application:\s*([\w]+)"
PAT_EMAIL = r"^SRC_EMAIL\s*=\s*[\"\']([\w@\.-]+)[\"\'](.*)"
PAT_DOMAIN = r"^DOMAIN\s*=\s*[\"\']([\w:/\.-]+)[\"\'](.*)"
PAT_TZ = r"^TIMEZONE\s*=\s*?(-{0,1}\d+)(.*)"
try:
input = raw_input
except NameError:
pass
#(re)move chinese books to a subdirectory (donot display in webpage)
def RemoveChineseBooks(ke_dir):
lang = 'zh_CN'
cn_books = [] #Relative path saved
loc = locale.getdefaultlocale()
if loc and len(loc) > 1:
lang = loc[0]
if lang.startswith('zh'):
return
#create list of books which language is Chinese
books_dir = os.path.join(ke_dir, 'books')
if not os.path.exists(books_dir):
return
list_book_dirs = os.walk(books_dir)
for root, dirs, files in list_book_dirs:
for f in files:
if not f.endswith('.py') or f.startswith('__') or f.endswith('base.py'):
continue
bkfile = os.path.join(root, f)
rel_path_bkfile = bkfile.replace(books_dir, '').lstrip('/').lstrip('\\') #Relative path
all_lines = []
try:
with codecs.open(bkfile, 'r', 'utf-8') as f:
all_lines = f.read().split('\n')
except:
continue
if not all_lines:
continue
iscnbook = False
for line in all_lines:
ln = line.replace(' ', '').replace('\t', '')
if ln.startswith(('title=', 'description=')): #title line
for ch in ln:
if u'\u4e00' <= ch <= u'\u9fff': #Chinese Chars
iscnbook = True
break
#if not iscnbook:
# break #next book
if iscnbook: #Is Chinese Book
cn_books.append(rel_path_bkfile)
#*.pyc exists?
if rel_path_bkfile.endswith('.py'):
pycfile = rel_path_bkfile + 'c'
if os.path.exists(pycfile):
cn_books.append(pycfile)
if not cn_books:
return
#if exist some Chinese books, then ask for move or not
ret = input('Do you want to remove Chinese books? (y/n)')
if ret not in ('Y', 'YES', 'y', 'yes'):
return
#check and create subdirectory
bakdir = os.path.join(books_dir, 'ChineseBooksBak')
if not os.path.exists(bakdir):
os.makedirs(bakdir)
for book in cn_books:
dst = os.path.join(bakdir, book)
dst_dir = os.path.dirname(dst) #create dst directory
if not os.path.exists(dst_dir):
try:
os.makedirs(dst_dir)
except:
pass
if os.path.exists(dst): #dst exist, try to remove it firstly.
try:
os.remove(dst)
except:
pass
#remove book to bak directory
try:
shutil.move(os.path.join(books_dir, book), dst)
except:
try:
os.remove(os.path.join(books_dir, book))
except:
pass
#Delete __init__.py of directory backup
list_bak_dir = os.walk(bakdir)
for root, dirs, files in list_bak_dir:
for f in files:
if f == '__init__.py' or f == '__init__.pyc':
#try:
os.remove(os.path.join(root, f))
#except:
# pass
def Main():
#Searching for KindleEar folder
ke_dir = os.path.join(os.path.dirname(__file__), KE_DIR)
kem_dir = os.path.join(os.path.dirname(__file__), KE_MASTER_DIR)
kemm_dir = os.path.join(kem_dir, KE_MASTER_DIR)
keup_dir = os.path.join(os.path.dirname(__file__), '..', KE_DIR)
dirs = list(filter(os.path.exists, (ke_dir, kemm_dir, kem_dir, keup_dir)))
if not dirs:
print("Cant found folder 'KindleEar'! Please download it from github firstly.")
return 1
ke_dir = dirs[0]
custom_file = os.path.join(os.path.dirname(__file__), CUSTOM_FILE) #file for saving your custom info
app_yaml = os.path.join(ke_dir, 'app.yaml')
work_yaml = os.path.join(ke_dir, 'module-worker.yaml')
cfg_file = os.path.join(ke_dir, 'config.py')
slapp = [] #string buffer for app.yaml
if os.path.exists(app_yaml):
with open(app_yaml, 'r') as fapp:
slapp = fapp.read().split('\n')
if not slapp:
print("Not exist 'app.yaml' or it's invalid, please download KindleEar again.")
return 1
slcfg = [] #string buffer for config.py
if os.path.exists(cfg_file):
with codecs.open(cfg_file, 'r', 'utf-8') as fcfg:
slcfg = fcfg.read().split('\n')
if not slcfg:
print("Not exist 'config.py' or it's invalid, please download KindleEar again.")
return 1
slwork = [] #string buffer for module-worker.yaml
if os.path.exists(work_yaml):
with open(work_yaml, 'r') as fwork:
slwork = fwork.read().split('\n')
#init some parameter
app = email = timezone = ''
mt1 = re.match(PAT_APP, slapp[0])
mt2 = re.match(PAT_APP, slwork[0]) if slwork else mt1
if mt1 and mt2 and mt1.group(1) == mt2.group(1):
app = mt1.group(1)
for index, line in enumerate(slcfg):
mt = re.match(PAT_EMAIL, line)
if mt:
email = mt.group(1)
continue
mt = re.match(PAT_DOMAIN, line)
if mt:
domain = mt.group(1)
continue
mt = re.match(PAT_TZ, line)
if mt:
timezone = mt.group(1)
continue
slcustom = []
needinput = True
if os.path.exists(custom_file):
with open(custom_file, 'r') as fcustom:
slcustom = fcustom.read().split('\n')
for line in slcustom:
if line.lower().startswith('application:'):
app = line[len('application:'):].strip()
elif line.lower().startswith('email:'):
email = line[len('email:'):].strip()
elif line.lower().startswith('timezone:'):
timezone = line[len('timezone:'):].strip()
ret = input('Your custom info :\n\t app id : %s\n\t email : %s\n\ttimezone : %s\nCorrect? (y/n) : '%(app,email,timezone))
if ret in ('y', 'yes', 'Y', 'YES'):
needinput = False #configure items correct!
while 1:
if needinput or not all((app, email, timezone)):
new_app = input('Input app id (%s): ' % app)
new_email = input('Input your gmail (%s): ' % email)
new_timezone = input('Input your timezone (%s): ' % timezone)
app = new_app if new_app else app
email = new_email if new_email else email
timezone = new_timezone if new_timezone else timezone
with open(custom_file, 'w') as fcustom:
fcustom.write('application: %s\n' % app)
fcustom.write('email: %s\n' % email)
fcustom.write('timezone: %s' % timezone)
if all((app, email, timezone)):
break
elif not app:
print('app id is empty, please input it again.')
elif not email:
print('email is empty, please input it again.')
elif not timezone:
print('timezone is empty, please input it again.')
#Check and modify app.yaml
mt = re.match(PAT_APP, slapp[0])
if mt:
if mt.group(1) != app:
slapp[0] = 'application: ' + app
with open(app_yaml, 'w') as fapp:
fapp.write('\n'.join(slapp))
else:
print('app.yaml seems invalid, please download KindleEar again.')
return 1
#Check and modify module-work.yaml
if slwork:
mt = re.match(PAT_APP, slwork[0])
if mt:
if mt.group(1) != app:
slwork[0] = 'application: ' + app
with open(work_yaml, 'w') as fwork:
fwork.write('\n'.join(slwork))
else:
print('module-work.yaml seems invalid, please download KindleEar again.')
return 1
#Check and modify config.py
cfg_changed = False
for index, line in enumerate(slcfg):
mt = re.match(PAT_EMAIL, line)
if mt:
if mt.group(1) != email:
slcfg[index] = 'SRC_EMAIL = "%s"' % email + mt.group(2)
cfg_changed = True
continue
mt = re.match(PAT_DOMAIN, line)
if mt:
domain = mt.group(1)
if domain.endswith('appspot.com') and domain not in (
'http://%s.appspot.com' % app, 'https://%s.appspot.com' % app):
slcfg[index] = 'DOMAIN = "https://%s.appspot.com"' % app + mt.group(2)
cfg_changed = True
continue
mt = re.match(PAT_TZ, line)
if mt:
if mt.group(1) != timezone:
slcfg[index] = 'TIMEZONE = %s'%timezone + mt.group(2)
cfg_changed = True
continue
if cfg_changed:
with codecs.open(cfg_file, 'w', 'utf-8') as fcfg:
fcfg.write('\n'.join(slcfg))
RemoveChineseBooks(ke_dir)
return 0
if __name__ == '__main__':
print('\nKindleEar uploader v%s (%s) by %s\n' % (__Version__, __Date__, __Author__))
ret = Main()
if ret:
import sys
sys.exit(ret)