1
0
mirror of https://github.com/bspeice/Melodia synced 2024-12-25 08:08:13 -05:00

Refactor sections of adding song metadata

Last commit before beginning web frontend
This commit is contained in:
Bradlee Speice 2013-01-18 21:07:33 -05:00
parent d8b7453260
commit f601f0834a
2 changed files with 59 additions and 42 deletions

View File

@ -146,6 +146,9 @@ INSTALLED_APPS = (
# Melodia apps
'archiver',
#Django management
'django_extensions',
)
# A sample logging configuration. The only tangible logging

View File

@ -29,6 +29,7 @@ class Song (models.Model):
"""
This class defines the fields and functions related to controlling
individual music files.
Note that the Playlist model depends on this model's PK being an int.
"""
#Standard user-populated metadata
@ -50,8 +51,8 @@ class Song (models.Model):
url = models.CharField(max_length = 255)
file_hash = melodia_settings.HASH_RESULT_DB_TYPE
def populate_metadata(self, use_echonest = False):
"Populate the metadata of this song (only if file hash has changed)"
def _file_not_changed(self):
"Make sure the hash for this file is valid - return True if it has not changed."
#Overload the hash function with whatever Melodia as a whole is using
from Melodia.melodia_settings import HASH_FUNCTION as hash
@ -63,18 +64,19 @@ class Song (models.Model):
if current_file_hash == self.file_hash:
#The song data hasn't changed at all, we don't need to do anything
return
return True
#If we've gotten to here, we do actually need to fully update the metadata
if use_echonest:
#Code to grab metadata from echonest here
return False
def _grab_metadata_echonest(self):
"Populate this song's metadata using EchoNest"
pass
else:
#Grab metadata for the database using what is in the track.
def _grab_metadata_local(self):
"Populate this song's metadata using what is locally available"
#It's weird, but we need to wrap importing audiotools
from Melodia.resources import add_resource_dir
add_resource_dir()
import audiotools
try:
@ -108,6 +110,18 @@ class Song (models.Model):
self.duration = self.bit_rate or _default_duration
self.echonest_song_id = self.echonest_song_id or _default_echonest_song_id
def populate_metadata(self, use_echonest = False):
"Populate the metadata of this song (only if file hash has changed), and save the result."
if self._file_not_changed():
return
#If we've gotten to here, we do actually need to fully update the metadata
if use_echonest:
self._grab_echonest()
else:
self._grab_metadata_local()
def convert(self, output_location, output_format, progress_func = lambda x, y: None):
"Convert a song to a new format."
#Note that output_format over-rides the format guessed by output_location