Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJoe Whitehead
2,179 Points2023 Bing API - Working Example
I had an issue with the changes that have come in since this video was made, especially the Bing API, thankfully after a bit of searching and re-watching the videos I've a solution that works. Helpful resources for me were:
From the API I was able to see what the json response would have in it so I could pick what I wanted to deserialize and it simply looks like this:
public class NewsSearch
{
[JsonProperty(PropertyName = "value")]
public List<NewsResult> NewsResults { get; set; }
}
public class NewsResult
{
[JsonProperty(PropertyName = "name")]
public string Headline { get; set; }
[JsonProperty(PropertyName = "description")]
public string Summary { get; set; }
[JsonProperty(PropertyName = "category")]
public string Category { get; set; }
[JsonProperty(PropertyName = "datePublished")]
public DateTime DatePublished { get; set; }
}
You can add more properties but I only wanted these ones.
Then I used the SDK example to help with getting the web result, feeding it into the StreamReader and JsonTextReader to deserialize it into the NewsResult list.
public static List<NewsResult> GetNewsForPlayer(string playerName)
{
string subscriptionKey = "YOUR_API_KEY";
string endpoint = "https://api.bing.microsoft.com/v7.0/news/search";
var results = new List<NewsResult>();
Console.WriteLine("Searching the Web for: " + playerName);
// Construct the URI of the search request
var uriQuery = endpoint + "?q=" + Uri.EscapeDataString(playerName);
// Perform the Web request and get the response
WebRequest request = HttpWebRequest.Create(uriQuery);
request.Headers["Ocp-Apim-Subscription-Key"] = subscriptionKey;
HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result;
var serializer = new JsonSerializer();
using (var reader = new StreamReader(response.GetResponseStream()))
using (var jsonReader = new JsonTextReader(reader))
{
results = serializer.Deserialize<NewsSearch>(jsonReader).NewsResults;
}
return results;
}
The rest of it can then follow on with the tutorial to loop through your players and output the results
var topTenPLayers = GetTopTenPlayers(players);
foreach (var player in topTenPLayers)
{
List<NewsResult> newsResults = GetNewsForPlayer(string.Format("{0} {1}", player.FirstName, player.SecondName));
foreach(var result in newsResults)
{
Console.WriteLine(string.Format("Date: {0:f}\r\nHeadline: {1}\r\nSummary: {2}\r\n", result.DatePublished, result.Headline, result.Summary));
Console.ReadKey();
}
}
Hope this helps, as a fellow student I would also appreciate if someone notices I have done anything which is bad practice or any improvements that can be made to the code.