public static async Task <List <TrendingRepo> > Trending(String Period = "daily", String Language = "")
        {
            ATLog.Info("    =>正在进行网络请求,解析HTML文件");
            List <TrendingRepo> Repos = new List <TrendingRepo>();

            //解决远程身份验证问题
            ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
            using (var web = new HttpClient())
            {
                var result = await web.GetAsync(GetUrl(Period, Language));

                if (result.IsSuccessStatusCode)
                {
                    var htmlData = await result.Content.ReadAsStringAsync();

                    var doc = new HtmlDocument();
                    doc.LoadHtml(htmlData);

                    var baseRepos = doc.DocumentNode.Descendants().Where(x => x.Name == "ol").Select(x => x.ChildNodes).First().Where(x => x.Name == "li").Select(x => x);

                    foreach (HtmlNode repo in baseRepos)
                    {
                        var tempTrendingRepo = new TrendingRepo();
                        var childDivs        = repo.ChildNodes.Where(x => x.Name == "div").Where(x => x.HasAttributes).Where(x => x.GetAttributeValue("class", "") != "float-right");


                        var repoOwnerData = childDivs.ElementAt(0).ChildNodes.Where(x => x.Name == "h3").First().ChildNodes.Where(x => x.Name == "a").First().ChildNodes;

                        var repoURLData = childDivs.ElementAt(0).ChildNodes.Where(x => x.Name == "h3").First().ChildNodes.Where(x => x.Name == "a");

                        tempTrendingRepo.RepoOwner = GetRepoOwner(repoOwnerData);
                        tempTrendingRepo.RepoTitle = GetRepoTitle(repoOwnerData);
                        tempTrendingRepo.Url       = GetRepoURL(repoURLData);

                        tempTrendingRepo.RepoDescription = GetRepoDescription(childDivs.ElementAt(1));

                        tempTrendingRepo.Language = GetRepoLanguage(childDivs.ElementAt(2));

                        tempTrendingRepo.Stars      = GetRepoStars(childDivs.ElementAt(2));
                        tempTrendingRepo.Forks      = GetRepoForks(childDivs.ElementAt(2));
                        tempTrendingRepo.StarsToday = GetRepoStarsToday(childDivs.ElementAt(2));
                        Repos.Add(tempTrendingRepo);
                    }
                }
            }

            if (Repos.Count != 0)
            {
                GithubOp.Instance.SaveRangeData(Repos);
            }

            return(Repos);
        }
示例#2
0
        public async Task <IEnumerable <TrendingRepo> > GetTrending([FromQuery] string Period = "daily", [FromQuery] string Language = "")
        {
            List <TrendingRepo> Repos = new List <TrendingRepo>();

            using (var Web = new HttpClient())
            {
                var result = await Web.GetAsync(TrendingReposUtil.GetUrl(Period, Language));

                if (result.IsSuccessStatusCode)
                {
                    var htmlData = await result.Content.ReadAsStringAsync();

                    var doc = new HtmlDocument();
                    doc.LoadHtml(htmlData);

                    var baseRepos = doc.DocumentNode.Descendants().Where(x => x.Name == "ol").Select(x => x.ChildNodes).First().Where(x => x.Name == "li").Select(x => x);

                    foreach (HtmlNode repo in baseRepos)
                    {
                        var tempTrendingRepo = new TrendingRepo();
                        var childDivs        = repo.ChildNodes.Where(x => x.Name == "div").Where(x => x.HasAttributes).Where(x => x.GetAttributeValue("class", "") != "float-right");


                        var repoOwnerData = childDivs.ElementAt(0).ChildNodes.Where(x => x.Name == "h3").First().ChildNodes.Where(x => x.Name == "a").First().ChildNodes;

                        tempTrendingRepo.RepoOwner = TrendingReposUtil.GetRepoOwner(repoOwnerData);
                        tempTrendingRepo.RepoTitle = TrendingReposUtil.GetRepoTitle(repoOwnerData);


                        tempTrendingRepo.RepoDescription = TrendingReposUtil.GetRepoDescription(childDivs.ElementAt(1));

                        tempTrendingRepo.Language = TrendingReposUtil.GetRepoLanguage(childDivs.ElementAt(2));



                        tempTrendingRepo.Stars      = TrendingReposUtil.GetRepoStars(childDivs.ElementAt(2));
                        tempTrendingRepo.Forks      = TrendingReposUtil.GetRepoForks(childDivs.ElementAt(2));
                        tempTrendingRepo.StarsToday = TrendingReposUtil.GetRepoStarsToday(childDivs.ElementAt(2));
                        Repos.Add(tempTrendingRepo);
                    }
                }
            }

            return(Repos.OrderByDescending(x => x.StarsToday));
        }