C# C# Streams and Data Processing Streaming Data on the Net Searching for News Headlines

Simon Sporrong
Simon Sporrong
33,747 Points

I made a new GetNewsForPlayer-method, following the new docs

So, if you are taking this course now (in 2019) the Microsoft docs that Carling is using is heavily outdated and is really cumbersome to use. So i followed the new updated docs and made a new GetNewsForplayer-method that works just fine and is easer to use since it doesn't require Streams and Readers.

The docs can be found here: https://docs.microsoft.com/sv-se/azure/cognitive-services/bing-News-search/news-search-sdk-quickstart

Note: You have to install the Azure.CognitiveServices.Search.NewsSearch package. Right click on your solution and select 'Manage NuGet Packages' and search for: Azure Cognitive News and select the Microsoft.Azure.CognitiveServices.Search.NewsSearch package. You must also use the namespace up top:

using Microsoft.Azure.CognitiveServices.Search.NewsSearch;

And the method

public static string GetNewsForPlayer(string playerName)
        {
            var key = YOUR_API_KEY
            var client = new NewsSearchClient(new ApiKeyServiceClientCredentials(key));
            var newsResults = client.News.SearchAsync(query: playerName, market: "en-us", count: 10).Result;
            if (newsResults.Value.Count > 0)
            {
                return newsResults.Value[0].Description;
            }
            else
            {
                return "No news found on this player";
            }

        }

The method can most certainly be enhanced but at least it does the job.

Hope that it can help someone!

Christopher Coscina
Christopher Coscina
4,919 Points

I agree, it was difficult to use. I was able to find a way to get it working with the newer version but it took some tinkering. I suppose that's valuable experience though! Great job finding another work around.

Hugo Zaanen
Hugo Zaanen
5,167 Points

You need an account and get a subscription to get the API Key?

1 Answer

Simon Sporrong
Simon Sporrong
33,747 Points

Hugo Zaanen You don't need a subscription key for the NewsSearch but later in the course where we're using TextAnalytics you need a subscription in order to get the correct endpoint, key and region.