Reputation: 11
I need a way to use kby Date with bing search API in Windows Azure Marketplace to get latest news (last 24 hrs for example) in c sharp code, or any other way to control the news retrieved by news service operation to be uptodated (only latest news during the day).
Upvotes: 1
Views: 1457
Reputation: 4567
Here is the Bing API v2 reference.
And here is the code samples of how to retrieve News.
Note that code samples are written on JS, but they look pretty clearly and could be easily converted to c#.
p.s. I didn't explicit piece of code doing like get the news for last 24 hrs
, however there is such nice thing:
for (var i = 0; i < results.length; ++i)
{
// omitted to make answer shorted
resultStr = "<a href=\""
+ results[i].Date // <--
// omitted to make answer shorted
}
UPDATE: How to get only news for last 24 hrs
I see the solution for getting news for last 24 hr's in the following way:
Let's define stale news item
as news with days out of 24 hr frame, and let 'fresh' is the opposite.
Ignore stale ones. Assing N = count of fresh news items.
Repeat steps 2-3 each next time in order to have news up to date.
Disclaimer Please note that the algorithm is very far from optimal in terms of performance, it's supposed only to demonstrate the main idea.
*
How to load next N news items. Should be achiveable by loading data as pages via "$top" and "$skip" query options. In Quick Start Guide is the sample how get news('Executing a news Service Operion' section).
// This is the query expression.
string query = "Xbox Live Games";
// Create a Bing container.
string rootUrl = "https://api.datamarket.azure.com/Bing/Search";
var bingContainer = new Bing.BingSearchContainer(new Uri(rootUrl));
// The market to use.
string market = "en-us";
// Get news for science and technology.
string newsCat = "rt_ScienceAndTechnology";
// Configure bingContainer to use your credentials.
bingContainer.Credentials = new NetworkCredential(AccountKey, AccountKey);
// Build the query, limiting to 10 results.
var newsQuery =
bingContainer.News(query, null, market, null, null, null, null, newsCat, null);
newsQuery = newsQuery.AddQueryOption("$top", 10);
// Run the query and display the results.
var newsResults = newsQuery.Execute();
foreach (var result in newsResults)
{
Console.WriteLine("{0}-{1}\n\t{2}",
result.Source, result.Title, result.Description);
}
Pay attention to line newsQuery = newsQuery.AddQueryOption("$top", 10);
. It should be possible(not sure if it is) to specify "$skip"
options, which makes you capable of using the paging functionality.
Hope this helps.
Upvotes: 1