Пример #1
0
        // idOrUrl : 955407 or http://www.songtaste.com/song/955407 or http://www.songtaste.com/song/955407/
        // songInfo: the ST song info: title, artist, realAddr, ...
        // errStr: error reason string
        public bool getSongInfo(string idOrUrl, out songInfo songInfo, out string errStr)
        {
            bool getInfoOk = false;

            errStr   = "未知错误!";
            songInfo = new songInfo();

            string songId  = "";
            string songUrl = "";

            if (!isValidIdOrUrl(idOrUrl, out songId, out songUrl))
            {
                errStr = "无法识别的Songtaste的Id或Url:" + idOrUrl + " !";
                return(getInfoOk);
            }
            else
            {
                songInfo.id  = songId;
                songInfo.url = songUrl;
            }

            // here must clear previous cookies
            // otherwise access html with previous cookies will get fault html:
            //信息提示:   对不起,该用户不存在! 3 秒钟以后系统将自动跳转!
            crl.clearCurCookies();

            string respHtml = "";

            respHtml = crl.getUrlRespHtml(songInfo.url, stHtmlCharset);
            if (respHtml != "")
            {
                //note: some song is illegal:
                //http://songtaste.com/song/3029002/
                //return invalid html content
                //following can not found valid info

                // 1. get song title and artist
                //<p class="mid_tit">╰.很多人都在找的一首伤感韩文丶最近很火﹀淑熙(啦啦啦)丶</p><p></p>
                string titleP     = @"<p\s+?class=""mid_tit"">(?<title>.+?)</p>";
                Match  foundTitle = (new Regex(titleP)).Match(respHtml);
                if (foundTitle.Success)
                {
                    songInfo.title = foundTitle.Groups["title"].Value;
                    songInfo.title = crl.removeInvChrInPath(songInfo.title);
                    songInfo.title = songInfo.title.Trim();

                    //<h1 class="h1singer">1956.烟蒂</h1>
                    //<h1 class="h1singer">Rie fu</h1>
                    //<h1 class="h1singer"></h1>
                    string singerP     = @"<h1\s+?class=""h1singer"">(?<singer>.*?)</h1>";
                    Match  foundSinger = (new Regex(singerP)).Match(respHtml);
                    if (foundSinger.Success)
                    {
                        songInfo.artist = foundSinger.Groups["singer"].Value;
                        //special: http://www.songtaste.com/song/809103/, no singer
                        if (songInfo.artist == "")
                        {
                            songInfo.artist = "Unknown Singer";
                        }
                        songInfo.artist = crl.removeInvChrInPath(songInfo.artist);
                        songInfo.artist = songInfo.artist.Trim();

                        // 2. get song real address
                        //<a href="javascript:playmedia1('playicon','player', '5bf271ccad05f95186be764f725e9aaf07e0c7791a89123a9addb2a239179e64c91834c698a9c5d82f1ced3fe51ffc51', '355', '68', 'b3a7a4e64bcd8aabe4cabe0e55b57af5', 'http://m3.', '3015123',0);ListenLog(3015123, 0);">
                        //http://www.songtaste.com/song/2428041/ contain:
                        //<a href="javascript:playmedia1('playicon','player', 'cachefile33.rayfile.com/12f1/zh-cn/download/d1e8d86a0a9880f697aee789f27383db/preview', '355', '68', 'b3a7a4e64bcd8aabe4cabe0e55b57af5', 'http://224.', '2428041',0);ListenLog(2428041, 0);">
                        //string songP = @"javascript:playmedia1\('playicon','player', '(?<str>\w+)', '\d+', '\d+', '\w+', '.+?', '(?<sid>\d+)',\d+\);";
                        string songP     = @"javascript:playmedia1\('playicon','player', '(?<str>[^']+)', '\d+', '\d+', '(?<keyStr>\w+)', '(?<urlPref>.+?)', '(?<sid>\d+)',\d+\);";
                        Regex  songRx    = new Regex(songP);
                        Match  foundSong = songRx.Match(respHtml);
                        if (foundSong.Success)
                        {
                            string str     = foundSong.Groups["str"].Value;
                            string urlPref = foundSong.Groups["urlPref"].Value;
                            string keyStr  = foundSong.Groups["keyStr"].Value;
                            string sid     = foundSong.Groups["sid"].Value;

                            if (str.Contains("/"))
                            {
                                //cachefile33.rayfile.com/12f1/zh-cn/download/d1e8d86a0a9880f697aee789f27383db/preview
                                //to get the suffix
                                string suffix         = "";
                                string mainJsUrl      = "http://image.songtaste.com/inc/main.js";
                                string respHtmlMainJs = crl.getUrlRespHtml(mainJsUrl);
                                //		case "b3a7a4e64bcd8aabe4cabe0e55b57af5":
                                //          return ".mp3";
                                string suffixP     = @"""" + keyStr + @""":.+?return\s+""(?<suffix>\.\w+)"";";
                                Regex  suffixRx    = new Regex(suffixP, RegexOptions.Singleline);
                                Match  foundSuffix = suffixRx.Match(respHtmlMainJs);
                                if (foundSuffix.Success)
                                {
                                    suffix            = foundSuffix.Groups["suffix"].Value;
                                    songInfo.realAddr = urlPref + str + suffix;
                                }
                            }
                            else
                            {
                                //5bf271ccad05f95186be764f725e9aaf07e0c7791a89123a9addb2a239179e64c91834c698a9c5d82f1ced3fe51ffc51

                                Dictionary <string, string> headerDict = new Dictionary <string, string>();
                                headerDict.Add("x-requested-with", "XMLHttpRequest");
                                // when click play
                                // access http://songtaste.com/time.php, post data:
                                //str=5bf271ccad05f95186be764f725e9aaf07e0c7791a89123a9addb2a239179e64c91834c698a9c5d82f1ced3fe51ffc51&sid=3015123&t=0
                                Dictionary <string, string> postDict = new Dictionary <string, string>();
                                postDict.Add("str", str);
                                postDict.Add("sid", sid);
                                postDict.Add("t", "0");
                                string getRealAddrUrl = "http://songtaste.com/time.php";
                                songInfo.realAddr = crl.getUrlRespHtml(getRealAddrUrl, headerDict, stHtmlCharset, postDict);
                            }

                            if (songInfo.realAddr != "")
                            {
                                int lastPoint = songInfo.realAddr.LastIndexOf('.');
                                int strLen    = songInfo.realAddr.Length;
                                songInfo.suffix = songInfo.realAddr.Substring(lastPoint);

                                songInfo.storedName = songInfo.title + " - " + songInfo.artist + songInfo.suffix;

                                getInfoOk = true;
                            }
                            else
                            {
                                errStr = "找不到歌曲的真实下载地址!";
                            }
                        }
                        else
                        {
                            errStr = "找不到歌曲的真实下载地址!";
                        }
                    }
                    else
                    {
                        errStr = "找不到歌曲的歌手信息!";
                    }
                }
                else
                {
                    errStr = "找不到歌曲的标题信息!";
                }
            }
            else
            {
                errStr = "无法获取网页信息!";
            }

            return(getInfoOk);
        }