Commit f988e490 authored by Brian Guadalupe's avatar Brian Guadalupe

Fix populate songs script, add time remaining info

parent 39588f24
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# run in Django shell ONLY!!! # run in Django shell ONLY!!!
# python manage.py shell # python manage.py shell
# >>> exec(open('./SQL/populate_songs.py').read()) # copy-paste everything then
# >>> run()
import time
from core.models import * from core.models import *
def extract_song_title(s): def extract_song_title(s):
...@@ -20,13 +21,59 @@ def extract_song_title(s): ...@@ -20,13 +21,59 @@ def extract_song_title(s):
def prettify_genre(g): def prettify_genre(g):
return ', '.join([h.replace('_',' ') for h in g.split()]) return ', '.join([h.replace('_',' ') for h in g.split()])
for i in Song.objects.filter(song_name__startswith="."): def parse_raw(n_albums):
songlst = extract_song_title(i.song_name) idx = 1
for e in songlst: total_time = 0
Song.objects.create(song_name=e, genre=prettify_genre(i.genre), artist=i.artist, album=i.album) for i in Song.objects.filter(song_name__startswith="."):
# uncomment the next line if you want to delete the original entry after parsing print('Processing entry %d of %d...' % (idx, n_albums))
i.delete() 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 def run():
# for song in Song.objects.exclude(song_name__startswith="."): print('Populate songs')
# song.delete() qu = Song.objects.filter(song_name__startswith=".")
\ No newline at end of file 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