]> git.0d.be Git - panikweb.git/commitdiff
add command to import from bonnes ondes export
authorFrédéric Péters <fpeters@0d.be>
Sat, 21 Jul 2018 11:42:22 +0000 (13:42 +0200)
committerFrédéric Péters <fpeters@0d.be>
Sat, 21 Jul 2018 11:42:22 +0000 (13:42 +0200)
panikweb/archives/__init__.py [new file with mode: 0644]
panikweb/archives/management/__init__.py [new file with mode: 0644]
panikweb/archives/management/commands/__init__.py [new file with mode: 0644]
panikweb/archives/management/commands/import_bonnes_ondes.py [new file with mode: 0644]
panikweb/settings.py

diff --git a/panikweb/archives/__init__.py b/panikweb/archives/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/panikweb/archives/management/__init__.py b/panikweb/archives/management/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/panikweb/archives/management/commands/__init__.py b/panikweb/archives/management/commands/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/panikweb/archives/management/commands/import_bonnes_ondes.py b/panikweb/archives/management/commands/import_bonnes_ondes.py
new file mode 100644 (file)
index 0000000..4857114
--- /dev/null
@@ -0,0 +1,81 @@
+# coding: utf-8
+
+import os
+import yaml
+import markdown
+from pytz import timezone
+
+from django.core.files import File
+from django.core.management.base import BaseCommand, CommandError
+from django.utils.timezone import make_naive, utc
+
+from emissions.models import Emission, Episode, SoundFile, Diffusion
+
+def construct_ruby_object(loader, suffix, node):
+    return loader.construct_yaml_map(node)
+
+def construct_ruby_sym(loader, node):
+    return loader.construct_yaml_str(node)
+
+yaml.add_multi_constructor(u"!ruby/object:", construct_ruby_object)
+yaml.add_constructor(u"!ruby/sym", construct_ruby_sym)
+
+def get_markdown_as_html(text):
+    return markdown.markdown(text)
+
+class Command(BaseCommand):
+    def add_arguments(self, parser):
+        parser.add_argument('--dirname', metavar='DIRNAME')
+        parser.add_argument('--edition', metavar='EDITION')
+
+    def handle(self, dirname, edition, *args, **kwargs):
+        print dirname, edition
+        emission, created = Emission.objects.get_or_create(
+                slug='edition-%s' % edition,
+                defaults={
+                    'title': u'Édition %s' % edition,
+                    'text': u'<p>Une bien belle année.</p>'
+                })
+        show = yaml.load(open(os.path.join(dirname, 'show.yaml')))
+        emission.title = show['attributes']['name']
+        emission.text = get_markdown_as_html(show['attributes']['description'])
+        emission.archived = True
+        emission.save()
+
+        europe_brussels = timezone('Europe/Brussels')
+        episodes = yaml.load(open(os.path.join(dirname, 'episodes.yaml')))
+        images = {}
+        for image_data in yaml.load(open(os.path.join(dirname, 'images.yaml'))):
+            images[image_data['attributes']['id']] = image_data['attributes']['content_uid']
+
+        episode_ids = {}
+        for episode_data in episodes:
+            episode, created = Episode.objects.get_or_create(
+                    slug=episode_data['attributes']['slug'],
+                    emission=emission,
+                    defaults={
+                        'title': episode_data['attributes']['title'],
+                    })
+            episode.diffusions().delete()
+            broadcasted_at = utc.localize(episode_data['attributes']['broadcasted_at']).astimezone(europe_brussels)
+            Diffusion(episode=episode, datetime=make_naive(broadcasted_at)).save()
+            episode.text = get_markdown_as_html(episode_data['attributes']['description'])
+            if not episode.image and episode_data['attributes'].get('image_id'):
+                episode.image = File(open(os.path.join(dirname, 'images', images[episode_data['attributes']['image_id']])))
+            episode.save()
+            episode_ids[episode_data['attributes']['id']] = episode.id
+
+        soundfiles = {}
+        for soundfile_data in yaml.load(open(os.path.join(dirname, 'contents.yaml'))):
+            audiobank_id = soundfile_data['attributes']['audiobank_id']
+            filepath = os.path.join(dirname, 'casts', '%s.ogg' % audiobank_id)
+            if not os.path.exists(filepath):
+                continue
+            episode = episode_ids[soundfile_data['attributes']['episode_id']]
+            soundfile, created = SoundFile.objects.get_or_create(
+                    episode_id=episode,
+                    title=soundfile_data['attributes']['name'],
+                    fragment=not(soundfile_data['attributes']['principal']))
+            soundfile.podcastable = True
+            soundfile.file = File(open(filepath))
+            soundfile.save()
index e8780711729f7b935cf82679aa5196530e9d435c..88813b8b8fee7bc7b8faaddb6c81d33f0404499d 100644 (file)
@@ -145,6 +145,7 @@ INSTALLED_APPS = (
     'taggit',
     'panikweb_templates',
     'panikweb.paniktags',
+    'panikweb.archives',
     'compressor',
     'sorl.thumbnail',
     'jquery',