-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_init.py
43 lines (34 loc) · 1.26 KB
/
db_init.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
import os
import mongoengine
from models import Track, Photo, Submission
import config
db = mongoengine.connect(config.db_name)
PICTURES_FOLDER = 'example_pictures'
def clean(string):
return string.lower().strip()
if __name__ == '__main__':
for track_photo_name in sorted(os.listdir(PICTURES_FOLDER)):
path = os.path.join(PICTURES_FOLDER, track_photo_name)
# Photo object
photo = Photo()
track_photo = open(path, 'rb')
photo.file.put(track_photo)
photo.save()
# Track object
artist, title = track_photo_name.split('.')[0].split('_')
# Trying to find a previously created track object
track = None
for t in Track.objects:
if clean(t.name) == clean(title) and clean(t.artist_name) == clean(title):
track = t
break
if track is None:
track = Track.get_from_spotify(artist, title)
if track is None:
print "! Couldn't get info for '{} - {}' ; skipping.".format(artist, title)
continue
track.save()
# Submission
submission = Submission(photo=photo, track=track)
submission.save()
print "* '{} - {}' saved in the db".format(artist, title)