Tweak decoding in import scripts

This commit is contained in:
Daan Sprenkels
2017-11-03 18:31:34 +01:00
parent 1152317490
commit a7fe50b61d
3 changed files with 16 additions and 7 deletions

View File

@ -56,6 +56,10 @@ class Command(BaseCommand):
song=song, user=user, song=song, user=user,
state=2, # already played state=2, # already played
order=0) order=0)
try:
playlist_song.save() playlist_song.save()
except OperationalError as e:
print('Error: {}'.format(e))
continue
print('Done importing history.') print('Done importing history.')

View File

@ -1,6 +1,7 @@
import json import json
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.db.utils import OperationalError
from songs.models import Song from songs.models import Song
TRACKSDB_FORMAT = b'TrackID Artist Title Genre length Filename Filesize UploadedBy UploadedTimestamp Lounge Deleted trackGain trackPeak\n' TRACKSDB_FORMAT = b'TrackID Artist Title Genre length Filename Filesize UploadedBy UploadedTimestamp Lounge Deleted trackGain trackPeak\n'
@ -38,12 +39,16 @@ class Command(BaseCommand):
import_song = line.strip(b'\n').split(b'\t') import_song = line.strip(b'\n').split(b'\t')
user = User.objects.filter(username=import_song[7]).first() user = User.objects.filter(username=import_song[7]).first()
song = Song(user=user, song = Song(user=user,
artist=import_song[1].strip(), artist=import_song[1].decode('utf-8', errors='ignore').strip(),
title=import_song[2].strip(), title=import_song[2].decode('utf-8', errors='ignore').strip(),
duration=float(import_song[4]), duration=float(import_song[4]),
hash=import_song[5].strip(), hash=import_song[5].strip(),
deleted=bool(int(import_song[10])), deleted=bool(int(import_song[10])),
old_id=import_song[0]) old_id=import_song[0])
try:
song.save() song.save()
except OperationalError as e:
print('Error: {}'.format(e))
continue
print('Done importing songs.') print('Done importing songs.')

View File

@ -39,13 +39,13 @@ class Command(BaseCommand):
assert line == USERDB_FORMAT, 'bad 1st line: {}'.format(line) assert line == USERDB_FORMAT, 'bad 1st line: {}'.format(line)
for line in fp: for line in fp:
import_user = line.strip(b'\n').split(b'\t') import_user = line.strip(b'\n').split(b'\t')
username = import_user[1].decode('utf-8', errors='replace').lower().strip() username = import_user[1].decode('utf-8', errors='strict').lower().strip()
user, created = User.objects.get_or_create(username=username) user, created = User.objects.get_or_create(username=username)
user.name = import_user[2].decode('utf-8', errors='replace').strip() user.name = import_user[2].decode('utf-8', errors='ignore').strip()
user.email = user.username + '@science.ru.nl' user.email = user.username + '@science.ru.nl'
user.password = 'md5$$' + import_user[3].decode('utf-8', errors='strict') user.password = 'md5$$' + import_user[3].decode('utf-8', errors='strict')
user.queue = get_first_queue() user.queue = get_first_queue()
user.study = import_user[5].decode('utf-8', errors='replace').strip() user.study = import_user[5].decode('utf-8', errors='ignore').strip()
user.save() user.save()
print('Done importing users.') print('Done importing users.')