def _update_podcast(self, podcast, parsed, episode_updater, update_result): """ updates a podcast according to new parser results """ # we need that later to decide if we can "bump" a category prev_latest_episode_timestamp = podcast.latest_episode_timestamp # will later be used to see whether the index is outdated old_index_fields = get_index_fields(podcast) podcast.title = parsed.get('title') or podcast.title podcast.description = parsed.get('description') or podcast.description podcast.subtitle = parsed.get('subtitle') or podcast.subtitle podcast.link = parsed.get('link') or podcast.link podcast.logo_url = parsed.get('logo') or podcast.logo_url podcast.author = to_maxlength(Podcast, 'author', parsed.get('author') or podcast.author) podcast.language = to_maxlength( Podcast, 'language', parsed.get('language') or podcast.language) podcast.content_types = (','.join(parsed.get('content_types')) or podcast.content_types) # podcast.tags['feed'] = parsed.tags or podcast.tags.get('feed', []) podcast.common_episode_title = to_maxlength( Podcast, 'common_episode_title', parsed.get('common_title') or podcast.common_episode_title, ) podcast.new_location = parsed.get( 'new_location') or podcast.new_location podcast.flattr_url = to_maxlength( Podcast, 'flattr_url', parsed.get('flattr') or podcast.flattr_url) podcast.hub = parsed.get('hub') or podcast.hub podcast.license = parsed.get('license') or podcast.license podcast.max_episode_order = episode_updater.max_episode_order podcast.add_missing_urls(parsed.get('urls', [])) if podcast.new_location: try: new_podcast = Podcast.objects.get( urls__url=podcast.new_location) if new_podcast != podcast: self._mark_outdated(podcast, 'redirected to different podcast', episode_updater) return except Podcast.DoesNotExist: podcast.set_url(podcast.new_location) # latest episode timestamp episodes = Episode.objects.filter( podcast=podcast, released__isnull=False).order_by('released') # Determine update interval # Update interval is based on intervals between episodes podcast.update_interval = episode_updater.get_update_interval(episodes) # factor is increased / decreased depending on whether the latest # update has returned episodes if update_result.episodes_added == 0: # no episodes, incr factor newfactor = podcast.update_interval_factor * 1.2 podcast.update_interval_factor = min(1000, newfactor) # never above 1000 elif update_result.episodes_added > 1: # new episodes, decr factor newfactor = podcast.update_interval_factor / 1.2 podcast.update_interval_factor = max(1, newfactor) # never below 1 latest_episode = episodes.last() if latest_episode: podcast.latest_episode_timestamp = latest_episode.released # podcast.episode_count is not update here on purpose. It is, instead, # continuously updated when creating new episodes in # EpisodeManager.get_or_create_for_url self._update_categories(podcast, prev_latest_episode_timestamp) # try to download the logo and reset logo_url to None on http errors found = CoverArt.save_podcast_logo(podcast.logo_url) if not found: podcast.logo_url = None # check if search index should be considered out of date new_index_fields = get_index_fields(podcast) if list(old_index_fields.items()) != list(new_index_fields.items()): podcast.search_index_uptodate = False # The podcast is always saved (not just when there are changes) because # we need to record the last update logger.info('Saving podcast.') podcast.last_update = datetime.utcnow() podcast.save() try: subscribe_at_hub(podcast) except SubscriptionError as se: logger.warn('subscribing to hub failed: %s', str(se)) self.assign_slug(podcast) episode_updater.assign_missing_episode_slugs() update_related_podcasts.delay(podcast.pk)
def _update_podcast(podcast, parsed, episodes, max_episode_order): """ updates a podcast according to new parser results """ # we need that later to decide if we can "bump" a category prev_latest_episode_timestamp = podcast.latest_episode_timestamp podcast.title = parsed.get('title') or podcast.title podcast.description = parsed.get('description') or podcast.description podcast.subtitle = parsed.get('subtitle') or podcast.subtitle podcast.link = parsed.get('link') or podcast.link podcast.logo_url = parsed.get('logo') or podcast.logo_url podcast.author = to_maxlength(Podcast, 'author', parsed.get('author') or podcast.author) podcast.language = to_maxlength(Podcast, 'language', parsed.get('language') or podcast.language) podcast.content_types = ','.join(parsed.get('content_types')) or \ podcast.content_types #podcast.tags['feed'] = parsed.tags or podcast.tags.get('feed', []) podcast.common_episode_title = to_maxlength( Podcast, 'common_episode_title', parsed.get('common_title') or podcast.common_episode_title) podcast.new_location = parsed.get('new_location') or podcast.new_location podcast.flattr_url = to_maxlength(Podcast, 'flattr_url', parsed.get('flattr') or podcast.flattr_url) podcast.hub = parsed.get('hub') or podcast.hub podcast.license = parsed.get('license') or podcast.license podcast.max_episode_order = max_episode_order podcast.add_missing_urls(parsed.get('urls', [])) if podcast.new_location: try: new_podcast = Podcast.objects.get(urls__url=podcast.new_location) if new_podcast != podcast: _mark_outdated(podcast, 'redirected to different podcast') return except Podcast.DoesNotExist: podcast.set_url(podcast.new_location) # latest episode timestamp episodes = Episode.objects.filter(podcast=podcast, released__isnull=False)\ .order_by('released') podcast.update_interval = get_update_interval(episodes) latest_episode = episodes.last() if latest_episode: podcast.latest_episode_timestamp = latest_episode.released # podcast.episode_count is not update here on purpose. It is, instead, # continuously updated when creating new episodes in # EpisodeManager.get_or_create_for_url _update_categories(podcast, prev_latest_episode_timestamp) # try to download the logo and reset logo_url to None on http errors found = _save_podcast_logo(podcast.logo_url) if not found: podcast.logo_url = None # The podcast is always saved (not just when there are changes) because # we need to record the last update logger.info('Saving podcast.') podcast.last_update = datetime.utcnow() podcast.save() try: subscribe_at_hub(podcast) except SubscriptionError as se: logger.warn('subscribing to hub failed: %s', str(se)) if not podcast.slug: slug = PodcastSlug(podcast).get_slug() if slug: podcast.add_slug(slug) assign_missing_episode_slugs(podcast) update_related_podcasts.delay(podcast)
def _update_podcast(self, podcast, parsed, episodes): """ updates a podcast according to new parser results """ # we need that later to decide if we can "bump" a category prev_latest_episode_timestamp = podcast.latest_episode_timestamp old_json = copy.deepcopy(podcast.to_json()) podcast.title = parsed.title or podcast.title podcast.urls = list(set(podcast.urls + parsed.urls)) podcast.description = parsed.description or podcast.description podcast.subtitle = parsed.subtitle or podcast.subtitle podcast.link = parsed.link or podcast.link podcast.logo_url = parsed.logo or podcast.logo_url podcast.author = parsed.author or podcast.author podcast.language = parsed.language or podcast.language podcast.content_types = parsed.content_types or podcast.content_types podcast.tags['feed'] = parsed.tags or podcast.tags.get('feed', []) podcast.common_episode_title = parsed.common_title or podcast.common_episode_title podcast.new_location = parsed.new_location or podcast.new_location podcast.flattr_url = parsed.flattr or podcast.flattr_url podcast.hub = parsed.hub or podcast.hub podcast.license = parsed.license or podcast.license if podcast.new_location: new_podcast = podcast_for_url(podcast.new_location) if new_podcast != podcast: self._mark_outdated(podcast, 'redirected to different podcast') return elif not new_podcast: podcast.urls.insert(0, podcast.new_location) logger.info('Retrieved %d episodes in total', len(episodes)) # latest episode timestamp eps = filter(lambda e: bool(e.released), episodes) eps = sorted(eps, key=lambda e: e.released) podcast.update_interval = get_update_interval(eps) if eps: podcast.latest_episode_timestamp = eps[-1].released podcast.episode_count = episode_count_for_podcast(podcast) self._update_categories(podcast, prev_latest_episode_timestamp) # try to download the logo and reset logo_url to None on http errors found = self._save_podcast_logo(podcast.logo_url) if not found: podcast.logo_url = None # The podcast is always saved (not just when there are changes) because # we need to record the last update logger.info('Saving podcast.') podcast.last_update = datetime.utcnow() podcast.save() try: subscribe_at_hub(podcast) except SubscriptionError as se: logger.warn('subscribing to hub failed: %s', str(se)) assign_slug(podcast, PodcastSlug) assign_missing_episode_slugs(podcast)