示例#1
0
        /// <summary>
        /// 分析返回的DIV快内的歌曲信息
        /// </summary>
        /// <param name="tr">歌曲tr块的HTML信息</param>
        /// <returns>TR块中歌曲信息列表</returns>
        private List<MusicInfo> MusicInfoBuild(string tr)
        {
            List<MusicInfo> musicInfoList = new List<MusicInfo>();

            // 从TR块中获取音乐基本信息
            tr = tr.Replace("\n", string.Empty);
            tr = tr.Replace("&nbsp;", string.Empty);
            GroupCollection infoGroup = RegexHelper.GetRegexGroup(tr, BAIDU_MUSIC_INFO_PATTERN, RegexOptions.IgnoreCase);
            if (infoGroup == null)
            {
                return musicInfoList;
            }

            // 设置音乐基本信息
            MusicInfo music = new MusicInfo();
            music.MusicSource = "百度音乐";
            music.MusicName = EncodeConverter.HtmlDecode(infoGroup["MusicName"].Value);

            // 这里单独从TR块中获取专辑信息,是出于性能考虑,如果在infoGroup中搜索专辑名信息,因为正则表达式过长
            // 导致每次搜索要940ms左右,而分离后,两次搜索总时间不到5ms
            music.Album = EncodeConverter.HtmlDecode(RegexHelper.GetRegexGroupItem(tr, BAIDU_MUSIC_INFO_PATTERN_2, "Album", RegexOptions.IgnoreCase));
            music.SingerName = infoGroup["Singer1"].Value + " " + infoGroup["Singer2"].Value + " " + infoGroup["Singer3"].Value;

            // 从歌名的链接地址中获得音乐地址
            string detailLink = infoGroup["LinkUrl"].Value;
            if (!string.IsNullOrEmpty(detailLink))
            {
                List<string> musicUrlList = MusicOperator.GetMusicUrlList(detailLink);  //目前一些文件名后面没有后缀名
                if (musicUrlList == null || musicUrlList.Count == 0)
                {
                    return null;
                }

                // 过滤掉重复地址
                var urlList = musicUrlList;

                // 每个不同的地址,组成一个音乐信息
                foreach (string url in urlList)
                {
                    if (MusicOperator.urlIsValid(url))
                    {
                        MusicInfo tmpMusic = music.Clone() as MusicInfo;
                        tmpMusic.MusicUrl = url;
                        tmpMusic.MusicFormat = MusicOperator.GetMusicFormat(url);
                        musicInfoList.Add(tmpMusic);
                    }
                }
            }
            else
            {
                return musicInfoList;
            }
            return musicInfoList;
        }
示例#2
0
        /// <summary>
        /// 分析返回的DIV快内的歌曲信息
        /// </summary>
        /// <param name="tr">歌曲TR块的HTML信息</param>
        /// <returns>TR块中歌曲信息列表</returns>
        private List<MusicInfo> MusicInfoBuild(string tr)
        {
            List<MusicInfo> musicInfoList = new List<MusicInfo>();

            // 获取音乐地址
            List<string> musicAddressList = MusicOperator.GetMusicUrlList(tr);
            if (musicAddressList != null && musicAddressList.Count > 0)
            {
                // 过滤掉重复地址,如果过滤后为空,则返回null
                var urlList = musicAddressList;
                if (urlList.Count() == 0)
                {
                    return null;
                }

                // 如果获得地址成功,获得歌曲信息
                GroupCollection group = RegexHelper.GetRegexGroup(tr, SOSO_MUSIC_INFO_PATTEN, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                if (group != null)
                {
                    // 新建歌曲信息列表,设置歌曲信息
                    MusicInfo musicInfo = new MusicInfo();
                    musicInfo.MusicName = EncodeConverter.HtmlDecode(group["MusicName"].Value);
                    musicInfo.SingerName = EncodeConverter.HtmlDecode(group["SingerName"].Value);
                    musicInfo.Album = EncodeConverter.HtmlDecode(group["Album"].Value);
                    musicInfo.MusicSource = "搜搜音乐";

                    // 设置歌曲地址和格式
                    foreach (string address in urlList)
                    {
                        MusicInfo tmpMusic = musicInfo.Clone() as MusicInfo;
                        // 检查地址是否合法
                        if (MusicOperator.urlIsValid(address))
                        {
                            tmpMusic.MusicUrl = address;
                            tmpMusic.MusicFormat = MusicOperator.GetMusicFormat(tmpMusic.MusicUrl);
                            musicInfoList.Add(tmpMusic);
                        }
                    }
                }
                return musicInfoList;
            }
            else
            {
                return musicInfoList;
            }
        }