Ejemplo n.º 1
0
        /// <summary>
        /// 获取必应新闻列表
        /// </summary>
        /// <param name="query">搜索关键词</param>
        /// <param name="count">数量</param>
        /// <param name="offset">偏移量</param>
        /// <param name="market">地区</param>
        /// <param name="freshness">时间频率</param>
        /// <returns></returns>
        public static async Task <List <BingNewsEntity> > GetNewsSearchResults(string query, int count = 20, int offset = 0, string market = "zh-CN", string freshness = "Day")
        {
            var articles = new List <BingNewsEntity>();

            try
            {
                HttpResponseMessage result = await SearchClient.GetAsync(
                    $"{NewsSearchEndPoint}/?q={WebUtility.UrlEncode(query)}&count={count}&offset={offset}&mkt={market}&freshness={freshness}");

                result.EnsureSuccessStatusCode();
                string json = await result.Content.ReadAsStringAsync();

                dynamic data = JObject.Parse(json);

                if (data.value == null || data.value.Count <= 0)
                {
                    return(articles);
                }

                for (int i = 0; i < data.value.Count; i++)
                {
                    var news = new BingNewsEntity
                    {
                        Title         = data.value[i].name,
                        Url           = data.value[i].url,
                        Description   = data.value[i].description,
                        ThumbnailUrl  = data.value[i].image?.thumbnail?.contentUrl,
                        Provider      = data.value[i].provider?[0].name,
                        DatePublished = data.value[i].datePublished,
                        Category      = data.value[i].category
                    };
                    if (!string.IsNullOrEmpty(news.ThumbnailUrl))
                    {
                        articles.Add(news);
                    }
                }

                articles = articles.Where(m => m.Category != null && m.Category.Equals("ScienceAndTechnology"))
                           .ToList();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Source + e.Message);
            }

            return(articles);
        }
Ejemplo n.º 2
0
        public static async Task <List <BingNewsEntity> > GetTopNews(string category = "ScienceAndTechnology")
        {
            var articles = new List <BingNewsEntity>();

            try
            {
                HttpResponseMessage result = await SearchClient.GetAsync(
                    $"{TopNewsSearchEndPoint}/?category={category}");

                result.EnsureSuccessStatusCode();
                string json = await result.Content.ReadAsStringAsync();

                dynamic data = JObject.Parse(json);

                if (data.value == null || data.value.Count <= 0)
                {
                    return(articles);
                }

                for (int i = 0; i < data.value.Count; i++)
                {
                    var news = new BingNewsEntity
                    {
                        Title         = data.value[i].name,
                        Url           = data.value[i].url,
                        Description   = data.value[i].description,
                        ThumbnailUrl  = data.value[i].image?.thumbnail?.contentUrl,
                        Provider      = data.value[i].provider?[0].name,
                        DatePublished = data.value[i].datePublished,
                        Category      = data.value[i].category
                    };
                    if (!string.IsNullOrEmpty(news.ThumbnailUrl))
                    {
                        articles.Add(news);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Source + e.Message);
            }
            return(articles);
        }