]> git.0d.be Git - earwig.git/commitdiff
add autodiscovery of some channel metadata
authorFrédéric Péters <fpeters@0d.be>
Sat, 1 Sep 2018 15:45:55 +0000 (17:45 +0200)
committerFrédéric Péters <fpeters@0d.be>
Sat, 1 Sep 2018 17:22:48 +0000 (19:22 +0200)
earwig/sounds/management/commands/update_sounds.py
earwig/sounds/migrations/0003_auto_20180901_1745.py [new file with mode: 0644]
earwig/sounds/models.py
earwig/sounds/sources.py

index 5ef59dd4d86b3e583ad7726cb881768f20596cee..5f212369ac3be785b57d9564c8ca9c0f57f818de 100644 (file)
@@ -22,4 +22,4 @@ from ...models import Channel
 class Command(BaseCommand):
     def handle(self, *args, **options):
         for channel in Channel.objects.all():
-            channel.update_sounds()
+            channel.update()
diff --git a/earwig/sounds/migrations/0003_auto_20180901_1745.py b/earwig/sounds/migrations/0003_auto_20180901_1745.py
new file mode 100644 (file)
index 0000000..8921192
--- /dev/null
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.11 on 2018-09-01 15:45
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('sounds', '0002_auto_20180901_1521'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='channel',
+            name='image_url',
+            field=models.URLField(blank=True, null=True, verbose_name='Image URL'),
+        ),
+        migrations.AddField(
+            model_name='channel',
+            name='license_url',
+            field=models.URLField(blank=True, null=True, verbose_name='License URL'),
+        ),
+        migrations.AddField(
+            model_name='channel',
+            name='subtitle',
+            field=models.CharField(blank=True, max_length=500, null=True, verbose_name='Subtitle'),
+        ),
+    ]
index c9a678da5611d130829c2710a3392c82a16c3c5d..1ea8f46501299525dae13d2a76ff997c748668fe 100644 (file)
@@ -37,6 +37,10 @@ class Channel(models.Model):
     policy = models.CharField(_('Policy'), max_length=20,
             default='', blank=True, choices=POLICIES)
 
+    subtitle = models.CharField(_('Subtitle'), max_length=500, blank=True, null=True)
+    image_url = models.URLField(_('Image URL'), blank=True, null=True)
+    license_url = models.URLField(_('License URL'), blank=True, null=True)
+
     creation_timestamp = models.DateTimeField(auto_now_add=True)
     last_update_timestamp = models.DateTimeField(auto_now=True)
 
@@ -46,9 +50,10 @@ class Channel(models.Model):
     def __str__(self):
         return self.title
 
-    def update_sounds(self):
+    def update(self):
         from . import sources
         source = sources.Rss(self)
+        source.update_meta()
         source.update_sounds()
 
 
index 9a864f05782ddbd0a95371bc26964b46e3df2cb3..a296f0c94ee32e23f5a668b33c8ec7845f18ef80 100644 (file)
@@ -27,6 +27,21 @@ class Rss(object):
         self.channel = channel
         assert channel.feed_url
 
+    def update_meta(self):
+        feed = feedparser.parse(self.channel.feed_url)
+        attrs = {
+            'subtitle': feed.feed.subtitle,
+            'image_url': feed.feed.image.href if feed.feed.image and feed.feed.image.href else None,
+            'license_url': getattr(feed.feed, 'license', None),
+        }
+        modified = False
+        for key, value in attrs.items():
+            if getattr(self.channel, key) != value:
+                setattr(self.channel, key, value)
+                modified = True
+        if modified:
+            self.channel.save()
+
     def update_sounds(self):
         feed = feedparser.parse(self.channel.feed_url)
         for entry in feed.entries: