]> git.0d.be Git - earwig.git/commitdiff
add management command to grab sounds from channels
authorFrédéric Péters <fpeters@0d.be>
Sat, 1 Sep 2018 08:56:04 +0000 (10:56 +0200)
committerFrédéric Péters <fpeters@0d.be>
Sat, 1 Sep 2018 17:22:48 +0000 (19:22 +0200)
earwig/sounds/management/__init__.py [new file with mode: 0644]
earwig/sounds/management/commands/__init__.py [new file with mode: 0644]
earwig/sounds/management/commands/update_sounds.py [new file with mode: 0644]
earwig/sounds/models.py
earwig/sounds/sources.py [new file with mode: 0644]

diff --git a/earwig/sounds/management/__init__.py b/earwig/sounds/management/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/earwig/sounds/management/commands/__init__.py b/earwig/sounds/management/commands/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/earwig/sounds/management/commands/update_sounds.py b/earwig/sounds/management/commands/update_sounds.py
new file mode 100644 (file)
index 0000000..5ef59dd
--- /dev/null
@@ -0,0 +1,25 @@
+# earwig
+# Copyright (C) 2018  Frederic Peters
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from django.core.management.base import BaseCommand
+
+from ...models import Channel
+
+
+class Command(BaseCommand):
+    def handle(self, *args, **options):
+        for channel in Channel.objects.all():
+            channel.update_sounds()
index 93fe3fe881474aa2a9d54ff12d02fc8516956af4..18b10d4d9c91be633d155ca23bedbea6c8497f41 100644 (file)
@@ -29,6 +29,11 @@ class Channel(models.Model):
     def __str__(self):
         return self.title
 
+    def update_sounds(self):
+        from . import sources
+        source = sources.Rss(self)
+        source.update_sounds()
+
 
 class Sound(models.Model):
     sound_url = models.URLField(_('Sound URL'))
diff --git a/earwig/sounds/sources.py b/earwig/sounds/sources.py
new file mode 100644 (file)
index 0000000..0c2ae74
--- /dev/null
@@ -0,0 +1,55 @@
+# earwig
+# Copyright (C) 2018  Frederic Peters
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import datetime
+
+from django.utils.timezone import make_aware
+import feedparser
+
+from .models import Sound
+
+
+class Rss(object):
+    def __init__(self, channel):
+        self.channel = channel
+        assert channel.feed_url
+
+    def update_sounds(self):
+        feed = feedparser.parse(self.channel.feed_url)
+        for entry in feed.entries:
+            for link in entry.links:
+                if link.rel != 'enclosure':
+                    continue
+                if not link.type.startswith('audio/'):
+                    continue
+                sound_url = link.href
+                sound, created = Sound.objects.get_or_create(uuid=entry.id,
+                        defaults={
+                            'title': entry.title,
+                            'sound_url': sound_url})
+                sound.title = entry.title
+                sound.page_url = entry.link
+                sound.channel = self.channel
+                sound.file_type = link.type
+                sound.original_publication_date = make_aware(datetime.datetime(*entry.published_parsed[:6]))
+                link_length = int(link.length) if link.length else None
+                if link_length != sound.file_size:
+                    sound.file_size = link_length
+                    sound.duration = None  # reset
+                if sound_url != sound.sound_url:
+                    sound.sound_url = sound_url
+                    sound.duration = None  # reset
+                sound.save()