/// <summary>
        /// Function to retrieve the latests news. With a successful response, it will call ParseNews with the first result
        /// </summary>
        /// <param name="queryString">The query to search for news with</param>
        /// <returns></returns>
        private async Task GetLatestNews(string queryString)
        {
            BingNewsResponse news = await _bingSearch.SearchNews(queryString, SafeSearch.Moderate);

            if (news != null && (news.value == null || news.value.Length == 0))
            {
                return;
            }

            await ParseNews(news.value[0]);
        }
Exemple #2
0
        /// <summary>
        /// Function to parse news search responses
        /// Displays news article name, publish date and a short description in the UI
        /// </summary>
        /// <param name="bingNewsResponse"></param>
        private void ParseNewsResponse(BingNewsResponse bingNewsResponse)
        {
            StringBuilder sb = new StringBuilder();

            foreach (Value news in bingNewsResponse.value)
            {
                sb.AppendFormat("{0}\n", news.name);
                sb.AppendFormat("Published: {0}\n", news.datePublished);
                sb.AppendFormat("{0}\n\n", news.description);
            }

            SearchResults = sb.ToString();
        }
        public async Task <BingNewsResponse> SearchNews(string query, SafeSearch safeSearch)
        {
            string endpoint = string.Format("{0}{1}&safeSearch={2}&count=5&mkt=en-US", "https://api.cognitive.microsoft.com/bing/v5.0/news/search?q=", query, safeSearch.ToString());

            try
            {
                BingNewsResponse response = await _webRequest.MakeRequest <BingNewsResponse>(endpoint);

                return(response);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return(null);
        }
        public async Task <BingNewsResponse> SearchNewsCategory(string query)
        {
            string endpoint = string.Format("{0}{1}&mkt=en-US", "https://api.cognitive.microsoft.com/bing/v5.0/news?category=", query);

            try
            {
                BingNewsResponse response = await _webRequest.MakeRequest <BingNewsResponse>(endpoint);

                return(response);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return(null);
        }