Beispiel #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')
Beispiel #2
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')
Beispiel #3
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')
Beispiel #4
0
 def test_convert_from_m4a_to_mp3(self):
     expect_return_code = 0
     return_code = convert.song(file_name + '.m4a',
                                file_name + '.mp3',
                                const.args.folder,
                                avconv=True)
     assert return_code == expect_return_code
Beispiel #5
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')
    def test_convert_from_webm_to_mp3(self):
        global _file_name
        _file_name = os.path.join(tmp_dir, file_name)

        expect_return_code = 0
        return_code = convert.song(_file_name + '.webm', _file_name + '.mp3',
                                   const.config.folder)
        assert return_code == expect_return_code
Beispiel #7
0
def test_convert():
    # exit code 0 = success
    expect_converted = 0
    global input_song
    global output_song
    input_song = file_name + const.args.input_ext
    output_song = file_name + const.args.output_ext
    converted = convert.song(input_song, output_song, const.args.folder)
    assert converted == expect_converted
Beispiel #8
0
def test_convert():
    # exit code 0 = success
    expect_converted = 0
    # prerequisites for determining filename
    global input_song
    global output_song
    input_song = file_name + const.args.input_ext
    output_song = file_name + const.args.output_ext
    converted = convert.song(input_song, output_song, const.args.folder)
    assert converted == expect_converted
Beispiel #9
0
 def test_convert_from_m4a_to_webm(self):
     expect_return_code = 0
     return_code = convert.song(file_name + '.m4a', file_name + '.webm',
                                const.args.folder)
     os.remove(os.path.join(const.args.folder, file_name + '.webm'))
     assert return_code == expect_return_code
Beispiel #10
0
 def test_convert_from_webm_to_m4a(self):
     expect_return_code = 0
     return_code = convert.song(file_name + '.webm', file_name + '.m4a',
                                const.args.folder)
     assert return_code == expect_return_code
Beispiel #11
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
Beispiel #12
0
 def test_convert_from_m4a_to_flac(self):
     expect_return_code = 0
     return_code = convert.song(file_name + '.m4a', file_name + '.flac',
                                const.args.folder)
     assert return_code == expect_return_code
 def test_convert_from_m4a_to_webm(self):
     expect_return_code = 0
     return_code = convert.song(_file_name + '.m4a', _file_name + '.webm',
                                const.config.folder)
     assert return_code == expect_return_code