From cd31eaa62c689c979c06900cab77749ef245d32b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Stefan=20K=C3=B6gl?= Date: Sun, 3 Dec 2017 20:59:06 +0100 Subject: [PATCH] Modify Podcast.update_interval with factor When no new episodes are found, the factor increases the interval between podcast updates. --- mygpo/data/feeddownloader.py | 11 +++++++++++ .../0040_podcast_update_interval_factor.py | 20 ++++++++++++++++++++ mygpo/podcasts/models.py | 20 ++++++++++++++++---- 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 mygpo/podcasts/migrations/0040_podcast_update_interval_factor.py diff --git a/mygpo/data/feeddownloader.py b/mygpo/data/feeddownloader.py index 9e7b3178..79dcbab0 100755 --- a/mygpo/data/feeddownloader.py +++ b/mygpo/data/feeddownloader.py @@ -239,8 +239,19 @@ class PodcastUpdater(object): 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 episode_updater.episodes_added == 0: # no episodes, incr factor + podcast.update_interval_factor *= 1.2 + elif episode_updater.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 diff --git a/mygpo/podcasts/migrations/0040_podcast_update_interval_factor.py b/mygpo/podcasts/migrations/0040_podcast_update_interval_factor.py new file mode 100644 index 00000000..57447b2d --- /dev/null +++ b/mygpo/podcasts/migrations/0040_podcast_update_interval_factor.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.5 on 2017-12-03 19:53 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('podcasts', '0039_podcast_search_index_uptodate'), + ] + + operations = [ + migrations.AddField( + model_name='podcast', + name='update_interval_factor', + field=models.FloatField(default=1), + ), + ] diff --git a/mygpo/podcasts/models.py b/mygpo/podcasts/models.py index 7e7ea056..f4596181 100644 --- a/mygpo/podcasts/models.py +++ b/mygpo/podcasts/models.py @@ -341,16 +341,20 @@ class PodcastQuerySet(MergedUUIDQuerySet): def order_by_next_update(self): """ Sort podcasts by next scheduled update """ - NEXTUPDATE = "last_update + (update_interval || ' hours')::INTERVAL" + NEXTUPDATE = ("last_update + (update_interval * " + "update_interval_factor || ' hours')::INTERVAL") q = self.extra(select={'_next_update': NEXTUPDATE}) return q.order_by('_next_update') @property def next_update(self): - return self.last_update + timedelta(hours=self.update_interval) + interval = (timedelta(hours=self.update_interval) * + self.update_interval_factor) + return self.last_update + interval def next_update_between(self, start, end): - NEXTUPDATE_BETWEEN = ("(last_update + (update_interval || " + NEXTUPDATE_BETWEEN = ("(last_update + (update_interval * " + " update_interval_factor || " "' hours')::INTERVAL) BETWEEN %s AND %s") return self.extra( where=[NEXTUPDATE_BETWEEN], params=[start, end] @@ -570,9 +574,15 @@ class Podcast(UUIDModel, TitleModel, DescriptionModel, LinkModel, latest_episode_timestamp = models.DateTimeField(null=True) episode_count = models.PositiveIntegerField(default=0) hub = models.URLField(null=True) + + # Interval between episodes, within a specified range update_interval = models.PositiveSmallIntegerField(null=False, default=DEFAULT_UPDATE_INTERVAL) + # factor to increase update_interval if an update does not find any + # new episodes + update_interval_factor = models.FloatField(default=1) + # "order" value of the most recent episode (will be the highest of all) max_episode_order = models.PositiveIntegerField(null=True, default=None) @@ -698,7 +708,9 @@ class Podcast(UUIDModel, TitleModel, DescriptionModel, LinkModel, @property def next_update(self): - return self.last_update + timedelta(hours=self.update_interval) + interval = (timedelta(hours=self.update_interval) * + self.update_interval_factor) + return self.last_update + interval class EpisodeQuerySet(MergedUUIDQuerySet): -- 2.11.4.GIT