Exemplo n.º 1
0
def grab_single(raw_song, number=None):
    """ Logic behind downloading a song. """
    if internals.is_youtube(raw_song):
        log.debug('Input song is a YouTube URL')
        content = go_pafy(raw_song, meta_tags=None)
        raw_song = slugify(content.title).replace('-', ' ')
        meta_tags = generate_metadata(raw_song)
    else:
        meta_tags = generate_metadata(raw_song)
        content = go_pafy(raw_song, meta_tags)

    if content is None:
        log.debug('Found no matching video')
        return

    # "[number]. [artist] - [song]" if downloading from list
    # otherwise "[artist] - [song]"
    youtube_title = get_youtube_title(content, number)
    log.info('{} ({})'.format(youtube_title, content.watchv_url))
    # generate file name of the song to download
    songname = content.title

    if meta_tags is not None:
        refined_songname = generate_songname(meta_tags)
        log.debug('Refining songname from "{0}" to "{1}"'.format(
            songname, refined_songname))
        if not refined_songname == ' - ':
            songname = refined_songname

    if args.dry_run:
        return

    file_name = internals.sanitize_title(songname)

    if not check_exists(file_name, raw_song, meta_tags):
        if download_song(file_name, content):
            input_song = file_name + args.input_ext
            output_song = file_name + args.output_ext
            print('')

            try:
                convert.song(input_song,
                             output_song,
                             args.folder,
                             avconv=args.avconv)
            except FileNotFoundError:
                encoder = 'avconv' if args.avconv else 'ffmpeg'
                log.warning(
                    'Could not find {0}, skipping conversion'.format(encoder))
                args.output_ext = args.input_ext
                output_song = file_name + args.output_ext

            if not args.input_ext == args.output_ext:
                os.remove(os.path.join(args.folder, input_song))
            if not args.no_metadata:
                metadata.embed(os.path.join(args.folder, output_song),
                               meta_tags)

        else:
            log.error('No audio streams available')
Exemplo n.º 2
0
def test_embed_metadata():
    expect_metadata = True
    # prerequisites for determining filename
    metadata_input = metadata.embed(
        os.path.join(const.args.folder, file_name + '.m4a'), meta_tags)
    metadata_output = metadata.embed(
        os.path.join(const.args.folder, file_name + '.mp3'), meta_tags)
    assert metadata_output == (metadata_input == expect_metadata)
Exemplo n.º 3
0
def grab_single(raw_song, number=None):
    """ Logic behind downloading a song. """
    if number:
        islist = True
    else:
        islist = False

    if internals.is_youtube(raw_song):
        log.debug('Input song is a YouTube URL')
        content = go_pafy(raw_song, meta_tags=None)
        raw_song = slugify(content.title).replace('-', ' ')
        meta_tags = generate_metadata(raw_song)
    else:
        meta_tags = generate_metadata(raw_song)
        content = go_pafy(raw_song, meta_tags)

    if content is None:
        log.debug('Found no matching video')
        return

    # log '[number]. [artist] - [song]' if downloading from list
    # otherwise log '[artist] - [song]'
    log.info(get_youtube_title(content, number))
    # generate file name of the song to download
    songname = content.title

    if meta_tags is not None:
        refined_songname = generate_songname(meta_tags)
        log.debug('Refining songname from "{0}" to "{1}"'.format(
            songname, refined_songname))
        if not refined_songname == ' - ':
            songname = refined_songname

    file_name = internals.sanitize_title(songname)

    if not check_exists(file_name, raw_song, meta_tags, islist=islist):
        if download_song(file_name, content):
            input_song = file_name + args.input_ext
            output_song = file_name + args.output_ext
            print('')
            convert.song(input_song,
                         output_song,
                         args.folder,
                         avconv=args.avconv)
            if not args.input_ext == args.output_ext:
                os.remove(os.path.join(args.folder, input_song))

            if not args.no_metadata:
                metadata.embed(os.path.join(args.folder, output_song),
                               meta_tags)
        else:
            log.error('No audio streams available')
Exemplo n.º 4
0
def test_metadata():
    expect_metadata = None
    meta_tags = spotify_tools.generate_metadata(raw_song)
    if meta_tags:
        metadata_output = metadata.embed(
            os.path.join(const.args.folder, output_song), meta_tags)
        metadata_input = metadata.embed(
            os.path.join(const.args.folder, input_song), meta_tags)
    else:
        metadata_input = None
        metadata_output = None
    assert (metadata_output == expect_metadata) and (metadata_input
                                                     == expect_metadata)
Exemplo n.º 5
0
def download_single(raw_song, number=None, folder=None) -> bool:
    """ Logic behind downloading a song. """
    meta_tags = spotify_tools.generate_metadata(raw_song)

    if const.args.download_only_metadata and meta_tags is None:
        log.info('Found no metadata. Skipping the download')
        return False

    # generate file name of the song to download
    songname = 'foo'

    if meta_tags is not None:
        refined_songname = internals.format_string(const.args.file_format,
                                                   meta_tags,
                                                   slugification=True)
        log.debug('Refining songname from "{0}" to "{1}"'.format(
            songname, refined_songname))
        if not refined_songname == ' - ':
            songname = refined_songname
    else:
        log.warning('Could not find metadata')
        songname = internals.sanitize_title(songname)

    if not check_exists(songname, raw_song, meta_tags, folder):
        # deal with file formats containing slashes to non-existent directories
        if folder:
            folder_path = folder
        else:
            folder_path = const.args.folder
        songpath = os.path.join(folder_path, os.path.dirname(songname))
        os.makedirs(songpath, exist_ok=True)
        file_name = os.path.join(folder_path, songname + const.args.output_ext)
        play_time = player.play_and_record(meta_tags['uri'], file_name,
                                           songname)
        if not record.verify_length(file_name, meta_tags['duration'],
                                    play_time):
            log.error('Duration mismatch! Deleting: {}'.format(songname))
            if duration_debug:
                fail_path = os.path.join(folder_path, 'mismatch')
                if not os.path.exists(fail_path):
                    os.mkdir(fail_path)
                _, file_only = os.path.split(file_name)
                copyfile(file_name, os.path.join(fail_path, file_only))
            os.remove(file_name)
            return False
        if not const.args.no_metadata and meta_tags is not None:
            metadata.embed(file_name, meta_tags)
        return True
    return True
Exemplo n.º 6
0
def grab_single(raw_song, number=None):
    """Logic behind downloading a song."""
    print('grab single ' + raw_song)
    if number:
        islist = True
    else:
        islist = False
    content = go_pafy(raw_song)
    if content is None:
        return

    # print '[number]. [artist] - [song]' if downloading from list
    # otherwise print '[artist] - [song]'
    print(get_youtube_title(content, number))

    # generate file name of the song to download
    try:
        meta_tags = generate_metadata(raw_song)
    except (urllib.request.URLError, TypeError, IOError,
            spotipy.oauth2.SpotifyOauthError):
        spotify = generate_token()
        meta_tags = generate_metadata(raw_song)
    if meta_tags is None:
        songname = content.title
    else:
        songname = generate_songname(meta_tags)
        print('Song -->' + songname)
    file_name = misc.sanitize_title(songname)

    if not check_exists(file_name, raw_song, islist=islist):
        if download_song(file_name, content):
            print('')
            input_song = file_name + args.input_ext
            output_song = file_name + args.output_ext
            print(output_song)
            convert.song(input_song,
                         output_song,
                         args.folder,
                         avconv=args.avconv,
                         verbose=args.verbose)
            if not args.input_ext == args.output_ext:
                os.remove(os.path.join(args.folder, input_song))

            if not args.no_metadata:
                metadata.embed(os.path.join(args.folder, output_song),
                               meta_tags)
        else:
            print('No audio streams available or song already downloaded')
Exemplo n.º 7
0
def grab_single(raw_song, number=None):
    """Logic behind downloading a song."""
    if number:
        islist = True
    else:
        islist = False

    content = go_pafy(raw_song)
    if content is None:
        return

    if misc.is_youtube(raw_song):
        raw_song = slugify(content.title).replace('-', ' ')

    # print '[number]. [artist] - [song]' if downloading from list
    # otherwise print '[artist] - [song]'
    print(get_youtube_title(content, number))

    # generate file name of the song to download
    meta_tags = generate_metadata(raw_song)
    songname = content.title

    if meta_tags is not None:
        refined_songname = generate_songname(meta_tags)
        if not refined_songname == ' - ':
            songname = refined_songname

    file_name = misc.sanitize_title(songname)

    if not check_exists(file_name, raw_song, islist=islist):
        if download_song(file_name, content):
            print('')
            input_song = file_name + args.input_ext
            output_song = file_name + args.output_ext
            convert.song(input_song,
                         output_song,
                         args.folder,
                         avconv=args.avconv,
                         verbose=args.verbose)
            if not args.input_ext == args.output_ext:
                os.remove(os.path.join(args.folder, input_song))

            if not args.no_metadata:
                metadata.embed(os.path.join(args.folder, output_song),
                               meta_tags)
        else:
            print('No audio streams available')
Exemplo n.º 8
0
def grab_single(raw_song, number=None):
    # check if song is being downloaded from list
    if number:
        islist = True
    else:
        islist = False
    content = go_pafy(raw_song)
    if content is None:
        return
    # print "[number]. [artist] - [song]" if downloading from list
    # otherwise print "[artist] - [song]"
    print(get_YouTube_title(content, number))
    # generate file name of the song to download
    music_file = misc.generate_filename(content.title)
    music_file = misc.fix_decoding(music_file)
    if not check_exists(music_file, raw_song, islist=islist):
        download_song(content)
        print('')
        convert_song(music_file)
        meta_tags = generate_metadata(raw_song)
        if not args.no_metadata:
            metadata.embed(music_file, meta_tags, args.output_ext)
Exemplo n.º 9
0
def download_single(raw_song, number=None):
    """ Logic behind downloading a song. """
    if internals.is_youtube(raw_song):
        log.debug('Input song is a YouTube URL')
        content = youtube_tools.go_pafy(raw_song, meta_tags=None)
        raw_song = slugify(content.title).replace('-', ' ')
        meta_tags = spotify_tools.generate_metadata(raw_song)
    else:
        meta_tags = spotify_tools.generate_metadata(raw_song)
        content = youtube_tools.go_pafy(raw_song, meta_tags)

    if content is None:
        log.debug('Found no matching video')
        return

    if const.args.download_only_metadata and meta_tags is None:
        log.info('Found no metadata. Skipping the download')
        return

    # "[number]. [artist] - [song]" if downloading from list
    # otherwise "[artist] - [song]"
    youtube_title = youtube_tools.get_youtube_title(content, number)
    log.info('{} ({})'.format(youtube_title, content.watchv_url))

    # generate file name of the song to download
    songname = content.title

    if meta_tags is not None:
        refined_songname = internals.format_string(const.args.file_format,
                                                   meta_tags,
                                                   slugification=True)
        log.debug('Refining songname from "{0}" to "{1}"'.format(
            songname, refined_songname))
        if not refined_songname == ' - ':
            songname = refined_songname
    else:
        log.warning('Could not find metadata')
        songname = internals.sanitize_title(songname)

    if const.args.dry_run:
        return

    if not check_exists(songname, raw_song, meta_tags):
        # deal with file formats containing slashes to non-existent directories
        songpath = os.path.join(const.args.folder, os.path.dirname(songname))
        os.makedirs(songpath, exist_ok=True)
        input_song = songname + const.args.input_ext
        output_song = songname + const.args.output_ext
        if youtube_tools.download_song(input_song, content):
            print('')
            try:
                convert.song(input_song,
                             output_song,
                             const.args.folder,
                             avconv=const.args.avconv,
                             trim_silence=const.args.trim_silence)
            except FileNotFoundError:
                encoder = 'avconv' if const.args.avconv else 'ffmpeg'
                log.warning(
                    'Could not find {0}, skipping conversion'.format(encoder))
                const.args.output_ext = const.args.input_ext
                output_song = songname + const.args.output_ext

            if not const.args.input_ext == const.args.output_ext:
                os.remove(os.path.join(const.args.folder, input_song))
            if not const.args.no_metadata and meta_tags is not None:
                metadata.embed(os.path.join(const.args.folder, output_song),
                               meta_tags)
            return True
Exemplo n.º 10
0
 def test_embed_in_webm(self):
     expect_embed = False
     embed = metadata.embed(track_path + '.webm', meta_tags)
     os.remove(track_path + '.webm')
     assert embed == expect_embed
Exemplo n.º 11
0
 def test_embed_in_m4a(self):
     expect_embed = True
     embed = metadata.embed(track_path + '.m4a', meta_tags)
     os.remove(track_path + '.m4a')
     assert embed == expect_embed
Exemplo n.º 12
0
 def test_embed_in_mp3(self):
     expect_embed = True
     global track_path
     track_path = os.path.join(const.args.folder, file_name)
     embed = metadata.embed(track_path + '.mp3', meta_tags)
     assert embed == expect_embed