コード例 #1
0
def downloader_wrapper(single_download_func, song_info, dist_path):
    if isinstance(song_info, dict):
        song_id = song_info["id"]
    else:
        song_id = song_info
        song_info, _ = netease_rename.detect_netease_music_name(song_id)

    song_artist_ne = song_info["artist"]
    song_name_ne = song_info["title"]
    print(">>>> song_id = %s, song_name_ne = %s, song_artist_ne = %s" %
          (song_id, song_name_ne, song_artist_ne))
    dist_name = netease_rename.generate_target_file_name(
        dist_path, song_name_ne, song_artist_ne, "mp3")
    if os.path.exists(dist_name):
        print("File %s exists, skip downloading, song_id = %s" %
              (dist_name, song_id))
        return dist_name

    keyword = " ".join([song_artist_ne, song_name_ne])
    download_url, song_name, song_artist, song_format = single_download_func(
        keyword)
    if download_url == None:
        return None

    dist_name = netease_rename.generate_target_file_name(
        dist_path, song_name, song_artist, song_format)
    dist_name = get_url_2_local_file(download_url, dist_name)
    return dist_name
コード例 #2
0
def netease_refresh_by_songlist_single(song_id,
                                       source_path,
                                       dist_path,
                                       single_download_func,
                                       WITH_SIZE_CHECK=False):
    song_info, _ = netease_rename.detect_netease_music_name(song_id)
    source_path_file = netease_rename.generate_target_file_name(
        source_path,
        song_info["title"],
        song_info["artist"],
        song_format="mp3")
    dist_path_file = netease_rename.generate_target_file_name(
        dist_path, song_info["title"], song_info["artist"], song_format="mp3")

    new_downloaded = False
    song_not_found = False
    if os.path.exists(dist_path_file):
        print("Dist file exists: %s, song_id = %s" % (dist_path_file, song_id))
        # temp_file_path = dist_path_file
        return new_downloaded, song_not_found
    elif os.path.exists(source_path_file) and WITH_SIZE_CHECK == False:
        print("Source file exists: %s, song_id = %s" %
              (source_path_file, song_id))
        temp_file_path = source_path_file
    else:
        temp_file_path = single_download_func(song_id, dist_path)
        new_downloaded = True

        if temp_file_path != None and os.path.exists(
                source_path_file) and WITH_SIZE_CHECK == True:
            source_size = os.path.getsize(source_path_file)
            downloaded_size = os.path.getsize(temp_file_path)
            print("New download, song_id = %s, title = %s, artist = %s" %
                  (song_id, song_info["title"], song_info["artist"]))
            print("source_size = %.2fM, downloaded_size = %.2fM" %
                  (source_size / 1024 / 1024, downloaded_size / 1024 / 1024))
            if downloaded_size - source_size >= 500000:
                print(">>>> Downloaded size is 500K bigger than source one")
            else:
                new_downloaded = False
                os.remove(temp_file_path)
                temp_file_path = source_path_file
        elif temp_file_path == None and os.path.exists(source_path_file):
            new_downloaded = False
            temp_file_path = source_path_file

    if temp_file_path == None:
        print("Song not found, song_id = %s, title = %s, artist = %s" %
              (song_id, song_info["title"], song_info["artist"]))
        new_downloaded = False
        song_not_found = True
    else:
        dist_path_file = netease_rename.netease_cache_rename_single(
            song_id, temp_file_path, dist_path, KEEP_SOURCE=False)
        print("Move %s to %s, song_id = %s" %
              (temp_file_path, dist_path_file, song_id))

    return new_downloaded, song_not_found
コード例 #3
0
def netease_download_single_bit_rate(song_info,
                                     dist_path=None,
                                     SIZE_ONLY=False):
    global global_requests_func
    if global_requests_func is None:
        global_requests_func = netease_rename.Requsets_with_login()

    if isinstance(song_info, dict):
        song_id = song_info["id"]
    else:
        song_id = song_info
        song_info, _ = netease_rename.detect_netease_music_name(song_id)

    song_download_url = "http://music.163.com/weapi/song/enhance/player/url?csrf_token="
    params = {"ids": [song_id], "br": 320000, "csrf_token": ""}

    data = encrypt.encrypted_request(params)
    resp = global_requests_func.post(song_download_url, data=data, timeout=30)
    resp_json = resp.json()
    if resp_json["code"] == -460:
        print(
            ">>>> Return with cheating in netease_download_single_bit_rate, maybe it is expired time limit, try again later"
        )
        exit(1)

    download_url = resp_json["data"][0]["url"]
    song_format = resp_json["data"][0].get("type", "mp3")
    if download_url == None:
        print(
            ">>>> download_url is None, maybe it is limited by copyright. song_id = %s"
            % (song_id))
        return None

    if SIZE_ONLY == True:
        return get_url_content_size(download_url)

    dist_name = netease_rename.generate_target_file_name(
        dist_path, song_info["title"], song_info["artist"], song_format)
    dist_name = get_url_2_local_file(download_url, dist_name)
    if dist_name and song_format == "mp3":
        dist_name = netease_rename.netease_cache_rename_single(
            song_info,
            dist_name,
            dist_path,
            KEEP_SOURCE=False,
            song_format=song_format)
        return dist_name
コード例 #4
0
def netease_download_single_outer(song_info, dist_path=None, SIZE_ONLY=False):
    if isinstance(song_info, dict):
        song_id = song_info["id"]
    else:
        song_id = song_info
        song_info, _ = netease_rename.detect_netease_music_name(song_id)

    url_base = "http://music.163.com/song/media/outer/url?id={}.mp3"
    url = url_base.format(song_id)

    if SIZE_ONLY == True:
        return get_url_content_size(url)

    dist_name = netease_rename.generate_target_file_name(
        dist_path, song_info["title"], song_info["artist"], "mp3")
    dist_name = get_url_2_local_file(url, dist_name)
    if dist_name:
        dist_name = netease_rename.netease_cache_rename_single(
            song_info,
            dist_name,
            dist_path,
            KEEP_SOURCE=False,
            song_format="mp3")
        return dist_name
コード例 #5
0
def netease_refresh_by_songlist(source_path, dist_path, songlist, single_download_func, WITH_SIZE_CHECK=False):
    if not os.path.exists(dist_path):
        os.mkdir(dist_path)

    new_downloaded = []
    song_not_found = []
    for song_id in songlist:
        song_info, _ = netease_rename.detect_netease_music_name(song_id)
        source_path_file = netease_rename.generate_target_file_name(
            source_path, song_info["title"], song_info["artist"], song_format="mp3"
        )
        dist_path_file = netease_rename.generate_target_file_name(
            dist_path, song_info["title"], song_info["artist"], song_format="mp3"
        )

        if os.path.exists(dist_path_file):
            print("Dist file exists: %s" % (dist_path_file))
            temp_file_path = dist_path_file
        elif os.path.exists(source_path_file) and WITH_SIZE_CHECK == False:
            print("Source file exists: %s" % (source_path_file))
            temp_file_path = source_path_file
        else:
            print(
                "Dowload from netease, song_id = %s, title = %s, artist = %s"
                % (song_id, song_info["title"], song_info["artist"])
            )
            temp_file_path = single_download_func(song_id, dist_path, WITH_RENAME=False)
            new_downloaded.append(song_id)

            if temp_file_path != None and os.path.exists(source_path_file) and WITH_SIZE_CHECK == True:
                source_size = os.path.getsize(source_path_file)
                downloaded_size = os.path.getsize(temp_file_path)
                print(
                    "source_size = %.2fM, downloaded_size = %.2fM" % (source_size / 1024 / 1024, downloaded_size / 1024 / 1024)
                )
                if downloaded_size - source_size >= 500000:
                    print(">>>> Downloaded size is 500K bigger than source one")
                else:
                    new_downloaded.remove(song_id)
                    os.remove(temp_file_path)
                    temp_file_path = source_path_file
            elif temp_file_path == None and os.path.exists(source_path_file):
                new_downloaded.remove(song_id)
                temp_file_path = source_path_file

        if temp_file_path == None:
            print("Song not found, song_id = %s, title = %s, artist = %s" % (song_id, song_info["title"], song_info["artist"]))
            new_downloaded.remove(song_id)
            song_not_found.append(song_id)
        else:
            dist_path_file = netease_rename.netease_cache_rename_single(song_id, temp_file_path, dist_path, KEEP_SOURCE=False)
            print("Move %s to %s" % (temp_file_path, dist_path_file))

        print()

    print("New downloaded size = %d" % (len(new_downloaded)))
    for ss in netease_rename.detect_netease_music_name_list(new_downloaded):
        print("    %s: %s - %s" % (ss["song_id"], ss["artist"], ss["title"]))
    print()
    print("Song not found, size = %d:" % len(song_not_found))
    for ss in netease_rename.detect_netease_music_name_list(song_not_found):
        print("    %s: %s - %s" % (ss["song_id"], ss["artist"], ss["title"]))
    print()
    print("Song not found id: %s" % (song_not_found))
    return song_not_found