]> git.0d.be Git - panikweb-esperanzah.git/blob - panikweb/archives/management/commands/import_bonnes_ondes.py
bonnes-ondes: switch importer to textile for formatted texts
[panikweb-esperanzah.git] / panikweb / archives / management / commands / import_bonnes_ondes.py
1 # coding: utf-8
2
3 import os
4 import yaml
5 import textile
6 from pytz import timezone
7
8 from django.core.files import File
9 from django.core.management.base import BaseCommand, CommandError
10 from django.utils.timezone import make_naive, utc
11
12 from emissions.models import Emission, Episode, SoundFile, Diffusion
13
14 def construct_ruby_object(loader, suffix, node):
15     return loader.construct_yaml_map(node)
16
17 def construct_ruby_sym(loader, node):
18     return loader.construct_yaml_str(node)
19
20 yaml.add_multi_constructor(u"!ruby/object:", construct_ruby_object)
21 yaml.add_constructor(u"!ruby/sym", construct_ruby_sym)
22
23 def get_as_html(text):
24     return textile.textile(text)
25
26 class Command(BaseCommand):
27     def add_arguments(self, parser):
28         parser.add_argument('--dirname', metavar='DIRNAME')
29         parser.add_argument('--edition', metavar='EDITION')
30
31     def handle(self, dirname, edition, *args, **kwargs):
32         print dirname, edition
33         emission, created = Emission.objects.get_or_create(
34                 slug='edition-%s' % edition,
35                 defaults={
36                     'title': u'Édition %s' % edition,
37                     'text': u'<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']] = image_data['attributes']['content_uid']
50
51         episode_ids = {}
52         for episode_data in episodes:
53             episode, created = Episode.objects.get_or_create(
54                     slug=episode_data['attributes']['slug'],
55                     emission=emission,
56                     defaults={
57                         'title': episode_data['attributes']['title'],
58                     })
59             episode.diffusions().delete()
60             broadcasted_at = utc.localize(episode_data['attributes']['broadcasted_at']).astimezone(europe_brussels)
61             Diffusion(episode=episode, datetime=make_naive(broadcasted_at)).save()
62             episode.text = get_as_html(episode_data['attributes']['description'])
63             if not episode.image and episode_data['attributes'].get('image_id'):
64                 episode.image = File(open(os.path.join(dirname, 'images', images[episode_data['attributes']['image_id']])))
65             episode.save()
66             episode_ids[episode_data['attributes']['id']] = episode.id
67
68         soundfiles = {}
69         for soundfile_data in yaml.load(open(os.path.join(dirname, 'contents.yaml'))):
70             audiobank_id = soundfile_data['attributes']['audiobank_id']
71             filepath = os.path.join(dirname, 'casts', '%s.ogg' % audiobank_id)
72             if not os.path.exists(filepath):
73                 continue
74             episode = episode_ids[soundfile_data['attributes']['episode_id']]
75             soundfile, created = SoundFile.objects.get_or_create(
76                     episode_id=episode,
77                     title=soundfile_data['attributes']['name'],
78                     fragment=not(soundfile_data['attributes']['principal']))
79             soundfile.podcastable = True
80             soundfile.file = File(open(filepath))
81             soundfile.save()