From d28228187e501e3155accde564fb44e30179ef7c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Stefan=20K=C3=B6gl?= Date: Fri, 28 Nov 2014 09:47:29 +0000 Subject: [PATCH] [Feeds] update / fix feed-downloader --- mygpo/data/feeddownloader.py | 21 +++++++++++++++------ mygpo/podcasts/models.py | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/mygpo/data/feeddownloader.py b/mygpo/data/feeddownloader.py index 9a394f79..dfda3e80 100755 --- a/mygpo/data/feeddownloader.py +++ b/mygpo/data/feeddownloader.py @@ -70,6 +70,11 @@ class PodcastUpdater(object): except NoPodcastCreated as npc: logger.info('No podcast created: %s', npc) + except: + logger.exception('Error while updating podcast "%s"', + podcast_url) + raise + def update(self, podcast_url): """ Update the podcast for the supplied URL """ @@ -142,11 +147,13 @@ class PodcastUpdater(object): 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.author = to_maxlength(Podcast, 'author', parsed.author or podcast.author) + podcast.language = to_maxlength(Podcast, 'language', parsed.language or podcast.language) + podcast.content_types = ','.join(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.common_episode_title = to_maxlength(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 = to_maxlength(Podcast, 'flattr_url', parsed.flattr or podcast.flattr_url) @@ -163,7 +170,7 @@ class PodcastUpdater(object): self._mark_outdated(podcast, 'redirected to different podcast') return except Podcast.DoesNotExist: - podcast.urls.insert(0, podcast.new_location) + podcast.set_url(podcast.new_location) # latest episode timestamp @@ -275,6 +282,8 @@ class PodcastUpdater(object): episode) """ num_episodes = podcast.episode_set.count() + if not num_episodes: + return 0 episodes = podcast.episode_set.all().extra(select={ 'has_released': 'released IS NOT NULL', @@ -367,7 +376,7 @@ def update_episode(parsed_episode, episode, podcast): episode.link = to_maxlength(Episode, 'link', parsed_episode.link or episode.link) episode.released = datetime.utcfromtimestamp(parsed_episode.released) if parsed_episode.released else episode.released - episode.author = parsed_episode.author or episode.author + episode.author = to_maxlength(Episode, 'author', parsed_episode.author or episode.author) episode.duration = parsed_episode.duration or episode.duration episode.filesize = parsed_episode.files[0].filesize episode.language = parsed_episode.language or episode.language or \ diff --git a/mygpo/podcasts/models.py b/mygpo/podcasts/models.py index b86c5d45..2dbbbbc8 100644 --- a/mygpo/podcasts/models.py +++ b/mygpo/podcasts/models.py @@ -174,6 +174,27 @@ class UrlsMixin(models.Model): logger.warn('Could not add URL: {err}'.format(err=ie)) continue + def set_url(self, url): + """ Sets the canonical URL """ + + urls = [u.url for u in self.urls.all()] + if url in urls: + urls.remove(url) + + urls.insert(0, url) + self.set_urls(urls) + + def set_urls(self, urls): + """ Update the object's URLS to the given list + + 'urls' should be a list of strings. Slugs that do not exist are + created. Existing urls that are not in the 'urls' list are + deleted. """ + urls = [utils.to_maxlength(URL, 'url', url) for url in urls] + existing = {u.url: u for u in self.urls.all()} + utils.set_ordered_entries(self, urls, existing, URL, 'url', + 'content_object') + class SlugsMixin(models.Model): """ Methods for working with Slug objects """ -- 2.11.4.GIT