1. 程式人生 > >[c#]喜馬拉雅、蜻蜓、荔枝FM音訊批量下載器V1.2 by Levme開發手記

[c#]喜馬拉雅、蜻蜓、荔枝FM音訊批量下載器V1.2 by Levme開發手記

0.前言

好久沒在CSDN上寫東西了,看了之前開發的下載器由於三個網站程式碼更新已經不能用了,應要求,重分析了三個網站播放機制,寫了1.2版本的下載器,在我的資源裡有下載。 貼個地址:https://download.csdn.net/download/mnikkqqw/10663205

工具截圖

1.程式碼更新

這次程式碼更新,引入了Newtonsoft.Json.dll解析json檔案,以前那個是自己寫的解析器,太麻煩,用別人現成的就行。核心業務程式碼基本沒有變動,只是更新了分析網站資源的程式碼,解決了一下之前列表欄滾動條不可用的問題,工具還是有很多問題的,不影響使用沒改。 分析和新增喜馬拉雅音訊的程式碼:

if (ResourceBox.Lines[i].IndexOf("ximalaya") > -1)  //處理喜馬拉雅FM下載地址
{
    try
    {
        string url = ResourceBox.Lines[i];
        string html = CommonMethds.GetHttpWebRequest(url);
        Regex reg = new Regex(@"(?m)<h2 class=""dOi2""[^>]*>(?<span>(?:\w|\W)*?)</h2[^>]*>"
, RegexOptions.Multiline | RegexOptions.IgnoreCase); Match mc = reg.Match(html); string temp = mc.Groups["span"].Value.Trim(); temp = temp.Replace("專輯裡的聲音(<!-- -->", "").Replace("<!-- -->)", ""); int ResoucesCount = int.Parse(temp); int pageCount = ResoucesCount / 100
+ 1; string albumId = Regex.Replace(url, @"[^0-9]+", "").ToString(); for (int k = 1; k <= pageCount; k++) { string tempUrl = "https://www.ximalaya.com/revision/play/album?albumId=" + albumId + "&pageNum=" + k + "&sort=-1&pageSize=100"; string JsonSources = CommonMethds.GetHttpWebRequest(tempUrl); JObject jobj = (JObject)JsonConvert.DeserializeObject(JsonSources); string data = jobj["data"].ToString(); jobj = (JObject)JsonConvert.DeserializeObject(data); string tracksAudioPlay = jobj["tracksAudioPlay"].ToString(); JArray soundInfos = (JArray)JsonConvert.DeserializeObject(tracksAudioPlay); int count = soundInfos.Count; for (int j = 0; j < count; j++) { //防止datagridview假死導致滾動條不可用 Invoke(new Action(delegate { //繫結datagridview程式碼 JToken tempObj = soundInfos[j]; if (tempObj["src"].ToString() != "null" && tempObj["src"].ToString() != "") { int index = DownloadList.Rows.Add(); DownloadList.Rows[index].Cells[0].Value = true; DownloadList.Rows[index].Cells[1].Value = tempObj["albumId"].ToString(); DownloadList.Rows[index].Cells[2].Value = tempObj["albumName"].ToString(); DownloadList.Rows[index].Cells[3].Value = "[" + ((k - 1) * 100 + j + 1) + "]" + tempObj["trackName"].ToString(); DownloadList.Rows[index].Cells[4].Value = tempObj["src"].ToString(); DownloadList.Rows[index].Cells[5].Value = Int32.Parse(tempObj["duration"].ToString()) / 60 + "分" + Int32.Parse(tempObj["duration"].ToString()) % 60 + "秒"; DownloadList.Rows[index].Cells[6].Value = ""; DownloadList.Rows[index].Cells[7].Value = ""; DownloadList.Rows[index].Cells[8].Value = "0%"; } })); State.Text = "正在分析第" + (i + 1) + "個源網頁下載地址,已完成" + ((k - 1) * 100 + j) + "/" + ResoucesCount + ",請稍等……"; } } } catch { MessageBox.Show("請確認輸入網址符合輸入規則且電腦正確聯網!"); } }

分析和新增蜻蜓音訊的程式碼:

else if (ResourceBox.Lines[i].IndexOf("qingting") > -1)  //處理蜻蜓FM下載地址
{
    try
    {
        string url = ResourceBox.Lines[i];
        string html = CommonMethds.GetHttpWebRequest(url);
        Regex reg = new Regex(@"(?m)<span class=""_1QL0"" data-reactid=""73""[^>]*>(?<span>(?:\w|\W)*?)</span[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
        Match mc = reg.Match(html);
        string temp = mc.Groups["span"].Value.Trim();
        temp = temp.Replace("<!-- react-text: 74 -->共<!-- /react-text --><!-- react-text: 75 -->", "").Replace("<!-- /react-text --><!-- react-text: 76 -->個節目<!-- /react-text -->", "");
        int ResoucesCount = int.Parse(temp);
        int pageCount = ResoucesCount / 10 + 1;
        for (int k = 1; k <= pageCount; k++)
        {
            string tempUrl = url.Replace("http://www.qingting.fm", "http://i.qingting.fm/wapi") + "/programs/page/" + k + "/pagesize/10";
            string JsonSources = CommonMethds.GetHttpWebRequest(tempUrl);
            JObject jobj = (JObject)JsonConvert.DeserializeObject(JsonSources);
            string data = jobj["data"].ToString();
            JArray soundInfos = (JArray)JsonConvert.DeserializeObject(data);
            int count = soundInfos.Count;
            for (int j = 0; j < count; j++)
            {
                //防止datagridview假死導致滾動條不可用
                Invoke(new Action(delegate
                {
                    //繫結datagridview程式碼
                    JToken tempObj = soundInfos[j];
                    if (tempObj["file_path"].ToString() != "null" && tempObj["file_path"].ToString() != "")
                    {
                        int index = DownloadList.Rows.Add();
                        DownloadList.Rows[index].Cells[0].Value = true;
                        DownloadList.Rows[index].Cells[1].Value = tempObj["channel_id"].ToString();
                        DownloadList.Rows[index].Cells[2].Value = tempObj["channel_id"].ToString();
                        DownloadList.Rows[index].Cells[3].Value = "[" + ((k - 1) * 10 + j + 1) + "]" + tempObj["name"].ToString();
                        DownloadList.Rows[index].Cells[4].Value = "http://od.qingting.fm/" + tempObj["file_path"].ToString();
                        string duration = tempObj["duration"].ToString();
                        duration = duration.Substring(0, duration.IndexOf("."));
                        DownloadList.Rows[index].Cells[5].Value = Int32.Parse(duration) / 60 + "分" + Int32.Parse(duration) % 60 + "秒";
                        DownloadList.Rows[index].Cells[6].Value = "";
                        DownloadList.Rows[index].Cells[7].Value = "";
                        DownloadList.Rows[index].Cells[8].Value = "0%";
                    }
                }));
                State.Text = "正在分析第" + (i + 1) + "個源網頁下載地址,已完成" + ((k - 1) * 10 + j) + "/" + ResoucesCount + ",請稍等……";
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("請確認輸入網址符合輸入規則且電腦正確聯網!");
    }
}

分析和新增荔枝音訊的程式碼:

else if (ResourceBox.Lines[i].IndexOf("lizhi") > -1)   //處理荔枝FM下載地址
{
    try
    {
        string url = ResourceBox.Lines[i];
        string html = CommonMethds.GetHttpWebRequest(url);
        Regex reg = new Regex(@"\([1-9]\d*\)", RegexOptions.Multiline | RegexOptions.IgnoreCase);
        Match mc = reg.Match(html);
        mc = reg.Match(html);
        string temp = mc.Value.Trim();
        temp = temp.Replace("(", "").Replace(")", "");
        int ResoucesCount = int.Parse(temp);
        int pageCount = ResoucesCount / 20 + 1;
        for (int k = 1; k <= pageCount; k++)
        {
            string tempUrl = url + "/p/" + k + ".html";
            string webSources = CommonMethds.GetHttpWebRequest(tempUrl);
            webSources = webSources.Substring(webSources.IndexOf(@"<ul class=""audioList fontYaHei js-audio-list"">"));
            //專輯編號
            List<string> radioID = CommonMethds.GetHtmlAttr(webSources, "a", "data-uid");
            //專輯名稱
            List<string> radioName = CommonMethds.GetHtmlAttr(webSources, "a", "data-radio-name");
            //檔名稱
            List<string> audioName = CommonMethds.GetHtmlAttr(webSources, "a", "data-title");
            //檔案編號,用於獲取下載地址
            List<string> audioID = CommonMethds.GetHtmlAttr(webSources, "a", "data-id");
            //檔案時長
            List<string> audioDuration = CommonMethds.GetHtmlAttr(webSources, "a", "data-duration");

            for (int j = 0; j < audioID.Count; j++)
            {
                //防止datagridview假死導致滾動條不可用
                Invoke(new Action(delegate
                {
                    string JsonUrl = "http://www.lizhi.fm/media/url/" + audioID[j];
                    string json = CommonMethds.GetHttpWebRequest(JsonUrl);
                    JObject jobj = (JObject)JsonConvert.DeserializeObject(json);
                    string data = jobj["data"].ToString();
                    JObject soundUrl = (JObject)JsonConvert.DeserializeObject(data);
                    string audioUrl = soundUrl["url"].ToString();
                    if (audioUrl != "null" && audioUrl != "")
                    {
                        //繫結datagridview程式碼
                        int index = DownloadList.Rows.Add();
                        DownloadList.Rows[index].Cells[0].Value = true;
                        DownloadList.Rows[index].Cells[1].Value = radioID[j];
                        DownloadList.Rows[index].Cells[2].Value = radioName[j];
                        DownloadList.Rows[index].Cells[3].Value = "[" + ((k - 1) * 20 + j + 1) + "]" + audioName[j];
                        DownloadList.Rows[index].Cells[4].Value = audioUrl;
                        DownloadList.Rows[index].Cells[5].Value = Int32.Parse(audioDuration[j]) / 60 + "分" + Int32.Parse(audioDuration[j]) % 60 + "秒";
                        DownloadList.Rows[index].Cells[6].Value = "";
                        DownloadList.Rows[index].Cells[7].Value = "";
                        DownloadList.Rows[index].Cells[8].Value = "0%";
                    }
                }));
                State.Text = "正在分析第" + (i + 1) + "個源網頁下載地址,已完成" + ((k - 1) * 20 + j) + "/" + ResoucesCount + ",請稍等……";
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("請確認輸入網址符合輸入規則且電腦正確聯網!");
    }
}

原理幾乎沒有變,就是分析下載地址需要有點耐心。

2.後記

工具使用過程中因為源音訊本身不存在,會出現部分音訊下載失敗,一直卡在那個位置,只需要取消該音訊的下載繼續下其他的就可以。 如工具侵犯了您的合法權益,請郵箱聯絡我:[email protected]