def _save_index_file(self, name, data): LOG.debug('Save index file %r', name) path = self._index_path(name) if data: require_directory(os.path.dirname(path)) with open(path, 'w') as dst: json.dump(data, dst) else: delete_if_exists(path)
def cache_put(self, namespace, key, value): '''Put a value into the cache.''' LOG.debug('Cache put %r: %r', key, value) path = self._cache_path(namespace, key) forget = not bool(value) if not forget: try: require_directory(os.path.dirname(path)) with open(path, 'w') as cachefile: cachefile.write(value) except Exception as err: LOG.error('Error writing cache file: %r', err) forget = True # value was empty or writing failed if forget: self.cache_forget(namespace, key)
def _download_one(self, index, url, content_type, dst_file=None): '''Download a single enclosure from the given URL. Generates a local filename for the enclosure and stores the downloaded data there. If dst is given, no filename is generated but dst_path is used. returns the path to the local file. ''' if dst_file: local_file = dst_file else: filename = self._generate_filename(content_type, index) local_file = os.path.join(self.subscription.content_dir, filename) local_file = unique_filename(local_file) LOG.info('Download from %r.', url) LOG.info('Local file is %r.', local_file) require_directory(os.path.dirname(local_file)) download(url, local_file) return local_file
def save_subscription(self, subscription): '''Save a single subscription.''' s = subscription cfg = _mk_config_parser() cfg.add_section(SECTION) def _set(key, value): if value: # will lose max_episodes = 0 cfg.set(SECTION, key, value) _set('url', s.feed_url) _set('max_episodes', str(s.max_episodes)) _set('enabled', 'yes' if s.enabled else 'no') _set('title', s.title) _set('filename_template', s.filename_template) _set('content_dir', s._content_dir) path = self._subscription_path(s.name) LOG.debug('Save Subscription %r to %r.', s.name, path) require_directory(os.path.dirname(path)) with open(path, 'w') as fp: cfg.write(fp)
def move_local_files(self): '''Re-apply the filename template for this Episode's downloaded files and rename files. Useful if the filename pattern changes. ''' files = self.files[:] for index, details in enumerate(files): url, content_type, oldpath = details newpath = os.path.join(self.subscription.content_dir, self._generate_filename(content_type, index)) if not oldpath: LOG.warning('Episode %r has no local file', self) elif newpath != oldpath: newpath = unique_filename(newpath) dirname = os.path.dirname(newpath) require_directory(dirname) LOG.debug('Move %r -> %r', oldpath, newpath) try: shutil.move(oldpath, newpath) self.files[index] = (url, content_type, newpath) except FileNotFoundError: LOG.warning(('Failed to rename file %r.' ' File does not exist.'), oldpath)