Ejemplo n.º 1
0
def import_file(filename, mutagen_options):
    """Import the song referred to by filename into the library

    Arguments:
        filename - the filename of the audio file to import
        mutagen_options - list of options needed by mutagen.File
    """

    try:
        if filename.rsplit('.')[-1].lower() == 'wma':
            info = get_wma_info(filename)
        else:
            info = get_song_info(filename, mutagen_options)
    except ValueError as e:
        handle_import_error(filename, e)
        return

    artist_name = titlecase(info.artist)
    artist_path = os.path.join(artist_name[0].upper(), artist_name)
    artist, created = Artist.objects.get_or_create(name=artist_name,
                                                   filepath=artist_path)

    album_path = os.path.join(artist_path, titlecase(info.album))
    album, created = Album.objects.get_or_create(
        title=titlecase(info.album),
        artist=artist,
        filepath=album_path)

    if created:
        cover_img = get_cover_art(filename, info.cover_data)
        album.cover_file_type = 'jpg'   # FIXME
        album.cover.save(album.cover.name, cover_img)

    filetype = filename.rsplit('.')[-1].lower()
    original_path = filename.replace(settings.DROPBOX, '', 1
                           ).lstrip(os.path.sep)

    #TODO: Handle Unknown Title by Unknown Artist
    try:
        song, created = Song.objects.get_or_create(
            title=info.title,
            album=album,
            track=info.track,
            defaults={'filefield': File(open(filename, 'rb')),
                      'bitrate': info.bitrate,
                      'filetype': filetype,
                      'original_path': original_path,
                      'first_save': True})
    except IntegrityError as e:
        handle_import_error(filename, e)
        return

    if not created:
        # Song already exists, keep only if better bitrate
        if song.bitrate >= info.bitrate:
            #FIXME: uncomment when Unknown songs are handled correctly
            #os.remove(filename)
            handle_import_error(
                filename,
                '%s by %s already exists' % (info.title, info.artist))
            return
        else:
            song.bitrate = info.bitrate
            song.filefield = File(open(filename, 'rb'))
            song.original_path = original_path
            song.first_save = True
            song.save()

    os.remove(filename)
Ejemplo n.º 2
0
 def test_titlecase(self):
     self.assertEqual(titlecase("spam and eggs"), "Spam And Eggs")
     self.assertEqual(titlecase("Spam And Eggs"), "Spam And Eggs")
     self.assertEqual(titlecase("spam'n eggs"), "Spam'n Eggs")
     self.assertEqual(titlecase("spàm é ôeufs"), "Spàm É Ôeufs")