Exemplo n.º 1
0
        /// <summary>
        /// 查询游戏信息
        /// </summary>
        /// <param name="keyWords"></param>
        /// <returns></returns>
        public async Task <MSearchPlayerResult> SearchPlayer(string keyWords)
        {
            Task <MSearchPlayerResult>         steamID64Task = SearchPlayerBySteamID64(keyWords);
            Task <MSearchPlayerResult>         customURLTask = SearchPlayerByUrl(keyWords);
            Task <MSearchPlayerResult>         userListTask  = SearchUsers(keyWords);
            List <Task <MSearchPlayerResult> > taskList      = new List <Task <MSearchPlayerResult> > {
                steamID64Task, customURLTask, userListTask
            };
            MSearchPlayerResult result = new MSearchPlayerResult();

            while (taskList.Count > 0)
            {
                Task <MSearchPlayerResult> finishedTask = await Task.WhenAny(taskList);

                MSearchPlayerResult response = await finishedTask;
                if (response?.IsGameInfo ?? false)
                {
                    return(response);
                }
                if (finishedTask == userListTask)
                {
                    result = response;
                }
                taskList.Remove(finishedTask);
            }
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 通过昵称+SessionID+PageNo搜索玩家
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="sessionID"></param>
        /// <param name="pageNo"></param>
        /// <returns></returns>
        public async Task <MSearchPlayerResult> GetPlayerList(string userName, string sessionID, int pageNo)
        {
            string          httpUrl = "https://steamcommunity.com/search/SearchCommunityAjax?text=" + userName + $"&filter=users&sessionid={sessionID}&steamid_user=false&page={pageNo}";
            CookieContainer cookie  = new CookieContainer(); //创建Cookie容器对象 (Cookies集合)

            cookie.Add(new Uri("https://steamcommunity.com/"), new Cookie("sessionid", sessionID));
            cookie.Add(new Uri("https://steamcommunity.com/"), new Cookie("steamCountry", "CN%7C65c3258fe7f771ce24de930fa87b1495"));
            string response;

            try
            {
                response = await HttpHelper.HttpGet(httpUrl, cookie);
            }
            catch (Exception)
            {
                return(null);
            }
            if (string.IsNullOrWhiteSpace(response))
            {
                return(null);
            }
            JObject obj = JObject.Parse(response);

            if ((int)obj["success"] != 1)
            {
                return(null);
            }
            MSearchPlayerResult result = new MSearchPlayerResult
            {
                Total     = (int)obj["search_result_count"],
                PageNo    = (int)obj["search_page"],
                SessionID = sessionID
            };
            string            html       = obj["html"].ToString();
            MatchCollection   avatorList = Regex.Matches(html, @"(?<=img\s?src\s?=\s?"")[^>]*(?="">)", RegexOptions.IgnoreCase);
            MatchCollection   nameList   = Regex.Matches(html, @"(?<=>)((?!</a>).)*(?=</a><br\s{1}/>)", RegexOptions.IgnoreCase);
            MatchCollection   contentID  = Regex.Matches(html, @"(?<=<a\s{1}href=""https://steamcommunity.com/)((?!><img).)*(?="">)", RegexOptions.IgnoreCase);
            List <PlayerInfo> playerList = new List <PlayerInfo>();

            for (int i = 0; i < avatorList.Count; i++)
            {
                string[] contentOfID = contentID?[i]?.Value.Split("/");
                playerList.Add(new PlayerInfo
                {
                    Avatar       = avatorList[i]?.Value,
                    UserName     = nameList[i]?.Value,
                    ProfilesOrID = contentOfID?[0],
                    ContentOfID  = contentOfID?[1]
                });
            }
            result.PlayerList = playerList;
            return(result);
        }