您的位置:首页 > 编程语言 > C#

C#网易云音乐中需付费歌曲的下载助手。

2016-06-05 16:22 603 查看
           网易云音乐要收费了,部分歌曲只能听不能下载,还是很遗憾的。闲来无事,写了个收费歌曲的下载助手。

下载链接:链接:http://pan.baidu.com/s/1ge5stgB 密码:f9qo

使用方法:

1、安装.net framework 4.0 (若没装的话,下载地址:https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=17718)

2、安装网易云音乐客户端

3、打开“下载助手”,设置好下载保存地址,如下图:



4、选中所需下载的音乐,点右键,点复制链接,如下图:



5、下载就开始了。。。。。就这么简单。。。。。

(只要复制歌曲链接,就能自动添加到下载助手里面哟,然后自动下载。)



6、可以一个劲复制很多链接(重复4操作)。

7、退出软件,点“”没用的。。会退到后台。必须在状态栏找到以下图标,点右键,点退出

    

          


8、若觉得对您有帮助,可以发个微信红包打赏哦



(微信号:li1239019842)

缺点:

下载的音乐不是高品质的。。。

---------------------------------------------------分割线---------------------------------------------------------

原理:通过网易云音乐客户端复制的音乐链接中获取音乐ID(红色),

“http://music.163.com/#/m/song?id=27804029&userid=98810162”

将歌曲id替换以下链接中“XXXXXX”
http://music.163.com/api/song/detail/?id=XXXXXX&ids=%5BXXXXXX%5D&csrf_token=
网址输入浏览器中返回json语句,其中“mp3Url”即为歌曲链接,如下:
http://m2.music.126.net/MEYfrcH-XivohQ01yOsoCw==/3288639278734103.mp3
这就是音乐的下载地址。。。。

-------------------------------------------------源码--------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace NeteaseCouldMusicDownload
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("user32")]
private static extern IntPtr SetClipboardViewer(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport("user32")]
private static extern IntPtr ChangeClipboardChain(IntPtr hwnd, IntPtr hWndNext);
[System.Runtime.InteropServices.DllImport("user32")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

const int WM_DRAWCLIPBOARD = 0x308;
const int WM_CHANGECBCHAIN = 0x30D;
IntPtr NextClipHwnd;

private static StreamWriter sw;

int id;
int finished;
Thread t;
public Form1()
{
InitializeComponent();
id = 0;
}

private void btnSelectPath_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbDialog = new FolderBrowserDialog();
fbDialog.Description = "请选择文件保存路径";
if(fbDialog.ShowDialog() == DialogResult.OK)
{
txtPath.Text = fbDialog.SelectedPath;
string iniPathName = Environment.CurrentDirectory + "\\Default.ini";
OperateIniFile.WriteIniData("Dictionary", "key", txtPath.Text, iniPathName);
}
}
private void AddDownload(string url)
{
url = TransfromUrl(url);
if (string.IsNullOrEmpty(url))
{
return;
}
if (url != "error")
{
Song song = DownloadHelper.GetMusicUrl(DownloadHelper.GetWebContent(url));
if (song == null)
{
return;
}
if (File.Exists(txtPath + "\\" + song.songName + "." + song.extension))
{
File.Delete(txtPath + "\\" + song.songName + "." + song.extension);
}
DataGridViewRow dr = new DataGridViewRow();
dr.CreateCells(dataGridView1);
dr.Cells[0].Value = id + 1;
dr.Cells[1].Value = song.songName + "." + song.extension;
dr.Cells[2].Value = song.singer;
dr.Cells[3].Value = (Convert.ToDouble(song.songSize) / 1024 / 1024).ToString("0.00M");
dr.Cells[4].Value = "0%";
dr.Cells[5].Value = song.songUrl;
dataGridView1.Rows.Add(dr);
txtUrl.Text = "";
id++;
t.Resume(); //恢复线程
}
}
private void btnDownload_Click(object sender, EventArgs e)
{
string url = TransfromUrl(txtUrl.Text);
if (string.IsNullOrEmpty(url))
{
MessageBox.Show("请输入歌曲链接!");
return;
}
if (url != "error")
{
Song song = DownloadHelper.GetMusicUrl(DownloadHelper.GetWebContent(url));
if (song == null)
{
MessageBox.Show("找不到歌曲!");
return;
}
if (File.Exists(txtPath + "\\" + song.songName + "." + song.extension))
{
File.Delete(txtPath + "\\" + song.songName + "." + song.extension);
}
DataGridViewRow dr = new DataGridViewRow();
dr.CreateCells(dataGridView1);
dr.Cells[0].Value = id+1;
dr.Cells[1].Value = song.songName+"."+song.extension;
dr.Cells[2].Value = song.singer;
dr.Cells[3].Value = (Convert.ToDouble(song.songSize)/1024/1024).ToString("0.00M");
dr.Cells[4].Value = "0%";
dr.Cells[5].Value = song.songUrl;
dataGridView1.Rows.Add(dr);
txtUrl.Text = "";
Thread.Sleep(100);
id++;
t.Resume(); //恢复线程
}
else
{
MessageBox.Show("歌曲链接错误!");
}
}
private string TransfromUrl(string url)
{
if (string.IsNullOrEmpty(url))
{
return "";
}

string[] tmp = url.Split(new char[]{'?','=','&'});
if (tmp.Length>1 && tmp[1] == "id")
{
return "http://music.163.com/api/song/detail/?id=" + tmp[2] + "&ids=%5B" + tmp[2] + "%5D&csrf_token=";
}
else
return "error";
}

private void Form1_Load(object sender, EventArgs e)
{
NextClipHwnd = SetClipboardViewer(this.Handle);
string iniPathName = Environment.CurrentDirectory + "\\Default.ini";
OperateIniFile.Checkini(iniPathName);
string songPath = OperateIniFile.ReadIniData("Dictionary", "key", "Empty", iniPathName);
if (System.IO.Directory.Exists(songPath))
{
txtPath.Text = songPath;
}
else
{
txtPath.Text = Environment.CurrentDirectory;
OperateIniFile.WriteIniData("Dictionary", "key", txtPath.Text, iniPathName);
}
t = new Thread(new ThreadStart(DownloadProc));
id = 0;        //添加下载的数目
finished = 0;  //已下载数
t.Start();     //启动下载线程
}

private void DownloadProc()
{
while (true)
{
if (finished < id)
{
DownloadHelper.DownloadFile(ref dataGridView1, txtPath.Text, finished, null);
finished++;
}
else
{
Thread.Sleep(1000);
t.Suspend(); //挂起线程
}
}
}

protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_DRAWCLIPBOARD:
//将WM_DRAWCLIPBOARD消息传递到下一个观察链中的窗口
SendMessage(NextClipHwnd, m.Msg, m.WParam, m.LParam);
IDataObject iData = Clipboard.GetDataObject();
//检测文本
if (iData.GetDataPresent(DataFormats.Text) | iData.GetDataPresent(DataFormats.OemText))
{
if (iData != null && System.IO.Directory.Exists(txtPath.Text))
{
string url = (String)iData.GetData(DataFormats.Text);
AddDownload(url);
}
}
break;
default:
base.WndProc(ref m);
break;
}
}

private void ToolMenuShow_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();   //激活窗体给予焦点
}

private void ToolMenuClose_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("确定要退出程序?", "安全提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dr == DialogResult.Yes)
{

notifyIcon1.Visible = false;
this.Close();
this.Dispose();
t.Resume();
t.Abort();

Application.Exit();
}
}

private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = true;
}
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;  //取消关闭操作,表现为不关闭窗体
this.Hide();     //隐藏窗体
}
}

}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Runtime.InteropServices;

namespace NeteaseCouldMusicDownload
{
class Song
{
public string songUrl { get; set; }
public string songName { get; set; }
public string singer { get; set; }
public string songSize { get; set; }
public string extension { get; set; }
}
class DownloadHelper
{
/// <summary>
/// 获得Json字符串
/// </summary>
/// <param name="Url">网址</param>
/// <returns></returns>
public static string GetWebContent(string Url)
{
string strResult = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//声明一个HttpWebRequest请求
request.Timeout = 30000;
//设置连接超时时间
request.Headers.Set("Pragma", "no-cache");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamReceive, Encoding.GetEncoding("UTF-8"));
strResult = streamReader.ReadToEnd();
}
catch(Exception e)
{
MessageBox.Show(e.ToString()+":"+e.Message);
}
return strResult;
}
/// <summary>
/// 解析Json语句,得到音乐信息
/// </summary>
/// <param name="jsonStr"></param>
/// <returns></returns>
public static Song GetMusicUrl(string jsonStr)
{
Song song = new Song();
JObject jsonobj = JObject.Parse(jsonStr);
JArray jar = JArray.Parse(jsonobj["songs"].ToString());
if (jar.Count == 0)
return null;
JObject songobj = JObject.Parse(jar[0].ToString());
song.songUrl =  songobj["mp3Url"].ToString();
song.songName = songobj["name"].ToString();

song.singer = JObject.Parse(JArray.Parse(songobj["artists"].ToString())[0].ToString())["name"].ToString();
song.songSize = JObject.Parse(songobj["bMusic"].ToString())["size"].ToString();
song.extension = JObject.Parse(songobj["bMusic"].ToString())["extension"].ToString();

return song;
}
/// <summary>
/// 通过url下载文件
/// </summary>
/// <param name="URL">网址链接</param>
/// <param name="filename">保存路径</param>
/// <param name="prog"></param>
/// <param name="label1"></param>
public static void DownloadFile(ref DataGridView grid,string path,int row,ProgressBar prog)
{
string URL = grid.Rows[row].Cells[5].Value.ToString();
string filename = path + "\\" + grid.Rows[row].Cells[1].Value.ToString();
float percent = 0;
try
{
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
long totalBytes = myrp.ContentLength;
if (prog != null)
{
prog.Maximum = (int)totalBytes;
}
System.IO.Stream st = myrp.GetResponseStream();
System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
long totalDownloadedByte = 0;
byte[] by = new byte[102400];
int osize = st.Read(by, 0, (int)by.Length);
while (osize > 0)
{
totalDownloadedByte = osize + totalDownloadedByte;
System.Windows.Forms.Application.DoEvents();
so.Write(by, 0, osize);
if (prog != null)
{
prog.Value = (int)totalDownloadedByte;
}
osize = st.Read(by, 0, (int)by.Length);

percent = (float)totalDownloadedByte / (float)totalBytes * 100;
grid.Rows[row].Cells[4].Value = percent.ToString("0.0") + "%";
System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
}
so.Close();
st.Close();
}
catch (System.Exception)
{
throw;
}
}
}
public class OperateIniFile
{
#region API函数声明

[DllImport("kernel32")]//返回0表示失败,非0为成功
private static extern long WritePrivateProfileString(string section, string key,
string val, string filePath);

[DllImport("kernel32")]//返回取得字符串缓冲区的长度
private static extern long GetPrivateProfileString(string section, string key,
string def, StringBuilder retVal, int size, string filePath);

#endregion

#region 读Ini文件

public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
{
if (File.Exists(iniFilePath))
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
return temp.ToString();
}
else
{
return String.Empty;
}
}

#endregion

#region 写Ini文件

public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
{
if (File.Exists(iniFilePath))
{
long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
if (OpStation == 0)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}

#endregion

public static bool Checkini(string pathName)
{
if (!File.Exists(pathName))
{
File.Create(pathName).Close();
}
return true;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息