2019-06-03 14:59:18 +00:00
|
|
|
'''
|
|
|
|
Django management command to import old playlist data. This should only be used
|
|
|
|
for seeding a newly created database.
|
|
|
|
'''
|
|
|
|
|
|
|
|
import decimal
|
|
|
|
import json
|
2018-01-04 21:29:59 +00:00
|
|
|
import os
|
2019-06-12 17:51:36 +00:00
|
|
|
import re
|
2018-01-04 21:29:59 +00:00
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2020-03-11 15:58:50 +00:00
|
|
|
from django.utils import timezone
|
2018-01-04 21:29:59 +00:00
|
|
|
|
2019-06-03 14:59:18 +00:00
|
|
|
from core.utils import path_to_iri
|
|
|
|
from radio.models import Album, Artist, Game, Store, Song
|
2018-01-04 21:29:59 +00:00
|
|
|
|
2019-06-03 14:59:18 +00:00
|
|
|
decimal.getcontext().prec = 8
|
2018-01-04 21:29:59 +00:00
|
|
|
|
2020-03-11 15:58:50 +00:00
|
|
|
PROGRESS = '\rAdding {}: [{:>{width}} / {}]'
|
|
|
|
COMPLETED = 'Imported {} {}'
|
|
|
|
|
2018-01-04 21:29:59 +00:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2019-06-03 14:59:18 +00:00
|
|
|
'''Main "importoldreadio" command class'''
|
|
|
|
help = 'Imports the old radio data from an exported playlist'
|
2018-01-04 21:29:59 +00:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2019-06-03 14:59:18 +00:00
|
|
|
parser.add_argument('playlist_file', nargs=1)
|
2018-01-04 21:29:59 +00:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2019-06-03 14:59:18 +00:00
|
|
|
playlist_file = options['playlist_file'][0]
|
|
|
|
if not os.path.isfile(playlist_file):
|
2018-01-04 21:29:59 +00:00
|
|
|
raise CommandError('File does not exist')
|
2019-06-03 14:59:18 +00:00
|
|
|
|
|
|
|
with open(playlist_file, 'r', encoding='utf8') as pfile:
|
|
|
|
playlist = json.load(pfile, parse_float=decimal.Decimal)
|
|
|
|
|
|
|
|
totals = {
|
|
|
|
'albums': 0,
|
|
|
|
'artists': 0,
|
|
|
|
'games': 0,
|
|
|
|
'songs': 0,
|
|
|
|
'jingles': 0
|
|
|
|
}
|
|
|
|
|
|
|
|
# Fetching albums first
|
2020-03-11 15:58:50 +00:00
|
|
|
number = len(playlist['albums']) if 'albums' in playlist else 0
|
|
|
|
self.stdout.write('\n')
|
2019-06-03 14:59:18 +00:00
|
|
|
for album in playlist['albums']:
|
|
|
|
Album.objects.create(title=album['title'],
|
|
|
|
disabled=album['disabled'])
|
2019-06-03 19:02:43 +00:00
|
|
|
totals['albums'] += 1
|
2020-03-11 15:58:50 +00:00
|
|
|
self.stdout.write(PROGRESS.format(
|
|
|
|
'albums',
|
|
|
|
totals['albums'],
|
|
|
|
number,
|
|
|
|
width=len(str(number))
|
|
|
|
), ending='')
|
|
|
|
self.stdout.flush()
|
|
|
|
self.stdout.write('\n')
|
|
|
|
self.stdout.write(COMPLETED.format(str(totals['albums']), 'albums'))
|
2019-06-03 14:59:18 +00:00
|
|
|
|
|
|
|
# Next up, artists
|
2020-03-11 15:58:50 +00:00
|
|
|
number = len(playlist['artists']) if 'artists' in playlist else 0
|
|
|
|
self.stdout.write('\n')
|
2019-06-03 14:59:18 +00:00
|
|
|
for artist in playlist['artists']:
|
|
|
|
Artist.objects.create(alias=artist['alias'] or '',
|
|
|
|
first_name=artist['first_name'] or '',
|
|
|
|
last_name=artist['last_name'] or '',
|
|
|
|
disabled=artist['disabled'])
|
2019-06-03 19:02:43 +00:00
|
|
|
totals['artists'] += 1
|
2020-03-11 15:58:50 +00:00
|
|
|
self.stdout.write(PROGRESS.format(
|
|
|
|
'artists',
|
|
|
|
totals['artists'],
|
|
|
|
number,
|
|
|
|
width=len(str(number))
|
|
|
|
), ending='')
|
|
|
|
self.stdout.flush()
|
|
|
|
self.stdout.write('\n')
|
|
|
|
self.stdout.write(COMPLETED.format(str(totals['artists']), 'artists'))
|
2019-06-03 14:59:18 +00:00
|
|
|
|
|
|
|
# On to games
|
2020-03-11 15:58:50 +00:00
|
|
|
number = len(playlist['games']) if 'games' in playlist else 0
|
|
|
|
self.stdout.write('\n')
|
2019-06-03 14:59:18 +00:00
|
|
|
for game in playlist['games']:
|
|
|
|
Game.objects.create(title=game['title'],
|
|
|
|
disabled=game['disabled'])
|
2019-06-03 19:02:43 +00:00
|
|
|
totals['games'] += 1
|
2020-03-11 15:58:50 +00:00
|
|
|
self.stdout.write(PROGRESS.format(
|
|
|
|
'games',
|
|
|
|
totals['games'],
|
|
|
|
number,
|
|
|
|
width=len(str(number))
|
|
|
|
), ending='')
|
|
|
|
self.stdout.flush()
|
|
|
|
self.stdout.write('\n')
|
|
|
|
self.stdout.write(COMPLETED.format(str(totals['games']), 'games'))
|
2019-06-03 14:59:18 +00:00
|
|
|
|
|
|
|
# Followed by the songs
|
2020-03-11 15:58:50 +00:00
|
|
|
number = len(playlist['songs']) if 'songs' in playlist else 0
|
|
|
|
progress = '\rAdding requestables: [{:>{width}} / {}]'
|
|
|
|
self.stdout.write('\n')
|
2019-06-03 14:59:18 +00:00
|
|
|
for song in playlist['songs']:
|
|
|
|
try:
|
|
|
|
album = Album.objects.get(title__exact=song['album'])
|
|
|
|
except Album.DoesNotExist:
|
|
|
|
album = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
game = Game.objects.get(title__exact=song['game'])
|
|
|
|
except Game.DoesNotExist:
|
|
|
|
game = None
|
|
|
|
|
|
|
|
new_song = Song.objects.create(album=album,
|
|
|
|
game=game,
|
|
|
|
disabled=song['disabled'],
|
|
|
|
song_type=song['type'],
|
|
|
|
title=song['title'])
|
|
|
|
|
|
|
|
for artist in song['artists']:
|
|
|
|
new_artist = Artist.objects.get(
|
|
|
|
alias__exact=artist['alias'] or '',
|
|
|
|
first_name__exact=artist['first_name'] or '',
|
|
|
|
last_name__exact=artist['last_name'] or ''
|
2018-01-04 21:29:59 +00:00
|
|
|
)
|
2019-06-03 14:59:18 +00:00
|
|
|
new_song.artists.add(new_artist)
|
2018-01-04 21:29:59 +00:00
|
|
|
|
2019-06-12 17:51:36 +00:00
|
|
|
localfile = re.match(
|
|
|
|
r'^(?:(?:[A-Za-z]:|\\)\\|\/)',
|
|
|
|
song['store']['path']
|
|
|
|
)
|
|
|
|
|
|
|
|
if localfile:
|
|
|
|
iri = path_to_iri(song['store']['path'])
|
|
|
|
else:
|
|
|
|
iri = song['store']['path']
|
|
|
|
|
2019-07-03 15:57:46 +00:00
|
|
|
if song['store']['track_gain']:
|
|
|
|
gain_str = re.sub(r'[dB\+ ]', '', song['store']['track_gain'])
|
|
|
|
gain = decimal.Decimal(gain_str)
|
|
|
|
else:
|
|
|
|
gain = None
|
|
|
|
|
|
|
|
if song['store']['track_peak']:
|
|
|
|
peak = decimal.Decimal(song['store']['track_peak'])
|
|
|
|
else:
|
|
|
|
peak = None
|
|
|
|
|
2019-06-03 14:59:18 +00:00
|
|
|
new_store = Store.objects.create(
|
2019-06-12 17:51:36 +00:00
|
|
|
iri=iri,
|
2019-06-03 14:59:18 +00:00
|
|
|
mime_type=song['store']['mime'],
|
|
|
|
file_size=song['store']['filesize'],
|
2019-07-03 15:57:46 +00:00
|
|
|
length=song['store']['length'],
|
|
|
|
track_gain=gain,
|
|
|
|
track_peak=peak
|
2019-06-03 14:59:18 +00:00
|
|
|
)
|
|
|
|
new_song.stores.add(new_store)
|
2019-06-04 16:22:58 +00:00
|
|
|
new_song.active_store = new_store
|
2019-06-03 14:59:18 +00:00
|
|
|
new_song.save()
|
|
|
|
if song['type'] == 'S':
|
|
|
|
totals['songs'] += 1
|
2018-01-04 21:29:59 +00:00
|
|
|
else:
|
2019-06-03 14:59:18 +00:00
|
|
|
totals['jingles'] += 1
|
|
|
|
|
2020-03-11 15:58:50 +00:00
|
|
|
self.stdout.write(PROGRESS.format(
|
|
|
|
'requestables',
|
|
|
|
totals['songs'] + totals['jingles'],
|
|
|
|
number,
|
|
|
|
width=len(str(number))
|
|
|
|
), ending='')
|
|
|
|
self.stdout.flush()
|
|
|
|
self.stdout.write('\n')
|
|
|
|
self.stdout.write(COMPLETED.format(
|
|
|
|
str(totals['songs'] + totals['jingles']),
|
|
|
|
'requestables ({} songs, {} jingles)'.format(
|
2019-06-03 14:59:18 +00:00
|
|
|
str(totals['songs']),
|
|
|
|
str(totals['jingles'])
|
|
|
|
)
|
2020-03-11 15:58:50 +00:00
|
|
|
))
|
2019-06-03 14:59:18 +00:00
|
|
|
|
|
|
|
pub = input('Do you want to publish all imported objects as well? '
|
|
|
|
'[Y/N] ')
|
|
|
|
|
|
|
|
if pub in ('Y', 'y'):
|
2020-03-11 15:58:50 +00:00
|
|
|
date = timezone.now()
|
|
|
|
Album.objects.all().update(published_date=date)
|
|
|
|
Artist.objects.all().update(published_date=date)
|
|
|
|
Game.objects.all().update(published_date=date)
|
|
|
|
Song.objects.all().update(published_date=date)
|
|
|
|
|
2019-06-03 14:59:18 +00:00
|
|
|
self.stdout.write('Published imported objects successfully')
|
|
|
|
else:
|
|
|
|
self.stdout.write('Skipped publishing songs')
|