def check_for_update(self): '''Use updatecheck.py to check for newer versions of the plugin''' chk = UpdateChecker(self.prefs['last_time_checked'], self.bk._w) update_available, online_version, time = chk.update_info() # update preferences with latest date/time/version self.prefs['last_time_checked'] = time if online_version is not None: self.prefs['last_online_version'] = online_version if update_available: return (True, online_version) return (False, online_version)
def check_for_update(self): last_time_checked = self.update_prefs['last_time_checked'] last_online_version = self.update_prefs['last_online_version'] chk = UpdateChecker(last_time_checked, last_online_version, self.bk._w) update_available, online_version, time = chk.update_info() # update preferences with latest date/time/version self.update_prefs['last_time_checked'] = time if online_version is not None: self.update_prefs['last_online_version'] = online_version if update_available: return (True, online_version) return (False, online_version)
def run(bk): global prefs global GUI if bk.launcher_version() >= 20170115: GUI = 'pyqt' else: GUI = 'tkinter' prefs = bk.getPrefs() # set default preference values if 'use_file_path' not in prefs: prefs['use_file_path'] = expanduser('~') if 'azw3_epub_version' not in prefs: prefs['azw3_epub_version'] = "2" # A, F, 2 or 3 if 'use_hd_images' not in prefs: prefs['use_hd_images'] = True if 'use_src_from_dual_mobi' not in prefs: prefs['use_src_from_dual_mobi'] = True if 'asin_for_kindlegen_plugin' not in prefs: prefs['asin_for_kindlegen_plugin'] = False if 'preserve_kindleunpack_meta' not in prefs: prefs['preserve_kindleunpack_meta'] = False if 'last_time_checked' not in prefs: prefs['last_time_checked'] = str(datetime.now() - timedelta(hours=7)) if 'last_online_version' not in prefs: prefs['last_online_version'] = '0.1.0' chk = UpdateChecker(prefs['last_time_checked'], prefs['last_online_version'], bk._w) update_available, online_version, time = chk.update_info() # update preferences with latest date/time/version prefs['last_time_checked'] = time if online_version is not None: prefs['last_online_version'] = online_version if update_available: title = 'Plugin Update Available' msg = 'Version {} of the {} plugin is now available.'.format(online_version, bk._w.plugin_name) # update_msgbox(title, msg) update_msgbox(title, msg, bk, GUI) if _DEBUG_: print('Python sys.path', sys.path) print('Default AZW3 epub version:', prefs['azw3_epub_version']) # inpath = fileChooser() inpath = fileChooser(prefs['use_file_path'], bk, GUI) if inpath == '' or not os.path.exists(inpath): print('No input file selected!') bk.savePrefs(prefs) return 0 print('Path to Kindlebook {0}'.format(inpath)) from mobi_stuff import mobiProcessor, topaz if topaz(inpath): print('Kindlebook is in Topaz format: can\'t open!') bk.savePrefs(prefs) return -1 mobionly = False mp = mobiProcessor(inpath, prefs['azw3_epub_version'], prefs['use_hd_images']) # Save last directory accessed to JSON prefs prefs['use_file_path'] = pathof(os.path.dirname(inpath)) if mp.isEncrypted: print('Kindlebook is encrypted: can\'t open!') bk.savePrefs(prefs) return -1 if mp.isPrintReplica: print('Kindlebook is a Print Replica: can\'t open!') bk.savePrefs(prefs) return -1 if not mp.isComboFile and not mp.isKF8: mobionly = True with make_temp_directory() as temp_dir: TWEAK = True asin = None if not mobionly: epub, opf, src = mp.unpackEPUB(temp_dir) if src is not None and isEPUB(src) and prefs['use_src_from_dual_mobi']: print('Using included kindlegen sources.') epub = src else: # If user requested no tweaks through preferences, use standard epub from KindleUnpack if not prefs['asin_for_kindlegen_plugin'] and not prefs['preserve_kindleunpack_meta']: TWEAK = False elif prefs['asin_for_kindlegen_plugin']: if opf is not None: # Get asin from metadata and put it in a dc:meta that the Kindlegen plugin can use. asin = get_asin(opf) if asin is not None: asin = unicode_str(asin) else: TWEAK = False if TWEAK: # Modify the opf with the requested tweaks and build a new epub if tweak_opf(opf, asin, epub_version=prefs['azw3_epub_version'], preserve_comments=prefs['preserve_kindleunpack_meta']): os.remove(epub) with temp_epub_handle(delete=False) as new_epub: epub_zip_up_book_contents(os.path.join(temp_dir,'mobi8'), new_epub) epub = new_epub else: from quickepub import QuickEpub mobidir, mobi_html, mobi_opf, mobiBaseName = mp.unpackMOBI(temp_dir) if not prefs['asin_for_kindlegen_plugin'] and not prefs['preserve_kindleunpack_meta']: TWEAK = False elif prefs['asin_for_kindlegen_plugin']: if mobi_opf is not None: # Get asin from metadata and put it in a dc:meta that the Kindlegen plugin can use. asin = get_asin(mobi_opf) if asin is not None: asin = unicode_str(asin) else: TWEAK = False if TWEAK: if not tweak_opf(mobi_opf, asin, preserve_comments=prefs['preserve_kindleunpack_meta']): print('OPF manipulation failed!') return -1 qe = QuickEpub(mobidir, mobi_html, mobi_opf) epub = qe.makeEPUB() # Save prefs to json bk.savePrefs(prefs) print('Path to epub or src {0}'.format(epub)) with file_open(epub,'rb')as fp: data = fp.read() bk.addotherfile('dummy.epub', data) return 0
def run(bk): if not bk.epub_version().startswith('3'): print('Error: NCXRemove requires a valid EPUB 3.0 ebook as input') return -1 try: from PyQt5.QtWidgets import QApplication except ImportError: GUI = 'tkinter' else: GUI = 'pyqt' ncxid = bk.gettocid() if ncxid is None: print('Error: EPUB 3.0 ebook has no NCX') return -1 ncxhref = bk.id_to_href(ncxid) prefs = bk.getPrefs() # set default preference values if 'use_file_path' not in prefs: prefs['use_file_path'] = os.path.expanduser('~') if 'check_for_updates' not in prefs: prefs['check_for_updates'] = True if 'last_time_checked' not in prefs: prefs['last_time_checked'] = str(datetime.now() - timedelta(hours=12)) if 'last_online_version' not in prefs: prefs['last_online_version'] = '0.1.0' if prefs['check_for_updates']: chk = UpdateChecker(prefs['last_time_checked'], prefs['last_online_version'], bk._w) update_available, online_version, time = chk.update_info() # update preferences with latest date/time/version prefs['last_time_checked'] = time if online_version is not None: prefs['last_online_version'] = online_version if update_available: title = 'Plugin Update Available' msg = 'Version {} of the {} plugin is now available.'.format( online_version, bk._w.plugin_name) update_msgbox(title, msg, GUI) if _DEBUG_: print('Python sys.path', sys.path) doctitle = getTitle(bk.getmetadataxml()) fname = cleanup_file_name(doctitle) + '_no_ncx.epub' outpath = fileChooser(fname, prefs['use_file_path'], GUI) if not outpath: print("NCXRemove plugin cancelled by user") return 0 with make_temp_directory() as temp_dir: bk.copy_book_contents_to(temp_dir) opfdata = bk.readotherfile("OEBPS/content.opf").splitlines() opfout = os.path.join(temp_dir, 'OEBPS', 'content.opf') ncxfile = os.path.join(temp_dir, 'OEBPS', ncxhref) open(os.path.join(temp_dir, 'mimetype'), 'wb').write('application/epub+zip'.encode('utf-8')) newopf = '' ncxitem = 'id="{}"'.format(ncxid) spineattr = ' toc="{}"'.format(ncxid) for line in opfdata: skip = False # Remove ncx item from the manifest of the OPF if line.lstrip().startswith('<item ') and line.find( ncxitem) is not -1: skip = True # Remove the ncx attribute from the spine tag of the OPF if line.lstrip().startswith('<spine') and line.find( spineattr) is not -1: line = line.replace(spineattr, '') if not skip: newopf += line + '\n' if _DEBUG_: print(line) # Write new OPF to temp directory open(opfout, 'wb').write(newopf.encode('utf-8')) # Delete the the toc.ncx file from the temp directory os.remove(ncxfile) # Build the new epub from the tmp structure and save to specified location epub_zip_up_book_contents(temp_dir, outpath) # Save last directory accessed to JSON prefs prefs['use_file_path'] = os.path.dirname(outpath) # Save prefs to json bk.savePrefs(prefs) # print ('Path to epub or src {0}'.format(epub)) return 0
def run(bk): global prefs prefs = bk.getPrefs() # set default preference values if 'use_file_path' not in prefs: prefs['use_file_path'] = expanduser('~') if 'check_for_updates' not in prefs: prefs['check_for_updates'] = True if 'last_time_checked' not in prefs: prefs['last_time_checked'] = str(datetime.now() - timedelta(hours=delta + 1)) if 'last_online_version' not in prefs: prefs['last_online_version'] = '0.1.0' if prefs['check_for_updates']: chk = UpdateChecker(prefs['last_time_checked'], prefs['last_online_version'], bk._w) update_available, online_version, time = chk.update_info() # update preferences with latest date/time/version prefs['last_time_checked'] = time if online_version is not None: prefs['last_online_version'] = online_version if update_available: title = 'Plugin Update Available' msg = 'Version {} of the {} plugin is now available.'.format( online_version, bk._w.plugin_name) show_msgbox(title, msg, 'info') if 'META-INF/{}'.format(XMLFILE) in bk._w.other: title = 'File Already Present!' msg = 'The {} file is already present. Please delete it before trying to add another'.format( XMLFILE) show_msgbox(title, msg, 'error') return 0 if _DEBUG_: print('Python sys.path: {}\n'.format(sys.path)) inpath = fileChooser() if inpath == '' or not os.path.exists(inpath): print('iBooks XML file selection canceled!') bk.savePrefs(prefs) return 0 if _DEBUG_: print('Path to XML file: {}\n'.format(inpath)) # Save last directory accessed to JSON prefs prefs['use_file_path'] = pathof(os.path.dirname(inpath)) # Save prefs to json bk.savePrefs(prefs) try: with file_open(inpath, 'rb') as fp: data = fp.read() except: title = 'Unexpected error!' msg = 'Error reading the {} file. Perhaps it is corrupt or missing?'.format( XMLFILE) show_msgbox(title, msg, 'error') return -1 if _DEBUG_: print('Internal epub href: META-INF/{}\n'.format(XMLFILE)) bk.addotherfile('META-INF/{}'.format(XMLFILE), data) return 0