def retag_track(track): tags = scan_file(track.file_path).tags mapper = TAG_MAPPERS[tags.__class__] tags.clear() for tag, name in mapper.items(): if name == 'artist': tags[tag] = track.artist.name elif name == 'title': tags[tag] = track.name elif name == 'album': tags[tag] = track.album.name elif name == 'genre': tags[tag] = track.genre.name tags.save(track.file_path)
def create_track(file_path, owner, copy=None): from juque.library.models import Track if copy is None: copy = settings.JUQUE_COPY_SOURCE file_size = os.path.getsize(file_path) meta = scan_file(file_path) if not meta or not meta.tags: logger.warning('No tags found in %s; Skipping.', file_path) return tags = map_tags(meta.tags) # Sometimes the track number is a number, sometimes it's a string like "2 / 13" track_number = 0 if 'track' in tags: if tags['track'].isdigit(): track_number = int(tags['track']) elif '/' in tags['track']: try: track_number = int(tags['track'].split('/')[0]) except: pass track = Track.objects.create( owner=owner, name=tags.get('title', os.path.basename(file_path)), length=meta.info.length, bitrate=meta.info.bitrate, sample_rate=meta.info.sample_rate, artist_name=tags.get('artist', '').strip(), album_name=tags.get('album', '').strip(), genre_name=tags.get('genre', '').strip(), lyrics=tags.get('lyrics', '').strip(), notes=tags.get('notes', '').strip(), track_number=track_number, file_path=file_path, file_size=file_size, file_managed=copy, file_type=meta.mime[0], ) # If we're copying the track, update the file_path to the copied path. if copy: track.file_path = library_storage.save(track.get_expected_path(), File(open(file_path, 'rb'))) track.save(check_relations=False) # If the album doesn't have any artwork yet, try to pull it out of the tags. if track.album and not track.album.artwork_path: try: track.album.artwork_type, data = extract_artwork(meta.tags) track.album.artwork_path = library_storage.save(track.album.get_expected_path(), ContentFile(data)) track.album.save() except: pass return track