/// <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); }
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); }
public static async Task <IEnumerable <string> > GetImageSearchResults(string query, string imageContent = "Face", int count = 20, int offset = 0) { var urls = new List <string>(); HttpResponseMessage result = await SearchClient.GetAsync( $"{ImageSearchEndPoint}?q={WebUtility.UrlEncode(query)}&safeSearch=Strict&imageType=Photo&color=ColorOnly&count={count}&offset={offset}{(string.IsNullOrEmpty(imageContent) ? "" : "&imageContent=" + imageContent)}"); result.EnsureSuccessStatusCode(); string json = await result.Content.ReadAsStringAsync(); dynamic data = JObject.Parse(json); if (data.value == null || data.value.Count <= 0) { return(urls); } for (int i = 0; i < data.value.Count; i++) { urls.Add(data.value[i].contentUrl.Value); } return(urls); }