示例#1
0
 public static SearchResult Search(string key, int page, int size)
 {
     var result = new SearchResult
     {
         ErrorCode = 200,
         ErrorMsg = "OK",
         KeyWord = key,
         PageNum = page,
         TotalSize = -1,
         Songs = new List<SongResult>()
     };
     var url = "http://search.dongting.com/song/search?page=" + page + "&user_id=0&tid=0&app=ttpod&size=" + size + "&q=" + key + "&active=0";
     var html = CommonHelper.GetHtmlContent(url);
     if (string.IsNullOrEmpty(html))
     {
         result.ErrorCode = 300;
         result.ErrorMsg = "获取搜索结果信息失败";
         return result;
     }
     try
     {
         var json = JObject.Parse(html);
         if (json["totalCount"].ToString() == "0")
         {
             result.ErrorCode = 404;
             result.ErrorMsg = "没有找到符合要求的歌曲";
             return result;
         }
         result.TotalSize = json["totalCount"].Value<int>();
         var datas = json["data"];
         result.Songs = GetListByJson(datas);
         return result;
     }
     catch (Exception ex)
     {
         CommonHelper.AddLog(ex);
         result.ErrorCode = 500;
         result.ErrorMsg = "解析歌曲时发生错误";
         return result;
     }
 }
示例#2
0
        //该API包含的歌曲信息十分全面,但是无法获取搜索结果总数目以及无法指定单页数量
        //附带另一个搜索API:http://api.xiami.com/web?v=2.0&app_key=1&key=hello&page=1&limit=30&r=search/songs
        //该API需要指定Refer:http://m.xiami.com/
        //否则提示非法请求
        public static SearchResult Search(string key, int page)
        {
            var url = "http://www.xiami.com/app/xiating/search-song2?key=" + key + "&uid=0&callback=xiami&page=" + page;
            var html = CommonHelper.GetHtmlContent(url);
            var result = new SearchResult
            {
                ErrorCode = 200,
                ErrorMsg = "OK",
                KeyWord = key,
                PageNum = page,
                TotalSize = -1,     //-1表示未知
                Songs = new List<SongResult>()
            };
            if (string.IsNullOrEmpty(html) || html == "null" || html.Contains("无法在虾米资料库中得到结果"))
            {
                result.ErrorCode = 404;
                result.ErrorMsg = "没有找到符合要求的歌曲";
                return result;
            }
            try
            {
                var json = JObject.Parse(html);
                var data = json["data"];
                foreach (var j in data)
                {
                    try
                    {
                        if (j["song_status"].ToString() != "0")
                        {
                            continue;   //滤除下架歌曲
                        }
                        var song = new SongResult
                        {
                            SongId = j["song_id"].ToString(),
                            SongName = j["song_name"].ToString(),
                            SongSubName = j["sub_title"]?.ToString(),
                            SongLink = "http://www.xiami.com/song/" + j["song_id"],

                            ArtistId = j["artist_id"].ToString(),
                            ArtistName = j["singer"].ToString(),
                            ArtistSubName = j["artist_sub_title"]?.ToString(),

                            AlbumId = j["album_id"].ToString(),
                            AlbumName = j["album_name"].ToString(),
                            AlbumSubName = j["album_sub_title"]?.ToString(),
                            AlbumArtist = j["artist_name"].ToString(),

                            Length = j["songtime"].ToString(),
                            Size = "",
                            BitRate = "320K",

                            FlacUrl = "",
                            ApeUrl = "",
                            WavUrl = "",
                            SqUrl = CommonHelper.GetSongUrl("xm", "320", j["song_id"].ToString(), "mp3"),
                            HqUrl = CommonHelper.GetSongUrl("xm", "192", j["song_id"].ToString(), "mp3"),
                            LqUrl = CommonHelper.GetSongUrl("xm", "128", j["song_id"].ToString(), "mp3"),
                            CopyUrl = CommonHelper.GetSongUrl("xm", "320", j["song_id"].ToString(), "mp3"),

                            PicUrl = ("http://img.xiami.net/" + j["album_logo"]).Replace("_1.", "_4."),
                            LrcUrl = j["lyric"].ToString(),
                            TrcUrl = "",
                            KrcUrl = j["lyric_karaok"]?.ToString(),

                            MvId = j["mv_id"]?.ToString(),
                            MvHdUrl = "",
                            MvLdUrl = "",

                            Language = "",
                            Company = "",
                            Year = CommonHelper.UnixTimestampToDateTime(Convert.ToInt64(string.IsNullOrEmpty(j["gmt_publish"].ToString()) ? "0" : j["gmt_publish"].ToString())).ToString("yyyy-MM-dd"),
                            Disc = j["cd_serial"].ToString(),
                            TrackNum = j["track"].ToString(),
                            Type = "xm"
                        };
                        if (!string.IsNullOrEmpty(song.LrcUrl))
                        {
                            if (song.LrcUrl.EndsWith("txt"))
                            {
                                song.LrcUrl = CommonHelper.GetSongUrl("xm", "128", song.SongId, "lrc");
                            }
                        }
                        if (!string.IsNullOrEmpty(song.MvId))
                        {
                            if (song.MvId != "0")
                            {
                                song.MvHdUrl = CommonHelper.GetSongUrl("xm", "hd", song.SongId, "mp4");
                                song.MvLdUrl = CommonHelper.GetSongUrl("xm", "ld", song.SongId, "mp4");
                            }
                        }
                        result.Songs.Add(song);
                    }
                    catch (Exception ex)
                    {
                        CommonHelper.AddLog(ex);
                    }
                }
                return result;
            }
            catch (Exception)
            {
                result.ErrorCode = 500;
                result.ErrorMsg = "解析歌曲时发生错误";
                return result;
            }
        }