def get_newest_version(): try: icon_theme_name = json.loads(I('icon-theme.json', data=True))['name'] except Exception: icon_theme_name = '' headers={ 'CALIBRE-VERSION':__version__, 'CALIBRE-OS': ('win' if iswindows else 'osx' if isosx else 'oth'), 'CALIBRE-INSTALL-UUID': prefs['installation_uuid'], 'CALIBRE-ICON-THEME': icon_theme_name, } try: version = get_https_resource_securely(URL, headers=headers) except ssl.SSLError as err: if getattr(err, 'reason', None) != 'CERTIFICATE_VERIFY_FAILED': raise # certificate verification failed, since the version check contains no # critical information, ignore and proceed # We have to do this as if the calibre CA certificate ever # needs to be revoked, then we wont be able to do version checks version = get_https_resource_securely(URL, headers=headers, cacerts=None) try: version = version.decode('utf-8').strip() except UnicodeDecodeError: version = u'' ans = NO_CALIBRE_UPDATE m = re.match(unicode(r'(\d+)\.(\d+).(\d+)$'), version) if m is not None: ans = tuple(map(int, (m.group(1), m.group(2), m.group(3)))) return ans
def update_plugin(name, updated, expected_hash): raw = get_https_resource_securely('https://code.calibre-ebook.com/metadata-sources/' + name) h = hashlib.sha1(raw).hexdigest() if h != expected_hash: raise ValueError('Actual hash did not match expected hash, probably an update occurred while downloading') plugin = bz2.decompress(raw).decode('utf-8') updated[name] = plugin, h
def read_available_plugins(raise_error=False): import json, bz2 display_plugins = [] try: raw = get_https_resource_securely(INDEX_URL) if not raw: return raw = json.loads(bz2.decompress(raw)) except: if raise_error: raise traceback.print_exc() return for plugin in itervalues(raw): try: display_plugin = DisplayPlugin(plugin) get_installed_plugin_status(display_plugin) display_plugins.append(display_plugin) except: if DEBUG: prints('======= Plugin Parse Error =======') traceback.print_exc() import pprint pprint.pprint(plugin) display_plugins = sorted(display_plugins, key=lambda k: k.name) return display_plugins
def download_builtin_recipe(urn): from calibre.utils.config_base import prefs from calibre.utils.https import get_https_resource_securely import bz2 recipe_source = bz2.decompress(get_https_resource_securely( 'https://code.calibre-ebook.com/recipe-compressed/'+urn, headers={'CALIBRE-INSTALL-UUID':prefs['installation_uuid']})) from calibre.web.feeds.recipes import compile_recipe recipe = compile_recipe(recipe_source) # ensure the downloaded recipe is at least compile-able if recipe is None: raise ValueError('Failed to find recipe object in downloaded recipe: ' + urn) if recipe.requires_version > numeric_version: raise ValueError('Downloaded recipe for {} requires calibre >= {}'.format(urn, recipe.requires_version)) return recipe_source
def download(): self.downloaded_theme = buf = BytesIO() try: response = get_https_resource_securely(url, get_response=True) while self.keep_downloading: raw = response.read(1024) if not raw: break buf.write(raw) d.downloaded(buf.tell()) d.queue_accept() except Exception: import traceback self.downloaded_theme = traceback.format_exc() d.queue_reject()
def download(): self.downloaded_theme = buf = BytesIO() try: response = get_https_resource_securely(url, get_response=True) while self.keep_downloading: raw = response.read(1024) if not raw: break buf.write(raw) d.downloaded(buf.tell()) d.queue_accept() except Exception: import traceback self.downloaded_theme = traceback.format_exc() d.queue_reject()
def download_cover(cover_url, etag=None, cached=b''): url = BASE_URL + cover_url headers = {} if etag: if etag[0] != '"': etag = '"' + etag + '"' headers['If-None-Match'] = etag try: response = get_https_resource_securely(url, headers=headers, get_response=True) cached = response.read() etag = response.getheader('ETag', None) or None return cached, etag except HTTPError as e: if etag and e.code == httplib.NOT_MODIFIED: return cached, etag raise
def update_needed(): needed = {} current_hashes = cache.get('hashes', {}) hashes = get_https_resource_securely( 'https://code.calibre-ebook.com/metadata-sources/hashes.json') hashes = bz2.decompress(hashes) hashes = json.loads(hashes) for k, v in hashes.iteritems(): if current_hashes.get(k) != v: needed[k] = v remove = set(current_hashes) - set(hashes) if remove: with cache: for k in remove: current_hashes.pop(k, None) del cache[k] cache['hashes'] = current_hashes return needed
def download_builtin_recipe(urn): from calibre.utils.config_base import prefs from calibre.utils.https import get_https_resource_securely import bz2 recipe_source = bz2.decompress( get_https_resource_securely( 'https://code.calibre-ebook.com/recipe-compressed/' + urn, headers={'CALIBRE-INSTALL-UUID': prefs['installation_uuid']})) from calibre.web.feeds.recipes import compile_recipe recipe = compile_recipe( recipe_source) # ensure the downloaded recipe is at least compile-able if recipe is None: raise ValueError( 'Failed to find recipe object in downloaded recipe: ' + urn) if recipe.requires_version > numeric_version: raise ValueError( 'Downloaded recipe for {} requires calibre >= {}'.format( urn, recipe.requires_version)) return recipe_source
def download_updates(ver_map={}, server='https://code.calibre-ebook.com'): from calibre.utils.https import get_https_resource_securely data = {k:unicode_type(v) for k, v in iteritems(ver_map)} data['ver'] = '1' url = '%s/stores?%s'%(server, urlencode(data)) # We use a timeout here to ensure the non-daemonic update thread does not # cause calibre to hang indefinitely during shutdown raw = get_https_resource_securely(url, timeout=90.0) while raw: name, raw = raw.partition(b'\0')[0::2] name = name.decode('utf-8') d = decompressobj() src = d.decompress(raw) src = src.decode('utf-8').lstrip(u'\ufeff') # Python complains if there is a coding declaration in a unicode string src = re.sub(r'^#.*coding\s*[:=]\s*([-\w.]+)', '#', src, flags=re.MULTILINE) # Translate newlines to \n src = io.StringIO(src, newline=None).getvalue() yield name, src raw = d.unused_data
def download_updates(ver_map={}, server='https://code.calibre-ebook.com'): from calibre.utils.https import get_https_resource_securely data = {k:type(u'')(v) for k, v in ver_map.iteritems()} data['ver'] = '1' url = '%s/stores?%s'%(server, urlencode(data)) # We use a timeout here to ensure the non-daemonic update thread does not # cause calibre to hang indefinitely during shutdown raw = get_https_resource_securely(url, timeout=90.0) while raw: name, raw = raw.partition(b'\0')[0::2] name = name.decode('utf-8') d = decompressobj() src = d.decompress(raw) src = src.decode('utf-8').lstrip(u'\ufeff') # Python complains if there is a coding declaration in a unicode string src = re.sub(r'^#.*coding\s*[:=]\s*([-\w.]+)', '#', src, flags=re.MULTILINE) # Translate newlines to \n src = io.StringIO(src, newline=None).getvalue() yield name, src raw = d.unused_data
def get_themes(self): self.usage = {} def get_usage(): try: self.usage = json.loads(bz2.decompress(get_https_resource_securely(BASE_URL + '/usage.json.bz2'))) except Exception: import traceback traceback.print_exc() t = Thread(name='IconThemeUsage', target=get_usage) t.daemon = True t.start() try: self.themes = json.loads(bz2.decompress(get_https_resource_securely(BASE_URL + '/themes.json.bz2'))) except Exception: import traceback self.themes = traceback.format_exc() t.join() self.themes_downloaded.emit()
def _download_zip(self, plugin_zip_url): from calibre.ptempfile import PersistentTemporaryFile raw = get_https_resource_securely(plugin_zip_url, headers={'User-Agent':'%s %s' % (__appname__, __version__)}) with PersistentTemporaryFile('.zip') as pt: pt.write(raw) return pt.name
def download_builtin_recipe(urn): from calibre.utils.config_base import prefs from calibre.utils.https import get_https_resource_securely import bz2 return bz2.decompress(get_https_resource_securely( 'https://code.calibre-ebook.com/recipe-compressed/'+urn, headers={'CALIBRE-INSTALL-UUID':prefs['installation_uuid']}))
def verify_ca_certs(self): from calibre.utils.https import get_https_resource_securely get_https_resource_securely('https://calibre-ebook.com', cacerts=self.b(self.CA_PATH))
def verify_ca_certs(self): from calibre.utils.https import get_https_resource_securely get_https_resource_securely('https://calibre-ebook.com', cacerts=self.b(self.CA_PATH))
def _download_zip(self, plugin_zip_url): from calibre.ptempfile import PersistentTemporaryFile raw = get_https_resource_securely(plugin_zip_url, headers={'User-Agent':'%s %s' % (__appname__, __version__)}) with PersistentTemporaryFile('.zip') as pt: pt.write(raw) return pt.name
def download_builtin_recipe(urn): from calibre.utils.https import get_https_resource_securely return get_https_resource_securely('https://status.calibre-ebook.com/recipe/'+urn)
def get_usage(): try: self.usage = json.loads(bz2.decompress(get_https_resource_securely(BASE_URL + '/usage.json.bz2'))) except Exception: import traceback traceback.print_exc()