Commit 030b676f authored by fred's avatar fred

load-from-drupal-json: adapt emission slug and do not duplicate soundfile

parent 22e81694
import datetime
import isodate
import json
import os
import requests
from django.conf import settings
......@@ -8,6 +9,7 @@ from django.core.files import File
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.core.management.base import BaseCommand, CommandError
from django.utils.six import BytesIO
from django.utils.text import slugify
from django.utils.timezone import make_naive
......@@ -19,6 +21,16 @@ class Command(BaseCommand):
parser.add_argument('filename', metavar='FILENAME', type=str,
help='name of file to import')
def adjust_slug(self, title):
slug = slugify(title.replace("'", " ").replace("’", " "))
if slug == 'quand-la-musique-est-bof':
slug = 'quand-la-musique-est-b-o-f'
elif slug == 'trad53-transmission-directe':
slug = 'trad-53-transmission-directe'
elif slug == 'tranzistor':
slug = 'tranzistor-l-emission'
return slug.strip('-')
def handle(self, filename, verbosity, **options):
self.verbose = (verbosity > 1)
for episode_data in json.load(open(filename)).get('episodes').values():
......@@ -28,10 +40,12 @@ class Command(BaseCommand):
continue
if not episode_data.get('text') and not episode_data.get('sound'):
continue
emission_slug = self.adjust_slug(episode_data['emission'])
emission, created = Emission.objects.get_or_create(
slug=slugify(episode_data['emission']),
slug=emission_slug,
defaults={'title': episode_data['emission']})
if created:
emission.archived = True
emission.save()
episode, created = Episode.objects.get_or_create(
slug=episode_data['path'].split('/')[-1],
......@@ -80,16 +94,15 @@ class Command(BaseCommand):
diffusion.datetime = start_datetime
diffusion.save()
if episode_data.get('sound') and not episode.has_sound():
orig_path = 'media/sounds.orig/%s/%s' % (emission.slug, episode_data.get('sound').split('/')[-1])
if not default_storage.exists(orig_path):
orig_path = os.path.join(
settings.MEDIA_ROOT,
'sounds.orig/%s/%s' % (emission.slug, episode_data.get('sound').split('/')[-1]))
if not os.path.exists(orig_path):
response = requests.get(episode_data.get('sound'))
if response.status_code == 200:
path = default_storage.save(
'sounds.orig/%s/%s' % (
emission.slug,
episode_data.get('sound').split('/')[-1]),
ContentFile(response.content))
bio = BytesIO(response.content)
soundfile = SoundFile(episode=episode, podcastable=True,
fragment=False, title=episode.title)
soundfile.file = default_storage.open(path)
soundfile.file = File(bio)
soundfile.file.name = episode_data.get('sound').split('/')[-1]
soundfile.save()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment