mirror of
				https://github.com/bspeice/Melodia
				synced 2025-11-03 18:00:50 -05:00 
			
		
		
		
	Refactor sections of adding song metadata
Last commit before beginning web frontend
This commit is contained in:
		@ -146,6 +146,9 @@ INSTALLED_APPS = (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	# Melodia apps
 | 
						# Melodia apps
 | 
				
			||||||
	'archiver',
 | 
						'archiver',
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						#Django management
 | 
				
			||||||
 | 
						'django_extensions',
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# A sample logging configuration. The only tangible logging
 | 
					# A sample logging configuration. The only tangible logging
 | 
				
			||||||
 | 
				
			|||||||
@ -29,6 +29,7 @@ class Song (models.Model):
 | 
				
			|||||||
	"""
 | 
						"""
 | 
				
			||||||
	This class defines the fields and functions related to controlling
 | 
						This class defines the fields and functions related to controlling
 | 
				
			||||||
	individual music files.
 | 
						individual music files.
 | 
				
			||||||
 | 
						Note that the Playlist model depends on this model's PK being an int.
 | 
				
			||||||
	"""
 | 
						"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#Standard user-populated metadata
 | 
						#Standard user-populated metadata
 | 
				
			||||||
@ -50,8 +51,8 @@ class Song (models.Model):
 | 
				
			|||||||
	url              = models.CharField(max_length = 255)
 | 
						url              = models.CharField(max_length = 255)
 | 
				
			||||||
	file_hash        = melodia_settings.HASH_RESULT_DB_TYPE
 | 
						file_hash        = melodia_settings.HASH_RESULT_DB_TYPE
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	def populate_metadata(self, use_echonest = False):
 | 
						def _file_not_changed(self):
 | 
				
			||||||
		"Populate the metadata of this song (only if file hash has changed)"
 | 
							"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
 | 
							#Overload the hash function with whatever Melodia as a whole is using
 | 
				
			||||||
		from Melodia.melodia_settings import HASH_FUNCTION as hash
 | 
							from Melodia.melodia_settings import HASH_FUNCTION as hash
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -63,18 +64,19 @@ class Song (models.Model):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			if current_file_hash == self.file_hash:
 | 
								if current_file_hash == self.file_hash:
 | 
				
			||||||
				#The song data hasn't changed at all, we don't need to do anything
 | 
									#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
 | 
							return False
 | 
				
			||||||
		if use_echonest:
 | 
					
 | 
				
			||||||
			#Code to grab metadata from echonest here
 | 
						def _grab_metadata_echonest(self):
 | 
				
			||||||
 | 
							"Populate this song's metadata using EchoNest"
 | 
				
			||||||
		pass
 | 
							pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		else:
 | 
						def _grab_metadata_local(self):
 | 
				
			||||||
			#Grab metadata for the database using what is in the track.
 | 
							"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
 | 
							from Melodia.resources import add_resource_dir
 | 
				
			||||||
		add_resource_dir()
 | 
							add_resource_dir()
 | 
				
			||||||
 | 
					 | 
				
			||||||
		import audiotools
 | 
							import audiotools
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		try:
 | 
							try:
 | 
				
			||||||
@ -108,6 +110,18 @@ class Song (models.Model):
 | 
				
			|||||||
			self.duration         = self.bit_rate         or _default_duration
 | 
								self.duration         = self.bit_rate         or _default_duration
 | 
				
			||||||
			self.echonest_song_id = self.echonest_song_id or _default_echonest_song_id
 | 
								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):
 | 
						def convert(self, output_location, output_format, progress_func = lambda x, y: None):
 | 
				
			||||||
		"Convert a song to a new format."
 | 
							"Convert a song to a new format."
 | 
				
			||||||
		#Note that output_format over-rides the format guessed by output_location
 | 
							#Note that output_format over-rides the format guessed by output_location
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user