]> git.0d.be Git - panikweb-esperanzah.git/blob - panikweb_esperanzah/management/commands/import_bonnes_ondes.py
adjust animations
[panikweb-esperanzah.git] / panikweb_esperanzah / management / commands / import_bonnes_ondes.py
1 import os
2
3 import textile
4 import yaml
5 from django.core.files import File
6 from django.core.management.base import BaseCommand, CommandError
7 from django.utils.timezone import make_naive, utc
8 from emissions.models import Diffusion, Emission, Episode, SoundFile
9 from pytz import timezone
10
11
12 def construct_ruby_object(loader, suffix, node):
13     return loader.construct_yaml_map(node)
14
15
16 def construct_ruby_sym(loader, node):
17     return loader.construct_yaml_str(node)
18
19
20 yaml.add_multi_constructor("!ruby/object:", construct_ruby_object)
21 yaml.add_constructor("!ruby/sym", construct_ruby_sym)
22
23
24 def get_as_html(text):
25     return textile.textile(text)
26
27
28 class Command(BaseCommand):
29     def add_arguments(self, parser):
30         parser.add_argument('--dirname', metavar='DIRNAME')
31         parser.add_argument('--edition', metavar='EDITION')
32
33     def handle(self, dirname, edition, *args, **kwargs):
34         print(dirname, edition)
35         emission, created = Emission.objects.get_or_create(
36             slug='edition-%s' % edition,
37             defaults={'title': 'Édition %s' % edition, 'text': '<p>Une bien belle année.</p>'},
38         )
39         show = yaml.load(open(os.path.join(dirname, 'show.yaml')))
40         emission.title = show['attributes']['name']
41         emission.text = get_as_html(show['attributes']['description'])
42         emission.archived = True
43         emission.save()
44
45         europe_brussels = timezone('Europe/Brussels')
46         episodes = yaml.load(open(os.path.join(dirname, 'episodes.yaml')))
47         images = {}
48         for image_data in yaml.load(open(os.path.join(dirname, 'images.yaml'))):
49             images[image_data['attributes']['id']] = (
50                 image_data['attributes']['content_uid'].encode('raw_unicode_escape').decode('utf-8')
51             )
52
53         episode_ids = {}
54         for episode_data in episodes:
55             episode, created = Episode.objects.get_or_create(
56                 slug=episode_data['attributes']['slug'],
57                 emission=emission,
58                 defaults={
59                     'title': episode_data['attributes']['title'],
60                 },
61             )
62             episode.diffusions().delete()
63             broadcasted_at = utc.localize(episode_data['attributes']['broadcasted_at']).astimezone(
64                 europe_brussels
65             )
66             Diffusion(episode=episode, datetime=make_naive(broadcasted_at)).save()
67             episode.text = get_as_html(episode_data['attributes']['description'])
68             if not episode.image and episode_data['attributes'].get('image_id'):
69                 episode.image = File(
70                     open(os.path.join(dirname, 'images', images[episode_data['attributes']['image_id']]))
71                 )
72             episode.save()
73             episode_ids[episode_data['attributes']['id']] = episode.id
74
75         soundfiles = {}
76         for soundfile_data in yaml.load(open(os.path.join(dirname, 'contents.yaml'))):
77             audiobank_id = soundfile_data['attributes']['audiobank_id']
78             if audiobank_id:
79                 filepath = os.path.join(dirname, 'casts', '%s.ogg' % audiobank_id)
80                 if not os.path.exists(filepath):
81                     continue
82             audiobank_cast = soundfile_data['attributes'].get('audiobank_cast')
83             if audiobank_cast:
84                 filepath = os.path.join(dirname, 'casts', '%s.ogg' % audiobank_cast)
85                 if not os.path.exists(filepath):
86                     continue
87             episode = episode_ids[soundfile_data['attributes']['episode_id']]
88             soundfile, created = SoundFile.objects.get_or_create(
89                 episode_id=episode,
90                 title=soundfile_data['attributes']['name'],
91                 fragment=not (soundfile_data['attributes']['principal']),
92             )
93             soundfile.podcastable = True
94             soundfile.file = File(open(filepath))
95             soundfile.save()