-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup_app.py
executable file
·109 lines (94 loc) · 3.13 KB
/
setup_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
import csv
import os
import subprocess
from datetime import datetime
from database import drop_database
from database import setup_database
from ptm.models.base import session
from ptm.models.music import Artist
from ptm.models.music import Song
from ptm.models.music import SongMeta
from config import db
corpus_location = 'http://www.reynolds-theatre.com/content/music_repo.tar'
def csv_import():
"""
Import the song metadata from song_dict.csv into the database.
"""
print "Importing CSV song data into the database..."
csvfile = open('song_dict.csv', 'r')
csvreader = csv.reader(csvfile, delimiter=',', quotechar = '\"')
artist_dict = {}
for title, artist_name, bpm, duration, filename in csvreader:
# Create or fetch artist
artist_name = artist_name.decode('utf-8')
artist = artist_dict.get(artist_name)
if not artist:
# Create new artist
artist = Artist(name=artist_name)
# Add artists to the DB session and flush to generate their
# primary key IDs
session.add(artist)
session.flush()
# Remember the artist via the artists dict
artist_dict[artist_name] = artist
# Create songs
song = Song(
filename = os.path.abspath(filename.decode('utf-8')),
title = title.decode('utf-8'),
date_added = datetime.utcnow(),
artist = artist, # Assign the artist object directly
)
# Add songs to the DB session and flush to generate their
# primary key IDs as well as populating the artist_id field
print "Adding song %s..." % song
session.add(song)
session.flush()
# Create Metadata
meta = SongMeta(
duration = int(float(duration)),
bpm = int(bpm),
song = song,
)
session.add(meta)
session.flush()
session.commit()
print "CSV import finished successfully."
def download_corpus():
corpus_path = './corpus'
if not os.path.exists(corpus_path):
# Create the corpus dir
os.mkdir(corpus_path, 0755)
print "Downloading corpus..."
if not os.path.exists('./corpus/music_repo.tar'):
# Download the music corpus from James's cheap DigitalOcean VPS that
# he's hosting it on
subprocess.check_call([
'wget',
corpus_location,
'-P',
corpus_path,
])
else:
print "Corpus already downloaded, skipping."
print "Unzipping songs..."
if len(os.listdir(corpus_path)) > 1:
print "Songs already unzipped, skipping."
else:
subprocess.check_call([
'tar',
'-xzf',
'./corpus/music_repo.tar',
'-C',
corpus_path,
])
print "Successfully unzipped songs."
if __name__ == '__main__':
if os.path.exists(db['path']):
print "Dropping existing database..."
drop_database()
print "Setting up the database..."
setup_database()
print "Finished database setup.\n"
download_corpus()
csv_import()