Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

digg API.NET - C# Implementation of digg Web Services

Rate me:
Please Sign up or sign in to vote.
3.92/5 (5 votes)
5 Oct 20075 min read 84K   275   28   17
A C# Library Interface For the Digg.com API

Download DiggAPI.zip - 30.6 KB

Screenshot - baileyrss01.jpg

example implementation

Table of Contents

Introduction
Background
The Digg API
The Base Url
The Application Key
Parameters
Endpoints
The C# digg Interface
StoryCollection Object
Story Object
Code Examples
Setting Library Properties
Get Most Popular Stories
Get Upcoming Stories
Get Stories By Topic
Get Upcoming Stories By topic
Get Stories Submitted By a User
Get Stories a User Has Digged
Get Story By it's ID
Get Comment For a Story
Get Valid Topics
Conclusion
ToDo
History

Introduction

Digg.com is the popular social news web-site that allows it's community to dictate which articles make it to the site's front page and which end up in the pits of internet hell, never to be read... by anyone. The concept is simple, digg.com members search the web for articles that they find interesting and then submit them to the site. The users of the site vote (or digg) on the articles and if enough people vote on the article it will be promoted to the front page. In addition, each article maintains a comment page where digg users can comment (or flame one another endlessly) on topics relating to the article.

Aside from being entertaining, digg.com provides a wealth of priceless information regarding comtemporary culture, science, sports, business, lifestyle, and technology. In the past, aggregating this data meant one had to use screen-scraping techniques with a mountain of Regular Expressions. Thankfully, digg has now provided users with a rich API for getting at it's data. My goal was to abstract the interface into a .NET object oriented library that can be imported into any project. It's still lacking a few features I'd like to add but it's certainly useable.

Background

Quite some time ago I rolled my own personal Rss Aggregator and viewer and at that time I discovered a great open-source component for parsing Rss feeds, Rss.NET. The library had worked flawlessly up until a few months ago when I began to notice that the feeds from digg.com were timing out. The Rss.NET library worked with all my other feeds but for some reason started to receive 403 Forbidden messages while trying to establish the HttpWebRequest to the digg.com servers. At this time I began researching the digg API for an alternate method for obtaining the feeds. While building a .NET library to interface with the digg API I discovered that the Rss.NET library was failing due to not setting all the required properties for the WebRequest object such as UserAgent, Credentials, and Accept. I have since added the necessary properties and sent a copy of the change to the Rss.NET developers.

However, since I had already come this far with the digg API I set about completely the portions I had begun and this article outlines that effort.

The Digg API

The full documentation is available from the digg.com api site.

The Base URL

The base url for accessing the api is http://services.digg.com/. However, if you attempt to access that url on it's own you will receive a (403) Forbidden error due to the fact that you have not provided an appkey value. The appkey is probably going to end up being a unique key that must be purchased once digg sells out, but for the moment, it's any valid url.

Application Key

The application key or appkey value represents the requestor of the data. It can be any valid Url and presently is only used to track which web service users are consuming what data. The appkey value is appended at the end of the query string. All queries must provide an appkey value in order to receive the data.
http://services.digg.com/story/popular?appkey=http://www.codeproject.com

Parameters

The digg.com API documentation contains all the parameters for each request but the most common ones are:
  • count - the amount of records to return (0-99)
  • sort - promote_date-desc, promote_date-asc, submit_date-desc, submit_date-asc
http://services.digg.com/stories/popular?count=25&appkey=http://www.codeproject.com 

Endpoints

Endpoints represent the url location to post your query. Below you'll see most of the endpoints available.
/stories - All stories. 
/stories/popular - Popular stories. 
/stories/upcoming - Upcoming stories. 
/stories/container/{container name} - All stories from a given container.
/stories/container/{container name}/popular Popular stories from a given container.
/stories/container/{container name}/upcoming - All stories from a given topic.
/stories/topic/{topic name}/popular - Most Popular stories in a topic
/stories/topic/{topic name}/upcoming - Upcoming stories from a given topic.
/story/{story id} - Identified story.
/stories/{comma-separated list of story ids} - A list of stories with the given ids.
/user/{user name}/submissions - Stories submitted by given user.
/topics - Get a list of all topics. 
/users - All users. 
/user/{user name} - Named user.
/user/{user name}/friends - Named user's friends.
/user/{user name}/activity - All digg and comment activity for a given user.
/user/{user name}/activity/diggs - All digg activity for a given user. 
/user/{user name}/activity/comments - All comment activity for a given user. 
/story/{story id}/activity - All digg and comment activity for a given story. 
/story/{story id}/activity/diggs - All digg activity for a given story.. 
/story/{story id}/activity/comments - All comment activity for a given story. 

The C# digg Interface

Screenshot - api01.jpg

StoryCollection Object

The StoryCollection is the heart of the library. When the StoryCollection object is instantiated, it creates a Story object to hold all the values of each story. So you just need to enumerate the story object contained in the collection to access the values. In order to construct the object you must send in the:
  • ArticleType - enumeration for the query to run (see below)
  • ArticleTypeCriteria - optional: the search criteria for certain queries
Screenshot - api03.jpg
C#
//In the references add
using Baileysoft.Rss.DiggAPI;



StoryCollection articles = StoryCollection(ArticleType.byUserDiggs, "thund3rstruck");

   foreach (Story article in articles)
        Console.WriteLine(article.Description);

Story Object

The Story type is an object representation of each article. You enumerate the StoryCollection and extract any values you need from the Story objects. See the properties below.

Screenshot - api02.jpg

Code Examples

NOTE: All code examples require a reference in the project to the DiggAPI.dll and declarations
using Baileysoft.Rss.DiggAPI

Setting Library Properties

You can pre-define global settings by changing the field values in Globals.cs or you can define them programmatically (e.g. see below)
C#
Globals.Count = "3"; //max 99
Globals.AppKey = "http://www.codeproject.com"; //the referring site (your site)

Get Most Popular Stories

C#
Globals.Count = "25";
Globals.AppKey = "http://www.codeproject.com";

StoryCollection articles = new StoryCollection(ArticleType.popular);
foreach (Story article in articles)
      Console.WriteLine(article.Title);

Get Upcoming Stories

C#
 Globals.Count = "25";
 Globals.AppKey = "http://www.codeproject.com"; 

StoryCollection articles = new StoryCollection(ArticleType.upcoming);
foreach (Story article in articles)
     Console.WriteLine(article.Title);

Get Stories By Topic

C#
Globals.Count = "25";
Globals.AppKey = "http://www.codeproject.com";

StoryCollection articles = new StoryCollection(ArticleType.topic, "apple"); //must be a valid topic
foreach (Story article in articles)
     Console.WriteLine(article.Title);

Get Upcoming Stories By topic

C#
Globals.Count = "25";
Globals.AppKey = "http://www.codeproject.com";

StoryCollection articles = new StoryCollection(ArticleType.topicUpcoming, "apple"); //must be a valid topic
foreach (Story article in articles)
     Console.WriteLine(article.Title);

Get Stories Submitted By a User

C#
Globals.Count = "25";
Globals.AppKey = "http://www.codeproject.com";

StoryCollection articles = new StoryCollection(ArticleType.byUserSubmissions, "thund3rstruck");
foreach (Story article in articles)
      Console.WriteLine(article.Title);

Get Stories a User Has Digged

C#
Globals.Count = "25";
Globals.AppKey = "http://www.codeproject.com";

StoryCollection articles = new StoryCollection(ArticleType.byUserDiggs, "thund3rstruck");
foreach (Story article in articles)
       Console.WriteLine(article.Title);

Get Story By it's ID

C#
Globals.Count = "25";
Globals.AppKey = "http://www.codeproject.com";

StoryCollection articles = new StoryCollection(ArticleType.byId, "2572048");
foreach (Story article in articles)
       Console.WriteLine(article.Title);

Get Comments for a Story

C#
Globals.Count = "25";
Globals.AppKey = "http://www.codeproject.com"; 

StoryCollection articles = new StoryCollection(ArticleType.byId, "3678318");
foreach (Story article in articles)
{
    foreach (Comment comment in article.Comments)
    Console.WriteLine("{0}: {1}", comment.User, comment.Text);
}

Get Valid Topics

C#
Globals.AppKey = "http://www.codeproject.com";

TopicsCollection topics = new TopicsCollection();
foreach (string topic in topics)
       Console.WriteLine(topic);

Conclusion

This library came entirely out of me not taking the time to dive into RSS.NET to identify why digg feeds timed out with (403) errors. In any event, this was a neat project and if it gets a good reception I may keep working on it.

ToDo:

  • Classes for submitting articles
  • Classes for digging articles

History

I have not updated the documentation in this article yet but there are a few new comment handling types for enumerating article comments. See the download for additional examples.
  • 07/24/2007 - Submitted article for DiggAPI v.01
  • 10/05/2007 - Added methods for enumerating comments

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
United States United States
I'm a professional .NET software developer and proud military veteran. I've been in the software business for 20+ years now and if there's one lesson I have learned over the years, its that in this industry you have to be prepared to be humbled from time to time and never stop learning!

Comments and Discussions

 
QuestionGood ..but I have a problem Pin
ulfat hussain10-May-09 20:13
ulfat hussain10-May-09 20:13 
AnswerRe: Good ..but I have a problem Pin
thund3rstruck11-May-09 2:16
thund3rstruck11-May-09 2:16 
GeneralRe: Good ..but I have a problem Pin
ulfat hussain8-Jun-09 23:50
ulfat hussain8-Jun-09 23:50 
GeneralRe: Good ..but I have a problem [modified] Pin
Timker13-Aug-09 9:11
Timker13-Aug-09 9:11 
I have modified this code to minimize errors related to schema changes in the response. You will notice that I have also changed the type on some properties and even added some.

public static void populateStoryCollection(StoryCollection alStories,
string diggWebServiceUrl)
{
XmlTextReader reader = Parser.CreateWebRequest(diggWebServiceUrl);
XmlDocument doc = new XmlDocument();
doc.Load(reader);

foreach (XmlNode story in doc.SelectNodes("//story"))
{
Story newStory = new Story();
foreach (XmlAttribute attr in story.Attributes)
{
switch (attr.Name)
{
case "link": newStory.Link = attr.Value; break;
case "submit_date":
DateTime submitDate;
TimeSpan submitspan;
long submit = 0;
if (long.TryParse(attr.Value, out submit))
{
submitspan = new TimeSpan(submit);
submitDate = DateTime.Now.Subtract(submitspan);
}
else
{
submitDate = DateTime.MinValue;
}
newStory.SubmitDate = submitDate;
break;
case "diggs":
int diggs = 0;
if (int.TryParse(attr.Value, out diggs))
{
newStory.Diggs = diggs;
}
break;
case "id": newStory.Id = attr.Value; break;
case "comments":
int x = 0;
if (int.TryParse(attr.Value, out x))
{
newStory.CommentCount = x;
}
break;
case "href": newStory.CommentsUrl = attr.Value; break;
case "status": newStory.Status = attr.Value; break;
case "media": newStory.Media = attr.Value; break;
}
}
if(story.SelectSingleNode("description") != null)
newStory.Description = story.SelectSingleNode("description").InnerText;
if (story.SelectSingleNode("title") != null)
newStory.Title = story.SelectSingleNode("title").InnerText;
if (story.SelectSingleNode("user") != null)
{
foreach (XmlAttribute attr in story.SelectSingleNode("user").Attributes)
{
switch (attr.Name)
{
case "icon": newStory.SubmitterIcon = attr.Value; break;
case "name": newStory.Submitter = attr.Value; break;
case "profileviews":
int profileviews = 0;
if (int.TryParse(attr.Value, out profileviews))
{
newStory.SubmitterProfileViews = profileviews;
}
break;
}
}
}
if (story.SelectSingleNode("topic") != null && story.SelectSingleNode("topic").Attributes["name"] != null)
newStory.Topic = story.SelectSingleNode("topic").Attributes["name"].Value;
if (story.SelectSingleNode("container") != null && story.SelectSingleNode("container").Attributes["name"] != null)
newStory.Container = story.SelectSingleNode("container").Attributes["name"].Value;
alStories.Add(newStory);
newStory = new Story();
}
}

I am curious why you were using an integer indexer.

modified on Thursday, August 13, 2009 4:32 PM

GeneralAwesome Pin
iffy4563-Jan-08 11:59
iffy4563-Jan-08 11:59 
Generalreally need to add support for comments Pin
hacx2k64-Oct-07 21:23
hacx2k64-Oct-07 21:23 
GeneralRe: really need to add support for comments Pin
thund3rstruck5-Oct-07 7:11
thund3rstruck5-Oct-07 7:11 
GeneralRe: really need to add support for comments Pin
hacx2k65-Oct-07 7:49
hacx2k65-Oct-07 7:49 
GeneralRe: really need to add support for comments Pin
thund3rstruck5-Oct-07 8:25
thund3rstruck5-Oct-07 8:25 
NewsAnother .Net Digg API + Your API's mention Pin
pag_floyd3-Aug-07 12:08
pag_floyd3-Aug-07 12:08 
GeneralRe: Another .Net Digg API + Your API's mention Pin
thund3rstruck3-Aug-07 16:39
thund3rstruck3-Aug-07 16:39 
GeneralThank you Pin
merlin98125-Jul-07 4:44
professionalmerlin98125-Jul-07 4:44 
QuestionHow to get Dugg count Pin
Michael Sync24-Jul-07 16:23
Michael Sync24-Jul-07 16:23 
AnswerRe: How to get Dugg count Pin
thund3rstruck25-Jul-07 2:24
thund3rstruck25-Jul-07 2:24 
GeneralRe: How to get Dugg count Pin
Michael Sync2-Aug-07 15:27
Michael Sync2-Aug-07 15:27 
GeneralSource code missing Pin
Tony Bermudez24-Jul-07 15:47
Tony Bermudez24-Jul-07 15:47 
GeneralRe: Source code missing Pin
thund3rstruck25-Jul-07 2:18
thund3rstruck25-Jul-07 2:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.