This C# .NET DayPI client is contributed by Tikiri, co-founder and star developer at Chillx.com.
Replace your Accesskey and SharedSecret in the attached code and you are ready to make the DayPI calls from your C# .NET app.
Below is sample code that demonstrates how you can use this library.
DayLifeAPI.Data.Topic objTopic; DayLifeAPI.Data.Article objArticle; DayLifeAPI.Data.Quote objQuote; DayLifeAPI.Data.SearchResult DayLifeSearchResult; DayLifeAPI.DayRequest RequestAPI; ListSearchTopicTypes; DateTime StartDate; DateTime EndDate; int Offset; int Limit; string Query; // Create new daylife request api object RequestAPI = new DayLifeAPI.DayRequest(); // Note your access key and shared secred need to be set in the DayRequest constructor #region "Get Specific Topic Example" //Example get specific topic objTopic = RequestAPI.GetTopicByID("03yu9dZ7aa6lP"); //Or objTopic = RequestAPI.GetTopicByName("Amitabh Bachchan"); //All done //Check response data packet is valid if (objTopic.prop_IsValid) { //The Topic object defines all the data as properties //objTopic.prop_TopicID; //objTopic.prop_TopicName; if (objTopic.prop_TopicType == DayLifeAPI.Data.Topic.enum_TopicType.person) { /* do something */ }; if (objTopic.prop_TopicType == DayLifeAPI.Data.Topic.enum_TopicType.place) { /* do something */ }; if (objTopic.prop_TopicType == DayLifeAPI.Data.Topic.enum_TopicType.organization) { /* do something */ }; if (objTopic.prop_TopicType == DayLifeAPI.Data.Topic.enum_TopicType.Unknown) { /* something is wrong */ }; //objTopic.prop_DaylifeUrl; if (objTopic.prop_HasWikipediaInfo) { //objTopic.prop_WikipediaURL; //objTopic.prop_WikipediaAbstract; } } else { // Something is wrong no data recieved - Usually topic ID or topic name is invalid, or possibly http error (501 bad gateway) } #endregion #region "Get specific article example" //Example get specific article objArticle = RequestAPI.GetArticle("06px1XH1su3mv"); //All done //Check response data packet is valid if (objArticle.prop_IsValid) { //The Article object defines all the data as properties //objArticle.prop_ArticleID; //objArticle.prop_ArticleDate; //objArticle.prop_URL; //objArticle.prop_DaylifeURL; //objArticle.prop_Headline; //objArticle.prop_Excerpt; //objArticle.prop_TimeStamp; if (objArticle.prop_Source.prop_IsValid) { //objArticle.prop_Source.prop_SourceID; //objArticle.prop_Source.prop_Name; //objArticle.prop_Source.prop_URL; //objArticle.prop_Source.prop_DaylifeURL; //objArticle.prop_Source.prop_Rank; //objArticle.prop_Source.prop_SourceType; } } else { // Something is wrong no data recieved - Usually article ID is invalid, or possibly http error (501 bad gateway) } #endregion #region "Get specific quote example" //Example get specific quote objQuote = RequestAPI.GetQuote("09XH5Db7uO7sq"); //All done //Check response data packet is valid if (objQuote.prop_IsValid) { //The Quote object defines all the data as properties //objQuote.prop_QuoteID; //objQuote.prop_QuoteText; //objQuote.prop_QuoteDate; //objQuote.prop_DaylifeURL; //objQuote.prop_ArticleID; if (objQuote.prop_Source.prop_IsValid) { //objQuote.prop_Source.prop_SourceID; //objQuote.prop_Source.prop_Name; //objQuote.prop_Source.prop_URL; //objQuote.prop_Source.prop_DaylifeURL; //objQuote.prop_Source.prop_Rank; //objQuote.prop_Source.prop_SourceType; } //This is a special one //The following property will make a request and get the article the first time it is accessed //objQuote.prop_Article; //objQuote.prop_Article.prop_Headline //etc... } else { // Something is wrong no data recieved - Usually quote ID is invalid, or possibly http error (501 bad gateway) } #endregion //---------Searching--------- // Search Types Implemented // // SearchRelatedTopics // SearchMatchingTopics // SearchRelatedArticles // SearchRelatedQuotes // SearchQuotesAbout // SearchQuotesBy // TopicGetRelatedTopicsByID // TopicGetRelatedQuotesByID // TopicGetRelatedArticlesByID // TopicGetRelatedTopicsByName // TopicGetRelatedQuotesByName // TopicGetRelatedArticlesByName // QuoteGetRelatedTopics // QuoteGetRelatedQuotes // QuoteGetRelatedArticles // ArticleGetTopics // ArticleGetRelatedTopics // ArticleGetRelatedQuotes // ArticleGetRelatedArticles // ArticleGetQuotes // Search Types NOT Implemented - I felt lazy // TopicGetTimeLineByID // TopicGetTimeLineByName // Common parameter set that we will use in all the example calls that follow; StartDate = ((DateTime)DateTime.Now.Subtract(TimeSpan.FromDays(30))).Date; EndDate = DateTime.Now.AddDays(1); Offset = 0; Limit = 10; #region "Example Search Call - Topic Get Related Topics By Topic Name" Query = "Amitabh Bachchan"; SearchTopicTypes = new List (); SearchTopicTypes.Add(DayLifeAPI.Data.Topic.enum_TopicType.person); SearchTopicTypes.Add(DayLifeAPI.Data.Topic.enum_TopicType.place); DayLifeSearchResult = RequestAPI.Search(DayRequest.enum_DayRequestSearchType.TopicGetRelatedTopicsByName, Query, DayRequest.enum_SortOrder.relevance, SearchTopicTypes, StartDate, EndDate, Offset, Limit); if (DayLifeSearchResult.prop_Success) { MessageBox.Show(string.Concat("Success, Topics found = ", DayLifeSearchResult.prop_SearchTopicsList.Count.ToString())); foreach (DayLifeAPI.Data.Topic RelatedTopic in DayLifeSearchResult.prop_SearchTopicsList) { MessageBox.Show(string.Concat("Topic Name: ", RelatedTopic.prop_TopicName)); MessageBox.Show(string.Concat("Topic ID: ", RelatedTopic.prop_TopicID)); //And so on RelatedTopic.prop_TopicType, RelatedTopic.prop_DaylifeUrl etc.... } } else { MessageBox.Show(string.Concat("Failed, Error Code = ", DayLifeSearchResult.prop_ResultCode)); MessageBox.Show(string.Concat("Failed, Error Message = ", DayLifeSearchResult.prop_ResultMessage)); } #endregion #region "Example Search Call - Topic Get TimeLine By Topic ID" Query = "03yu9dZ7aa6lP"; DayLifeSearchResult = RequestAPI.Search(DayRequest.enum_DayRequestSearchType.TopicGetRelatedArticlesByID, Query, DayRequest.enum_SortOrder.date, null, StartDate, EndDate, Offset, Limit); if (DayLifeSearchResult.prop_Success) { MessageBox.Show(string.Concat("Success, Articles found = ", DayLifeSearchResult.prop_SearchArticlesList.Count.ToString())); foreach (DayLifeAPI.Data.Article RelatedArticle in DayLifeSearchResult.prop_SearchArticlesList) { //Do stuff with //RelatedArticle.prop_ArticleDate; //RelatedArticle.prop_ArticleID; //RelatedArticle.prop_Headline; //RelatedArticle.prop_Excerpt; //etc.... } } else { MessageBox.Show(string.Concat("Failed, Error Code = ", DayLifeSearchResult.prop_ResultCode)); MessageBox.Show(string.Concat("Failed, Error Message = ", DayLifeSearchResult.prop_ResultMessage)); } #endregion #region "Example Search Call - Topic Get Related Articles By Topic ID" Query = "Amitabh Bachchan"; DayLifeSearchResult = RequestAPI.Search(DayRequest.enum_DayRequestSearchType.TopicGetRelatedQuotesByName, Query, DayRequest.enum_SortOrder.relevance, null, StartDate, EndDate, Offset, Limit); if (DayLifeSearchResult.prop_Success) { MessageBox.Show(string.Concat("Success, Quotes found = ", DayLifeSearchResult.prop_SearchQuotesList.Count.ToString())); foreach (DayLifeAPI.Data.Quote RelatedQuote in DayLifeSearchResult.prop_SearchQuotesList) { // Do stuff with //RelatedQuote.prop_QuoteID; //RelatedQuote.prop_QuoteText; //RelatedQuote.prop_Source.prop_Name; //RelatedQuote.prop_Source.prop_SourceID; //RelatedQuote.prop_ArticleID; //RelatedQuote.prop_Article.prop_ArticleDate; //RelatedQuote.prop_Article.prop_Headline; //etc... } } else { MessageBox.Show(string.Concat("Failed, Error Code = ", DayLifeSearchResult.prop_ResultCode)); MessageBox.Show(string.Concat("Failed, Error Message = ", DayLifeSearchResult.prop_ResultMessage)); } #endregion
| Attachment | Size |
|---|---|
| DayLifeAPI_DotNet.zip | 202.56 KB |
| DayLifeAPI_DotNet_with_caching.zip | 226 KB |
| DayLifeAPI_WithImages.zip | 255.39 KB |
Comments
With Caching
The C# .NET client implementing caching: http://cookbook.daylife.com/sites/default/files/DayLifeAPI_DotNet_with_caching.zip
// DayRequest uses a threadsafe internal cache. What this means is that the response for each API request is cached on
// the first request and returned from cache the second time around. the benefits are:
// 1) Wastefull api calls are not made for the same request again and again
// 2) response from cache is heaps faster
// Note the internal cache behaviour is defined by three constants in DayLifeAPI.DayRequest Class:
// 1) CacheRequestsForMinutes
/// Items expire from the cache after the number of minutes specified in MaxItemsInCache
/// Set this depending on the minimum freshness you want for your data.
// 2) MaxItemsInCache
/// The cache will hold a maximum number of items as specified by MaxItemsInCache
/// The more items you have in cache the greater the memory footprint.
/// If you are likely to make 100 unique requests many times during the time in CacheRequestsForMinutes above
/// then set this to 100
// 3) PruneCacheEveryNumRequests
/// The cache will be pruned (expired and excess items removed) every X number of requests
/// Where X is PruneCacheEveryNumRequests.
/// If you make say 100 requests in 10 minutes then set this to 100
// To be adjusted these to FIT the applications usage patterns.
With Caching and Images
This version extends the search method to be able to handle the DayPI search_getRelatedImages API method.
It is Essentially similar to the other calls. The only thing to bear in mind is that you have to use the overloaded version of the Search function which takes a source filter. Without your source filter ID getting images is rather messy. The resulting list of DayImage objects provide properties on most of the relevant XML data. Do bear in mind however that DayImageObject.prop_Credit and DayImageObject.prop_Source.prop_Name may not be the same. For example in the case of Getty Images AFP the credit will be AFP/Getty Images while the Source name will be Getty Images.
To download the image in a given width you could use the following code snippet
public System.Drawing.Image DownloadImage(string ImageID, int Width)
{
System.Net.WebRequest APIRequest;
System.Net.WebResponse APIResponse;
System.Drawing.Image APIImage;
string ImageURL;
ImageURL = string.Concat("http://cache.daylife.com/imageserve/", ImageID, "/", Width.ToString(), "x.jpg");
APIRequest = System.Net.WebRequest.Create(ImageURL);
APIRequest.Timeout = 60000;
APIResponse = APIRequest.GetResponse();
APIImage = System.Drawing.Image.FromStream(APIResponse.GetResponseStream());
APIResponse.Close();
APIResponse = null;
APIRequest = null;
return APIImage;
}
Extract from Usageexample.txt for how to call the search images method.
Query = "Amitabh Bachchan";
DayLifeSearchResult = RequestAPI.Search(DayRequest.enum_DayRequestSearchType.SearchGetRelatedImages, Query, DayRequest.enum_SortOrder.relevance, null, StartDate, EndDate, Offset, Limit, "Your Source Filter ID");
if (DayLifeSearchResult.prop_Success)
{
foreach (DayLifeAPI.Data.DayImage RelatedImage in DayLifeSearchResult.prop_SearchImagesList)
{
// Do something with
//RelatedImage.prop_ImageID;
//RelatedImage.prop_Caption;
//RelatedImage.prop_Credit;
//RelatedImage.prop_Height;
//RelatedImage.prop_ImageDate;
//RelatedImage.prop_ThumbURL;
//RelatedImage.prop_ImageURL;
//RelatedImage.prop_DaylifeURL;
if (RelatedImage.prop_Source.prop_IsValid)
{
////RelatedImage.prop_Source.prop_SourceID
////RelatedImage.prop_Source.prop_Name
////RelatedImage.prop_Source.prop_DaylifeURL
////etc...
}
//etc...
}
}
else
{
MessageBox.Show(string.Concat("Failed, Error Code = ", DayLifeSearchResult.prop_ResultCode));
MessageBox.Show(string.Concat("Failed, Error Message = ", DayLifeSearchResult.prop_ResultMessage));
}
Cheers
Tikiri
daylife constructor..
your library throws an exception in the default constructor
public DayRequest()
{
m_DayLifeURL = "http://freeapi.daylife.com/xmlrest/publicapi/4.4/";
throw new Exception("Please setup your Access Key and Shared Secret in DayLifeAPI.DayRequest Constructor. Also adjust the three constants above to fit your usage patterns.");
m_AccessKey = "Your Access Key Here";
m_SharedSecret = "Your Shared Secret Here";
}
Daylife no exception in constructor
The new version of the library do not throw an exception out of the constructor. Instead you pass in the access key and the shared secret each time. When a quote tries to automatically retrieve it's article then the last used access key and shared secret will be used.
Daylife constructor exception
I added the exception in the constructor to signify that something has to be done there before it will work.
The client needs your access key and your shared secret in order to work. So all the exception is saying is please add your access key and shared secred. then delete the exception line.
therefore:
Enter your access key where it says m_AccessKey = "Your Access Key Here";
enter your shared secret where it says m_SharedSecret = "Your Shared Secret Here";
then delete the line that throws the exception. Which looks like
throw new Exception("Please setup your Access Key and Shared Secret in DayLifeAPI.DayRequest Constructor. Also adjust the three constants above to fit your usage patterns.");
Optionally adjust the values in the three constants above the constructor.
This is the only human involvement thats needed to use the library.
Cheers Tikiri