├── helper.py ├── img ├── 1.png ├── 2.png ├── 3.png └── 4.png ├── readme.md ├── update.sh └── upload.sh /helper.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding:utf-8 -*- 3 | """ uploader helper for KindleEar 4 | It will modify AppId and some other items for you automatically. 5 | Configure file 'custom.txt' format (encoding of the file must be ascii): 6 | application: YourAppId 7 | email: YourEmail 8 | timezone: 8 9 | If it not exist, this script will create it in same directory of __file__. 10 | """ 11 | import os, re, codecs, locale 12 | __Author__ = 'cdhigh' 13 | __Version__ = '1.3.1' 14 | __Date__ = '2015-08-20' 15 | 16 | CUSTOM_FILE = 'custom.txt' 17 | KE_DIR = 'KindleEar' 18 | KE_MASTER_DIR = 'KindleEar-master' 19 | PAT_APP = r"^application:\s*([\w\-]+)" 20 | PAT_EMAIL = r"^SRC_EMAIL\s*=\s*[\"\']([\w@\.\-]+)[\"\'](.*)" 21 | PAT_DOMAIN = r"^DOMAIN\s*=\s*[\"\']([\w:/\.\-]+)[\"\'](.*)" 22 | PAT_TZ = r"^TIMEZONE\s*=\s*?(-{0,1}\d+)(.*)" 23 | 24 | 25 | #(re)move chinese books to a subdirectory (donot display in webpage) 26 | def RemoveChineseBooks(ke_dir): 27 | lang = 'zh_CN' 28 | cn_books = [] 29 | loc = locale.getdefaultlocale() 30 | if loc and len(loc) > 1: 31 | lang = loc[0] 32 | if lang.startswith('zh'): 33 | return 34 | 35 | #create list of books which language is Chinese 36 | books_dir = os.path.join(ke_dir, 'books') 37 | if not os.path.exists(books_dir): 38 | return 39 | for bkfile in os.listdir(books_dir): 40 | if bkfile.endswith('.py') and not bkfile.startswith('__') and not bkfile.endswith("base.py"): 41 | slbk = [] 42 | try: 43 | with codecs.open(os.path.join(books_dir, bkfile), 'r', 'utf-8') as f: 44 | slbk = f.read().split('\n') 45 | except: 46 | continue 47 | 48 | if not slbk: 49 | continue 50 | 51 | iscnbook = False 52 | for line in slbk: 53 | ln = line.replace(' ', '').replace('\t', '') 54 | if ln.startswith('title='): #title line 55 | for ch in ln: 56 | if u'\u4e00' <= ch <= u'\u9fff': #Chinese Chars 57 | iscnbook = True 58 | break 59 | if not iscnbook: 60 | break #next book 61 | 62 | if iscnbook: #Is Chinese Book 63 | cn_books.append(os.path.join(books_dir, bkfile)) 64 | #*.pyc exists? 65 | bookname = os.path.splitext(bkfile)[0] 66 | pycfile = os.path.join(books_dir, bookname + '.pyc') 67 | if os.path.exists(pycfile): 68 | cn_books.append(pycfile) 69 | break #next book 70 | 71 | if not cn_books: 72 | return 73 | 74 | #if exist some Chinese books, then ask for move or not 75 | ret = raw_input('Do you want to remove Chinese books? (y/n)') 76 | if ret not in ('Y', 'YES', 'y', 'yes'): 77 | return 78 | 79 | #check and create subdirectory 80 | bakdir = os.path.join(books_dir, 'ChineseBooksBak') 81 | if not os.path.exists(bakdir): 82 | os.makedirs(bakdir) 83 | 84 | for book in cn_books: 85 | dst = os.path.join(bakdir, os.path.basename(book)) 86 | if os.path.exists(dst): #dst exist, try to remove it firstly. 87 | try: 88 | os.remove(dst) 89 | except: 90 | pass 91 | 92 | #remove book to bak directory 93 | try: 94 | os.rename(book, dst) 95 | except: 96 | try: 97 | os.remove(book) 98 | except: 99 | pass 100 | 101 | def Main(): 102 | #Searching for KindleEar folder 103 | ke_dir = os.path.join(os.path.dirname(__file__), KE_DIR) 104 | kem_dir = os.path.join(os.path.dirname(__file__), KE_MASTER_DIR) 105 | kemm_dir = os.path.join(kem_dir, KE_MASTER_DIR) 106 | dirs = filter(os.path.exists, (ke_dir, kemm_dir, kem_dir)) 107 | if not dirs: 108 | print("Cant found folder 'KindleEar'! Please download it from github firstly.") 109 | return 1 110 | 111 | ke_dir = dirs[0] 112 | custom_file = os.path.join(os.path.dirname(__file__), CUSTOM_FILE) #file for saving your custom info 113 | app_yaml = os.path.join(ke_dir, 'app.yaml') 114 | work_yaml = os.path.join(ke_dir, 'module-worker.yaml') 115 | cfg_file = os.path.join(ke_dir, 'config.py') 116 | 117 | slapp = [] #string buffer for app.yaml 118 | if os.path.exists(app_yaml): 119 | with open(app_yaml, 'r') as fapp: 120 | slapp = fapp.read().split('\n') 121 | if not slapp: 122 | print("Not exist 'app.yaml' or it's invalid, please download KindleEar again.") 123 | return 1 124 | 125 | slcfg = [] #string buffer for config.py 126 | if os.path.exists(cfg_file): 127 | with codecs.open(cfg_file, 'r', 'utf-8') as fcfg: 128 | slcfg = fcfg.read().split('\n') 129 | if not slcfg: 130 | print("Not exist 'config.py' or it's invalid, please download KindleEar again.") 131 | return 1 132 | 133 | slwork = [] #string buffer for module-worker.yaml 134 | if os.path.exists(work_yaml): 135 | with open(work_yaml, 'r') as fwork: 136 | slwork = fwork.read().split('\n') 137 | 138 | #init some parameter 139 | app = email = timezone = '' 140 | mt1 = re.match(PAT_APP, slapp[0]) 141 | mt2 = re.match(PAT_APP, slwork[0]) if slwork else mt1 142 | if mt1 and mt2 and mt1.group(1) == mt2.group(1): 143 | app = mt1.group(1) 144 | for index, line in enumerate(slcfg): 145 | mt = re.match(PAT_EMAIL, line) 146 | if mt: 147 | email = mt.group(1) 148 | continue 149 | mt = re.match(PAT_DOMAIN, line) 150 | if mt: 151 | domain = mt.group(1) 152 | continue 153 | mt = re.match(PAT_TZ, line) 154 | if mt: 155 | timezone = mt.group(1) 156 | continue 157 | 158 | slcustom = [] 159 | needinput = True 160 | if os.path.exists(custom_file): 161 | with open(custom_file, 'r') as fcustom: 162 | slcustom = fcustom.read().split('\n') 163 | for line in slcustom: 164 | if line.lower().startswith('application:'): 165 | app = line[len('application:'):].strip() 166 | elif line.lower().startswith('email:'): 167 | email = line[len('email:'):].strip() 168 | elif line.lower().startswith('timezone:'): 169 | timezone = line[len('timezone:'):].strip() 170 | 171 | ret = raw_input('Your custom info :\n\t app id : %s\n\t email : %s\n\ttimezone : %s\nCorrect? (y/n) : '%(app,email,timezone)) 172 | if ret in ('y', 'yes', 'Y', 'YES'): 173 | needinput = False #configure items correct! 174 | 175 | while 1: 176 | if needinput or not all((app, email, timezone)): 177 | new_app = raw_input('Input app id (%s): ' % app) 178 | new_email = raw_input('Input your gmail (%s): ' % email) 179 | new_timezone = raw_input('Input your timezone (%s): ' % timezone) 180 | app = new_app if new_app else app 181 | email = new_email if new_email else email 182 | timezone = new_timezone if new_timezone else timezone 183 | with open(custom_file, 'w') as fcustom: 184 | fcustom.write('application: %s\n' % app) 185 | fcustom.write('email: %s\n' % email) 186 | fcustom.write('timezone: %s' % timezone) 187 | 188 | if all((app, email, timezone)): 189 | break 190 | elif not app: 191 | print('app id is empty, please input it again.') 192 | elif not email: 193 | print('email is empty, please input it again.') 194 | elif not timezone: 195 | print('timezone is empty, please input it again.') 196 | 197 | #Check and modify app.yaml 198 | mt = re.match(PAT_APP, slapp[0]) 199 | if mt: 200 | if mt.group(1) != app: 201 | slapp[0] = 'application: ' + app 202 | with open(app_yaml, 'w') as fapp: 203 | fapp.write('\n'.join(slapp)) 204 | else: 205 | print('app.yaml seems invalid, please download KindleEar again.') 206 | return 1 207 | 208 | #Check and modify module-work.yaml 209 | if slwork: 210 | mt = re.match(PAT_APP, slwork[0]) 211 | if mt: 212 | if mt.group(1) != app: 213 | slwork[0] = 'application: ' + app 214 | with open(work_yaml, 'w') as fwork: 215 | fwork.write('\n'.join(slwork)) 216 | else: 217 | print('module-work.yaml seems invalid, please download KindleEar again.') 218 | return 1 219 | 220 | #Check and modify config.py 221 | cfg_changed = False 222 | for index, line in enumerate(slcfg): 223 | mt = re.match(PAT_EMAIL, line) 224 | if mt: 225 | if mt.group(1) != email: 226 | slcfg[index] = 'SRC_EMAIL = "%s"' % email + mt.group(2) 227 | cfg_changed = True 228 | continue 229 | mt = re.match(PAT_DOMAIN, line) 230 | if mt: 231 | domain = mt.group(1) 232 | if domain.endswith('appspot.com') and domain not in ( 233 | 'http://%s.appspot.com' % app, 'https://%s.appspot.com' % app): 234 | slcfg[index] = 'DOMAIN = "https://%s.appspot.com"' % app + mt.group(2) 235 | cfg_changed = True 236 | continue 237 | mt = re.match(PAT_TZ, line) 238 | if mt: 239 | if mt.group(1) != timezone: 240 | slcfg[index] = 'TIMEZONE = %s'%timezone + mt.group(2) 241 | cfg_changed = True 242 | continue 243 | if cfg_changed: 244 | with codecs.open(cfg_file, 'w', 'utf-8') as fcfg: 245 | fcfg.write('\n'.join(slcfg)) 246 | 247 | RemoveChineseBooks(ke_dir) 248 | 249 | return 0 250 | 251 | if __name__ == '__main__': 252 | print('\nKindleEar uploader v%s (%s) by %s\n' % (__Version__, __Date__, __Author__)) 253 | ret = Main() 254 | if ret: 255 | import sys 256 | sys.exit(ret) 257 | -------------------------------------------------------------------------------- /img/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/miaowm5/KeUploader/23f8e2612a0b7c0cf358812aaae378a68d452404/img/1.png -------------------------------------------------------------------------------- /img/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/miaowm5/KeUploader/23f8e2612a0b7c0cf358812aaae378a68d452404/img/2.png -------------------------------------------------------------------------------- /img/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/miaowm5/KeUploader/23f8e2612a0b7c0cf358812aaae378a68d452404/img/3.png -------------------------------------------------------------------------------- /img/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/miaowm5/KeUploader/23f8e2612a0b7c0cf358812aaae378a68d452404/img/4.png -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # KeUploader 2 | 3 | ## Upload KindleEar in 4 Steps 4 | 5 | __1.Open cloud shell__ 6 | 7 | ![img](img/1.png) 8 | 9 | __2.Clone and cd then sh upload.sh__ 10 | 11 | 12 | ```shell 13 | git clone https://github.com/miaowm5/KeUploader.git 14 | cd KeUploader 15 | sh upload.sh 16 | ``` 17 | 18 | ![img](img/2.png) 19 | 20 | __3.Set information of your app__ 21 | 22 | ![img](img/4.png) 23 | 24 | __4.Open yourappid.appspot.com and enjoy. :-)__ 25 | 26 | ![img](img/3.png) 27 | 28 | -------------------------------------------------------------------------------- /update.sh: -------------------------------------------------------------------------------- 1 | python helper.py 2 | appcfg.py update KindleEar/app.yaml KindleEar/module-worker.yaml 3 | appcfg.py update KindleEar/ 4 | -------------------------------------------------------------------------------- /upload.sh: -------------------------------------------------------------------------------- 1 | rm -rf KindleEar 2 | git clone https://github.com/cdhigh/KindleEar.git 3 | python helper.py 4 | appcfg.py update KindleEar/app.yaml KindleEar/module-worker.yaml 5 | appcfg.py update KindleEar/ --------------------------------------------------------------------------------