コード例 #1
0
    def __init__(self, raw_song, meta_tags):
        self.raw_song = raw_song
        self.meta_tags = meta_tags

        if meta_tags is None:
            self.search_query = raw_song
        else:
            self.search_query = internals.format_string(const.args.search_format,
                                                        meta_tags, force_spaces=True)
コード例 #2
0
def test_check_track_exists_before_download(tmpdir):
    expect_check = False
    const.args.folder = str(tmpdir)
    # prerequisites for determining filename
    songname = internals.format_string(const.args.file_format, meta_tags)
    global file_name
    file_name = internals.sanitize_title(songname)
    check = spotdl.check_exists(file_name, TRACK_URL, meta_tags)
    assert check == expect_check
コード例 #3
0
    def refine_songname(self, songname):
        if self.meta_tags is not None:
            refined_songname = internals.format_string(const.args.file_format,
                                                       self.meta_tags,
                                                       slugification=True)
            log.debug('Refining songname from "{0}" to "{1}"'.format(
                songname, refined_songname))
            if not refined_songname == " - ":
                songname = refined_songname
        else:
            songname = internals.sanitize_title(songname)

        return songname
コード例 #4
0
 def test_without_spaces(self):
     const.args.no_spaces = True
     title = internals.format_string(const.args.file_format, meta_tags)
     assert title == EXPECTED_TITLE.replace(" ", "_")
コード例 #5
0
 def test_with_spaces(self):
     title = internals.format_string(const.args.file_format, meta_tags)
     assert title == EXPECTED_TITLE
コード例 #6
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):
            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
コード例 #7
0
def filename_fixture(metadata_fixture):
    songname = internals.format_string(const.args.file_format,
                                       metadata_fixture)
    filename = internals.sanitize_title(songname)
    return filename
コード例 #8
0
 def test_without_spaces(self, metadata_fixture):
     const.args.no_spaces = True
     title = internals.format_string(const.args.file_format,
                                     metadata_fixture)
     assert title == EXPECTED_SPOTIFY_TITLE.replace(" ", "_")
コード例 #9
0
 def test_with_spaces(self, metadata_fixture):
     title = internals.format_string(const.args.file_format,
                                     metadata_fixture)
     assert title == EXPECTED_SPOTIFY_TITLE
コード例 #10
0
def download_single(raw_song, number=None):
    """ Logic behind downloading a song. """
    content, meta_tags = youtube_tools.match_video_and_metadata(raw_song)

    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:
        if not const.args.no_metadata:
            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
コード例 #11
0
 def test_without_spaces(self):
     expect_title = 'David_André_Østby_-_Intro'
     const.args.no_spaces = True
     title = internals.format_string(const.args.file_format, meta_tags)
     assert title == expect_title
コード例 #12
0
 def test_with_spaces(self):
     expect_title = 'David André Østby - Intro'
     title = internals.format_string(const.args.file_format, meta_tags)
     assert title == expect_title