Skip to content

Instantly share code, notes, and snippets.

@Tsanfer
Last active February 26, 2024 10:31
Show Gist options
  • Save Tsanfer/bdd2353a2ab9709c9ff27041ab19f276 to your computer and use it in GitHub Desktop.
Save Tsanfer/bdd2353a2ab9709c9ff27041ab19f276 to your computer and use it in GitHub Desktop.
在 Powershell 中,增量下载网易云音乐多个歌单音乐。
# 增量下载网易云音乐多个歌单音乐。
#
# 为安装下载工具,最好准备好科学上网环境
#
# 以管理员权限,在 powershell 中,运行此脚本
#
# 使用以下命令允许 powershell 中运行脚本:
# Set-ExecutionPolicy RemoteSigned
#
# 请将 Powershell 的版本至少升级到 7
# 参考链接:https://learn.microsoft.com/zh-cn/powershell/scripting/install/installing-powershell-on-windows
# 查看当前 Powershell 主版本:
# $PSVersionTable.PSVersion.Major
# 测试 powershel 版本:7.4.0
# 更新时间:2024/02/20
# 保存到的文件夹
$download_path = "D:\folder\to\save"
# 下载选项
# 详细使用方法参考官方 Github 仓库:https://github.com/magicdawn/yun-playlist-downloader
# --cover 是否下载封面
# --concurrency 下载并发线程数
# --format 下载到本地的文件格式(路径:命令执行的目录)
# --progress 显示进度条
# --cookie 使用 cookie 获取下载 VIP 歌曲权限
# 例子
# $options = "--cover=true --progress --concurrency=8 --format=':name\:songName - :singer.:ext' "
$options = "--cover=true --progress --concurrency=8 --format=':name\:songName - :singer.:ext'"
# 要下载的歌单列表
# 例子:
# $urls = "https://music.163.com/playlist?id=11111111",
# "https://music.163.com/playlist?id=22222222",
# "https://music.163.com/playlist?id=33333333"
$urls = "https://music.163.com/playlist?id=11111111",
"https://music.163.com/playlist?id=22222222",
"https://music.163.com/playlist?id=33333333"
# ---------- 脚本开始位置 ----------
# # 安装 yun-playlist-downloader
# while (-not(yun -v)) {
# Write-Output "----- yun-playlist-downloader 未安装 -----"
# Write-Output "----- yun-playlist-downloader 安装中 -----"
# if (-not(choco -v)) {
# Write-Output "----- chocolatey 未安装 -----"
# Write-Output "----- chocolatey 安装中 -----"
# # 安装 chocolatey
# Set-ExecutionPolicy Bypass -Scope Process -Force
# [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
# Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) && `
# Write-Output "----- chocolatey 安装完成 -----"
# }
# if (-not(npm -v)) {
# Write-Output "----- npm 未安装 -----"
# Write-Output "----- npm 安装中 -----"
# choco install nodejs-lts && # 安装 node.js
# Write-Output "----- npm 安装完成 -----"
# }
# npm i yun-playlist-downloader -g && # 安装 yun-playlist-downloader
# Write-Output "----- yun-playlist-downloader 安装完成 -----"
# Read-Host "是否重试?(继续:回车, 退出: CRTL+C)`n"
# }
$start_time = Get-Date
$inputValue = Read-Host "是否安装下载软件(Y/Any)?"
if ($inputValue -eq 'Y' -or $inputValue -eq 'y') {
# 安装 yun-playlist-downloader
Write-Output "----- chocolatey 安装中 -----"
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
if (choco -v) {
Write-Output "----- chocolatey 安装完成 -----"
}
Write-Output "----- npm 安装中 -----"
choco install nodejs-lts # 安装 node.js
refreshenv
if (npm -v) {
Write-Output "----- npm 安装完成 -----"
# Write-Output "--- 切换 npm 镜像源 ---"
# npm config set registry https://registry.npm.taobao.org/
}
Write-Output "----- yun-playlist-downloader 安装中 -----"
npm i yun-playlist-downloader -g # 安装 yun-playlist-downloader
if (yun -v) {
Write-Output "----- yun-playlist-downloader 安装完成 -----"
}
}
else {
Write-Host "跳过安装下载软件"
}
Set-Location $download_path
for ($i = 0; $i -lt $urls.Count; $i++) {
"----- 预计下载 {0} 个歌单,现在开始下载第 {1} 个歌单 -----" -f $urls.Count, $($i + 1)
Invoke-Expression "yun $options $($urls[$i])"
"----- 第 {0} 个歌单下载完成 -----" -f $($i + 1)
}
Write-Host "----- 歌单下载完毕 -----"
# 计算脚本运行时间
$end_time = Get-Date
$elapsed_time = $end_time - $start_time
"`n##### 脚本总运行时间:{0} 分钟 #####`n" -f $elapsed_time.TotalMinutes
/**
* 打印用户全部歌单地址(包括“我喜欢的音乐”歌单)
* 在网易云音乐用户界面按 F12 后,在 console 里输入下面脚本
*/
// 延迟函数(单位:ms)
function delay(n) {
return new Promise(function (resolve) {
setTimeout(resolve, n);
});
}
(async function () {
var song_lists_urls = ""; // 输出的 urls 字符串
let iframe_document = document.getElementById("g_iframe").contentDocument;
for (let i = 0; i < 10; i++) {
// 向下滚动N次
scroll_height = iframe_document.documentElement.scrollHeight; // 整个元素的高度
iframe_document.documentElement.scrollTop = scroll_height; // 向下滚动,加载页面元素
await delay(500); // 等待网页加载
}
for (let node of iframe_document
.getElementById("cBox")
.getElementsByClassName("dec")) {
song_lists_urls += node.firstChild.href + "\n"; // 显示集合中的所有节点的链接
}
console.log(song_lists_urls);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment