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
12 def construct_ruby_object(loader, suffix, node):
13 return loader.construct_yaml_map(node)
16 def construct_ruby_sym(loader, node):
17 return loader.construct_yaml_str(node)
20 yaml.add_multi_constructor("!ruby/object:", construct_ruby_object)
21 yaml.add_constructor("!ruby/sym", construct_ruby_sym)
24 def get_as_html(text):
25 return textile.textile(text)
28 class Command(BaseCommand):
29 def add_arguments(self, parser):
30 parser.add_argument('--dirname', metavar='DIRNAME')
31 parser.add_argument('--edition', metavar='EDITION')
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>'},
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
45 europe_brussels = timezone('Europe/Brussels')
46 episodes = yaml.load(open(os.path.join(dirname, 'episodes.yaml')))
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')
54 for episode_data in episodes:
55 episode, created = Episode.objects.get_or_create(
56 slug=episode_data['attributes']['slug'],
59 'title': episode_data['attributes']['title'],
62 episode.diffusions().delete()
63 broadcasted_at = utc.localize(episode_data['attributes']['broadcasted_at']).astimezone(
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'):
70 open(os.path.join(dirname, 'images', images[episode_data['attributes']['image_id']]))
73 episode_ids[episode_data['attributes']['id']] = episode.id
76 for soundfile_data in yaml.load(open(os.path.join(dirname, 'contents.yaml'))):
77 audiobank_id = soundfile_data['attributes']['audiobank_id']
79 filepath = os.path.join(dirname, 'casts', '%s.ogg' % audiobank_id)
80 if not os.path.exists(filepath):
82 audiobank_cast = soundfile_data['attributes'].get('audiobank_cast')
84 filepath = os.path.join(dirname, 'casts', '%s.ogg' % audiobank_cast)
85 if not os.path.exists(filepath):
87 episode = episode_ids[soundfile_data['attributes']['episode_id']]
88 soundfile, created = SoundFile.objects.get_or_create(
90 title=soundfile_data['attributes']['name'],
91 fragment=not (soundfile_data['attributes']['principal']),
93 soundfile.podcastable = True
94 soundfile.file = File(open(filepath))