Commit f988e490 authored by Brian Guadalupe's avatar Brian Guadalupe

Fix populate songs script, add time remaining info

parent 39588f24
# -*- coding: utf-8 -*-
# run in Django shell ONLY!!!
# python manage.py shell
# >>> exec(open('./SQL/populate_songs.py').read())
# copy-paste everything then
# >>> run()
import time
from core.models import *
def extract_song_title(s):
......@@ -20,13 +21,59 @@ def extract_song_title(s):
def prettify_genre(g):
return ', '.join([h.replace('_',' ') for h in g.split()])
for i in Song.objects.filter(song_name__startswith="."):
songlst = extract_song_title(i.song_name)
for e in songlst:
Song.objects.create(song_name=e, genre=prettify_genre(i.genre), artist=i.artist, album=i.album)
# uncomment the next line if you want to delete the original entry after parsing
i.delete()
def parse_raw(n_albums):
idx = 1
total_time = 0
for i in Song.objects.filter(song_name__startswith="."):
print('Processing entry %d of %d...' % (idx, n_albums))
t0 = time.time()
songlst = extract_song_title(i.song_name)
for e in songlst:
# print(' FOUND: ' + e)
Song.objects.create(song_name=e, genre=prettify_genre(i.genre), artist=i.artist, album=i.album)
tf = time.time()
delta = tf-t0
total_time += delta
# time remaining = (avg time per album)*(# remaining albums)
trem = (total_time/idx)*(n_albums-idx)
print('[INFO] Parsing entry %d took %.3fs' % (idx, delta))
print('[INFO] Approx. time remaining: %d:%02d' % (trem//60, int(trem)%60))
idx += 1
def cleanup(n_albums):
print('Deleting original raw entries...')
didx = 1
for i in Song.objects.filter(song_name__startswith="."):
print('Deleting raw entry %d of %d...' % (didx, n_albums))
i.delete()
didx += 1
# if something messed up above, run this
def revert():
rm = len(Song.objects.exclude(song_name__startswith="."))
ridx = 1
ttime = 0
for song in Song.objects.exclude(song_name__startswith="."):
t0 = time.time()
song.delete()
tf = time.time()
delta = tf-t0
ttime += delta
trem = (ttime/ridx)*(rm-ridx)
print('[INFO] Approx. time remaining: %d:%02d' % (trem//60, int(trem)%60))
ridx += 1
def purge_all():
resp = input('[WARN] Delete ALL entries? [y/N] ')
if resp == 'y':
for song in Song.objects.all():
song.delete()
print('[INFO] All entries deleted!')
# if something messed up above, uncomment these lines
# for song in Song.objects.exclude(song_name__startswith="."):
# song.delete()
\ No newline at end of file
def run():
print('Populate songs')
qu = Song.objects.filter(song_name__startswith=".")
n_albums = len(qu)
print('Found %d entries yet to be parsed' % n_albums)
parse_raw(n_albums)
cleanup(n_albums)
\ No newline at end of file
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