--- /dev/null
+# 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()
--- /dev/null
+# 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()