Exemplo n.º 1
0
def _process_data_album_dict(hm,
                             lastfm,
                             alb_name,
                             a_name,
                             do_whichone,
                             mi=None,
                             process_to_end=True):
    assert (
        do_whichone in ('GRACENOTE', 'LASTFM', 'MUSICBRAINZ')
    ), "error, metadata service = %s should be one of GRACENOTE, LASTFM, or MUSICBRAINZ" % do_whichone
    if do_whichone == 'GRACENOTE':
        album_data_dict, status = hm.get_music_metadatas_album(
            a_name, alb_name)
        if status == 'SUCCESS': return album_data_dict, status
        if not_process_to_end:
            return return_error_raw(status)

        #
        ## now lastfm
        album_data_dict, status = lastfm.get_music_metadatas_album(
            a_name, alb_name)
        if status == 'SUCCESS': return album_data_dict, status

        #
        ## now musicbrainz
        if mi is not None:
            assert (mi.artist_name == a_name)
        else:
            mi = music.MusicInfo(a_name)
        album_data_dict, status = mi.get_music_metadatas_album(alb_name)
        if status == 'SUCCESS': return album_data_dict, status
        return return_error_raw(status)
    elif do_whichone == 'LASTFM':
        album_data_dict, status = lastfm.get_music_metadatas_album(
            a_name, alb_name)
        if status == 'SUCCESS': return album_data_dict, status
        if not process_to_end: return return_error_raw(status)

        #
        ## now musicbrainz
        if mi is not None:
            assert (mi.artist_name == a_name)
        else:
            mi = music.MusicInfo(a_name)
        album_data_dict, status = mi.get_music_metadatas_album(alb_name)
        if status == 'SUCCESS': return album_data_dict, status
        return return_error_raw(status)
    else:
        if mi is not None:
            assert (mi.artist_name == a_name)
        else:
            mi = music.MusicInfo(a_name)
        album_data_dict, status = mi.get_music_metadatas_album(alb_name)
        if status == 'SUCCESS': return album_data_dict, status
        return return_error_raw(status)
Exemplo n.º 2
0
def _download_songs_newformat(args):
    assert (args.song_names is not None)
    assert (args.artist_names is not None)
    assert (len(
        list(
            filter(lambda tok: tok is True,
                   (args.do_lastfm, args.do_musicbrainz)))) <= 1)
    #
    ## first get the music metadata
    hm = music.HowdyMusic(verify=args.do_verify)
    lastfm = music.HowdyLastFM(verify=args.do_verify)
    do_whichone = 'GRACENOTE'
    if args.do_lastfm: do_whichone = 'LASTFM'
    if args.do_musicbrainz: do_whichone = 'MUSICBRAINZ'
    song_names = list(
        map(lambda song_name: song_name.strip(), args.song_names.split(';')))
    artist_names = list(
        map(lambda artist_name: artist_name.strip(),
            args.artist_names.split(';')))
    artist_names_dict = dict(
        map(lambda artist_name: (artist_name, None), set(artist_names)))
    if args.do_musicbrainz:
        artist_names_dict = dict(
            map(
                lambda artist_name:
                (artist_name, music.MusicInfo(artist_name)),
                set(artist_names)))
    all_songs_downloaded = list(
        filter(
            None,
            map(
                lambda tup: _download_actual_song(hm,
                                                  lastfm,
                                                  tup[0],
                                                  tup[1],
                                                  args.maxnum,
                                                  do_whichone=do_whichone,
                                                  mi=artist_names_dict[tup[1]],
                                                  process_to_end=True),
                filter(None, zip(song_names, artist_names)))))
    return all_songs_downloaded
Exemplo n.º 3
0
def main():
    parser = ArgumentParser()
    parser.add_argument('-a',
                        '--artist',
                        dest='artist_name',
                        type=str,
                        action='store',
                        help='Name of the artist to get album image for.',
                        required=True)
    parser.add_argument('-A',
                        '--album',
                        dest='album_name',
                        type=str,
                        action='store',
                        help='Name of the album to get album image for.')
    parser.add_argument(
        '--songs',
        dest='do_songs',
        action='store_true',
        default=False,
        help=
        'If chosen, get the song listing instead of downloading the album image.'
    )
    parser.add_argument(
        '--formatted',
        dest='do_formatted',
        action='store_true',
        default=False,
        help=' '.join([
            'If chosen, print the song listing in a format recognized by plex_music_metafill.py',
            'for downloading a collection of songs.'
        ]))
    parser.add_argument(
        '--albums',
        dest='do_albums',
        action='store_true',
        default=False,
        help=
        'If chosen, then get a list of all the songs in all studio albums for the artist.'
    )
    parser.add_argument('--debug',
                        dest='do_debug',
                        action='store_true',
                        default=False,
                        help='Run with debug mode turned on.')
    parser.add_argument('--noverify',
                        dest='do_verify',
                        action='store_false',
                        default=True,
                        help='If chosen, do not verify SSL connections.')
    parser.add_argument(
        '--musicbrainz',
        dest='do_musicbrainz',
        action='store_true',
        default=False,
        help=' '.join([
            'If chosen, use Musicbrainz to get the artist metadata.',
            'Note that this is expensive, and is always applied when the --albums flag is set.'
        ]))
    args = parser.parse_args()
    music.MusicInfo.get_set_musicbrainz_useragent(emailAddress)
    music.MusicInfo.set_musicbrainz_verify(verify=args.do_verify)
    assert (args.artist_name is not None)
    if not args.do_albums: assert (args.album_name is not None)
    logger = logging.getLogger()
    if args.do_debug: logger.setLevel(logging.DEBUG)
    #
    ##
    hlastfm = music.HowdyLastFM(verify=args.do_verify)
    if args.do_albums:
        mi = music.MusicInfo(args.artist_name.strip())
        mi.print_format_album_names()
        return

    if not args.do_songs:  # just get the song image
        if args.do_musicbrainz:
            mi = music.MusicInfo(args.artist_name.strip())
            _, status = mi.get_album_image(args.album_name)
        else:
            _, status = hlastfm.get_album_image(args.artist_name,
                                                args.album_name)
        if status != 'SUCCESS':
            print(status)
        return

    #
    ## now get song listing, --songs is chosen
    if args.do_musicbrainz:
        mi = music.MusicInfo(args.artist_name.strip())
        track_listing, status = mi.get_song_listing(args.album_name)
    else:
        track_listing, status = hlastfm.get_song_listing(
            args.artist_name, album_name=args.album_name)
    if status != 'SUCCESS':
        print(status)
        return

    if not args.do_formatted:
        print('\n%s\n' %
              tabulate.tabulate(track_listing, headers=['Song', 'Track #']))
    else:
        print('\n%s\n' %
              ';'.join(map(lambda title_trkno: title_trkno[0], track_listing)))
Exemplo n.º 4
0
def _download_songs_oldformat(args):
    assert (args.artist_name is not None)
    #
    ## first get music metadata
    hm = music.HowdyMusic(verify=args.do_verify)
    lastfm = music.HowdyLastFM(verify=args.do_verify)

    #
    ## scenario #1: just get the list of albums
    # if args.do_albums: # use the --artist=<arg> --albums
    #     try:
    #         mi = music.MusicInfo( args.artist_name.strip( ) )
    #         mi.print_format_album_names( )
    #         return
    #     except Exception as e:
    #         logging.error( e, exc_info = True )
    #         print( 'Could not get find artist = %s with Musicbrainz.' % (
    #             args.artist_name.strip( ) ) )
    #         return
    if args.album_name is not None:  # use the --artist= --album=
        all_songs_downloaded = []
        #
        ## figure out order of music metadata services to get
        mi = None
        if args.do_lastfm:
            do_whichone = 'LASTFM'
        elif args.do_musicbrainz:
            do_whichone = 'MUSICBRAINZ'
            mi = music.MusicInfo(args.artist_name)
        else:
            do_whichone = 'GRACENOTE'
        album_data_dict, status = _process_data_album_dict(
            hm,
            lastfm,
            args.album_name.strip(),
            args.artist_name,
            do_whichone,
            mi=mi)
        if status != 'SUCCESS':
            print(status)
            return
        #
        ##
        data_dict = album_data_dict[0]
        artist_name = data_dict['artist']
        album_name = data_dict['album']
        album_tracks = data_dict['total tracks']
        album_url = data_dict['album url']
        image_data = None
        if album_url != '':
            image_data = io.BytesIO(
                requests.get(album_url, verify=args.do_verify).content)
        print('ACTUAL ARTIST: %s' % artist_name)
        print('ACTUAL ALBUM: %s' % album_name)
        if 'year' in data_dict:
            album_year = data_dict['year']
            print('ACTUAL YEAR: %d' % album_year)
        print('ACTUAL NUM TRACKS: %d' % album_tracks)
        for data_dict in album_data_dict:
            song_name = data_dict['song']
            track_number = data_dict['tracknumber']
            if 'duration' in data_dict:
                dur = int(data_dict['duration'])
                song_name_after = _get_final_song_name(song_name, dur)
                print('ACTUAL SONG: (%02d/%02d) %s' %
                      (track_number, album_tracks, song_name_after))
            else:
                print('ACTUAL SONG: (%02d/%02d) %s' %
                      (track_number, album_tracks, song_name))
            #
            ## now get the youtube song selections
            youtubeURL = _choose_youtube_item('%s %s' %
                                              (artist_name, song_name),
                                              maxnum=args.maxnum,
                                              verify=hm.verify)
            if youtubeURL is None:
                continue
            #
            ## now download the song into the given filename
            filename = '%s.%s.m4a' % (artist_name, song_name)
            music.get_youtube_file(youtubeURL, filename)
            #
            ## now fill out the metadata
            music.fill_m4a_metadata(filename,
                                    data_dict,
                                    verify=hm.verify,
                                    image_data=image_data)
            #
            ##
            os.chmod(filename, 0o644)
            all_songs_downloaded.append((artist_name, song_name, filename))
    else:  # use --artist= --songs=
        assert (args.song_names is not None)
        #0
        ## order of the music metadata to get
        mi = None
        if args.do_lastfm:
            do_whichone = 'LASTFM'
        elif args.do_musicbrainz:
            do_whichone = 'MUSICBRAINZ'
            mi = music.MusicInfo(args.artist_name)
        else:
            do_whichone = 'GRACENOTE'
        #
        ## now do the processing
        song_names = list(
            map(lambda song_name: song_name.strip(),
                args.song_names.split(';')))
        all_songs_downloaded = list(
            filter(
                None,
                map(
                    lambda song_name: _download_actual_song(
                        hm,
                        lastfm,
                        song_name,
                        args.artist_name,
                        args.maxnum,
                        do_whichone,
                        mi=mi,
                        process_to_end=False), song_names)))
    return all_songs_downloaded