def _start_webserver(self): ''' setup bibi and start web server ''' self.htmlroot = PersistentTemporaryDirectory() print(self.htmlroot) zipfile = os.path.join(self.htmlroot, 'bibi.zip') with open(zipfile, 'wb') as f: f.write( self.load_resources(['bibi-0.999.9-r7.zip' ]).itervalues().next()) extract(zipfile, self.htmlroot) handler = RootedHTTPRequestHandler self.htmlroot = self.htmlroot + '/bibi-0.999.9-r7/bib' handler.base_path = self.htmlroot self.httpd = ThreadedTCPServer(('localhost', 0), handler) self.ip, self.port = self.httpd.server_address print('server started at: ', self.ip, str(self.port)) # Start a thread with the server -- that thread will then start one # more thread for each request server_thread = threading.Thread(target=self.httpd.serve_forever) # Exit the server thread when the main thread terminates server_thread.daemon = True server_thread.start() print("Server loop running in thread: ", server_thread.name)
def extract_comic(path_to_comic_file): ''' Un-archive the comic file. ''' tdir = PersistentTemporaryDirectory(suffix='_comic_extract') if not isinstance(tdir, unicode): # Needed in case the zip file has wrongly encoded unicode file/dir # names tdir = tdir.decode(filesystem_encoding) extract(path_to_comic_file, tdir) for x in walk(tdir): bn = os.path.basename(x) nbn = bn.replace('#', '_') if nbn != bn: os.rename(x, os.path.join(os.path.dirname(x), nbn)) return tdir
def open_epub_by_bibi(self): c = config.plugin_prefs[config.STORE_NAME] rows = self.gui.library_view.selectionModel().selectedRows() if not rows or len(rows) != 1: return error_dialog( self.gui, 'No rows selected', 'You must select one book to perform this action.', show=True) book_id = self.gui.library_view.get_selected_ids()[0] db = self.gui.library_view.model().db epubpath = db.format_abspath(book_id, "EPUB", index_is_id=True) if not epubpath or not os.path.exists(epubpath): return error_dialog( self.gui, 'No EPUB file', 'You must select a book which has EPUB format.', show=True) if not hasattr(self, 'httpd'): self._start_webserver() book_path = str(book_id) + "_" + str(os.stat(epubpath).st_mtime) book_fullpath = os.path.join(self.htmlroot, 'bookshelf', book_path) if not os.path.exists(book_fullpath): extract(epubpath, book_fullpath) url = 'http://localhost:' + str(self.port) + '/i/?book=' + book_path browser = c.get(config.KEY_PATH_BROWSER).strip() if browser: # Open url by specified browser cmd = browser.split() cmd.append(url) subprocess.Popen(cmd) else: # Open url using platform default browser if platform.system() == "Windows": os.startfile(url) elif platform.system() == "Darwin": subprocess.Popen(["open", url]) else: subprocess.Popen(["xdg-open", url])
def unarchive(self, path, tdir): extract(path, tdir) files = list(walk(tdir)) files = [f if isinstance(f, unicode) else f.decode(filesystem_encoding) for f in files] from calibre.customize.ui import available_input_formats fmts = set(available_input_formats()) fmts -= {'htm', 'html', 'xhtm', 'xhtml'} fmts -= set(ARCHIVE_FMTS) for ext in fmts: for f in files: if f.lower().endswith('.'+ext): if ext in ['txt', 'rtf'] and os.stat(f).st_size < 2048: continue return f, ext return self.find_html_index(files)
def convert_book(self, input, format): if format in ARCHIVE_FORMATS: content = [] try: file_formats = prefs['file_formats'].split(',') directory = calibre.ptempfile.PersistentTemporaryDirectory() calibre.extract(input, directory) for curr_dir, _, files in os.walk(directory): for f in files: for fmt in file_formats: if f.upper().endswith('.' + fmt): content += self.convert_book( os.path.join(curr_dir, f), fmt) except Exception as ex: print(ex) return content os_name = platform.system() output = self.plugin.temporary_file(suffix='.txt').name creationflags = SUBPROCESS_CREATION_FLAGS[os_name] done = False if format == 'PDF': pdftotext_full_path = self._get_pdftotext_full_path() if pdftotext_full_path: subprocess.call( [pdftotext_full_path, '-enc', 'UTF-8', input, output], stdout=FNULL, stderr=FNULL, creationflags=creationflags) # print('Book {} converted with pdfttotext.'.format(id)) done = True if not done: ebook_convert_path = '/Applications/calibre.app/Contents/MacOS/ebook-convert' if os == 'Darwin' else 'ebook-convert' subprocess.call([ebook_convert_path, input, output], stdout=FNULL, stderr=FNULL, creationflags=creationflags) # print('Book {} converted to plaintext'.format(id)) if sys.version_info[0] >= 3: content = open(output, errors='ignore').readlines() else: content = open(output).readlines() return content