コード例 #1
0
        public bool Down(string filePath, ref byte[] lrcData,int iThread)
        {
            System.Net.ServicePointManager.DefaultConnectionLimit = iThread;
            string RequestURL = "http://music.163.com/api/search/get/web?csrf_token=";
            string ResponseURL = "http://music.163.com/api/song/lyric?os=osx&id=";
            var m_tools = new NetUtils();
            var m_info = new MusicInfo(filePath);

            try
            {
                string postStr = "&s=" + m_tools.URL_Encoding(m_info.Title, Encoding.UTF8) + "&type=1&offset=0&total=true&limit=5";
                string resultJson = m_tools.Http_Post(RequestURL, postStr);

                if (resultJson.Equals("")) return false;

                // 获得歌曲ID
                JObject jsonSID = JObject.Parse(resultJson);
                JArray jsonArraySID = (JArray)jsonSID["result"]["songs"];
                string sid = null;

                // 二级匹配
                bool flag = false;
                foreach (var item in jsonArraySID)
                {
                    if (item["artists"][0]["name"].ToString() == m_info.Singer)
                    {
                        sid = item["id"].ToString();
                        flag = true;
                    }
                }
                if(!flag) sid = jsonArraySID[0]["id"].ToString();

                string lrcUrl = ResponseURL + sid + "&lv=-1&kv=-1&tv=-1";
                string lrcString = m_tools.Http_Get(lrcUrl, Encoding.UTF8, true);

                // 从Json当中分析歌词信息
                JObject lrcObject = JObject.Parse(lrcString);
                string lrcDataString = lrcObject["lrc"]["lyric"].ToString();
                if (lrcDataString.Equals("")) return false;
                // 去掉网易时间轴后一位
                Regex reg = new Regex(@"\[\d+:\d+.\d+\]");
                string deal_ok = reg.Replace(lrcDataString, new MatchEvaluator((Match machs)=>
                {
                    string time = machs.ToString();
                    return time.Remove(time.Length - 2, 1);
                }));

                // 将字符串转换为字节流进行存储
                lrcData = Encoding.UTF8.GetBytes(deal_ok);
                return true;

            }
            catch (Exception)
            {
                return false;
            }
        }
コード例 #2
0
ファイル: LibNetease.cs プロジェクト: addf400/ZonyLrcDownLoad
        public bool Down(string filePath, ref byte[] lrcData, int iThread)
        {
            System.Net.ServicePointManager.DefaultConnectionLimit = iThread;
            string RequestURL  = "http://music.163.com/api/search/get/web?csrf_token=";
            string ResponseURL = "http://music.163.com/api/song/lyric?os=osx&id=";
            var    m_tools     = new NetUtils();
            var    m_info      = new MusicInfo(filePath);

            try
            {
                string postStr    = "&s=" + m_tools.URL_Encoding(m_info.Title, Encoding.UTF8) + "&type=1&offset=0&total=true&limit=5";
                string resultJson = m_tools.Http_Post(RequestURL, postStr);

                if (resultJson.Equals(""))
                {
                    return(false);
                }

                // 获得歌曲ID
                JObject jsonSID      = JObject.Parse(resultJson);
                JArray  jsonArraySID = (JArray)jsonSID["result"]["songs"];
                string  sid          = null;

                // 二级匹配
                bool flag = false;
                foreach (var item in jsonArraySID)
                {
                    if (item["artists"][0]["name"].ToString() == m_info.Singer)
                    {
                        sid  = item["id"].ToString();
                        flag = true;
                    }
                }
                if (!flag)
                {
                    sid = jsonArraySID[0]["id"].ToString();
                }


                string lrcUrl    = ResponseURL + sid + "&lv=-1&kv=-1&tv=-1";
                string lrcString = m_tools.Http_Get(lrcUrl, Encoding.UTF8, true);

                // 从Json当中分析歌词信息
                JObject lrcObject     = JObject.Parse(lrcString);
                string  lrcDataString = lrcObject["lrc"]["lyric"].ToString();
                if (lrcDataString.Equals(""))
                {
                    return(false);
                }
                // 去掉网易时间轴后一位
                Regex  reg     = new Regex(@"\[\d+:\d+.\d+\]");
                string deal_ok = reg.Replace(lrcDataString, new MatchEvaluator((Match machs) =>
                {
                    string time = machs.ToString();
                    return(time.Remove(time.Length - 2, 1));
                }));

                // 将字符串转换为字节流进行存储
                lrcData = Encoding.UTF8.GetBytes(deal_ok);
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }