Showing posts with label digg api. Show all posts
Showing posts with label digg api. Show all posts

Monday, 15 October 2007

Digg API on AIR

Digg API on AIR

Larger screenshot on Flickr.

Hey, everyone. I do have a programming related post today.

I have been coding in JavaScript, XML/HTML and CSS to create the above simple Digg API AIR application, which retrieves the top (e.g. newest) story for each category on Digg.

I have as good as finished it and will be releasing the AIR installation package (*.air) and source code (*.zip) for anyone who is interested in the AIR platform to look at.

Although I could have done an XmlHttp request without any additional framework, I did use the Prototype JavaScript framework for this project.


Here is a quick rundown of the files used in my application and their purpose:


DiggAPI.xml - This is the file which specifies the name, copyright information and file to use for content, etc. An XML file such as this is a standard requirement in any AIR application.


DiggAPI.html - The application content. This contains all of the HTML code for my application and calls to the neccessary JavaScript functions in my external *.js files.


sampleCert.pfx - The certificate used to sign the AIR application. All AIR applications must be signed to be built, even if not by a recognised body such as VeriSign.


script/AIRAliases.js - JavaScript file containing code used by the AIR runtime. Referencing this file provides convenient aliases for use in your code. Included with AIR SDK.


script/prototype.js - JavaScript file containing code for the Prototype framework. I used the cross-browser new Ajax.request provided by Prototype in my application.


script/DiggAPI.js - JavaScript file containing my code used by the application.


style/DiggAPI.css - Style sheet used to format my links and text colours, etc in my application.


style/digg.png - Digg guy logo image used in my application.


Check back soon for included AIR package and source code. Happy coding. :)



Digg This Story

Tuesday, 2 October 2007

Digg API and Greasemonkey


I wrote this straightforward Greasemonkey user script in JavaScript using Greasemonkey's implementation of an XmlHttpRequest:

// Digg API script
// version 1.0
// Copyright (c) 2007 Samuel Saint-Pettersen
// Released under the GPL licence
// http://www.gnu.org/copyleft/gpl.html
// ---------------------------------------------------------------------
// This is a Greasemonkey user script.
// To install it, you need Greasemonkey:
// http://greasemonkey.mozdev.org
// ---------------------------------------------------------------------
// ==UserScript==
// @name Digg API script
// @version 1.0
// @author Samuel Saint-Pettersen
// @description Utilise the Digg API
// @include *
// ==/UserScript==
(function() {
GM_xmlhttpRequest({
method: 'GET',
url: "http://services.digg.com/stories/topic/gadgets?count=1&
appkey=http://apidoc.digg.com&type=xml",

onload: function(responseDetails) {
var response = responseDetails.status;
var returned = responseDetails.responseText;
if(response != "200")
{
// if the request fails, display error message
alert("Error retrieving Digg Data");
}
else
{
// display Digg data in message box
alert(returned);
}
}});
})();

The Greasemonkey script request returned fine.
I wonder what it is with my .NET application which meant it didn't work?

Digg This Story

Monday, 1 October 2007

Digg API and .NET


I have being trying to develop a simple console application, which utilises Digg API in the Boo programming language.

This is the source code I have for my application:

/*
Simple application to access the Digg API
Version: 1.0
Language: Boo
Environment: SharpDevelop
Author: Samuel Saint-Pettersen
---------------------------------------------------------------------
Prerequisites:
* Boo libraries (included with)
* Microsoft .NET 2.0 Framework
---------------------------------------------------------------------
*/
namespace DiggAPI

import System
import System.Collections
import System.IO
import System.Net
import System.Text
import System.Xml
import System.Web

print "Digg API application\n"

// base request url
burl = "http://services.digg.com/stories/topic/gadgets?count=3&appkey="

// encode appkey
appkey = HttpUtility.UrlEncode("http://apidoc.digg.com")

// full request url
address = burl + appkey + "&type=xml"

// create request
request = cast(HttpWebRequest, WebRequest.Create(address))

// method
request.Method = "GET"

// get reponse
response = cast(HttpWebResponse, request.GetResponse())

// get response stream into reader
reader = StreamReader(response.GetResponseStream(), Encoding.UTF8)

// print response
print reader.ReadToEnd()
print "\n"

// clean up
reader.Close()

// wait for key press
print "Press any key to continue...\n"
Console.ReadKey(true)


For some reason, when I run this application I always get a Forbidden (403) error returned.

Weird? I'm sure I've got the the request URL correct. I get this error whether or not I encoded the appkey.

I'm sure this is some sort of odd behaviour on .NET's part, so I decided to try using the Digg API with something else.

If you know what the the problem is with the .NET application, please leave me a comment or e-mail


EDIT 10/03/2007:

Timothy Parez helped me out. The solution was to specify the
UserAgent. So I used:


request.UserAgent = appkey;


I used my encoded appkey for the the user agent string. However, in
the Digg API's current state it does not seem to matter whether or not
the appkey is encoded. But I might as well encode it in case they
change this.

Thanks, Timothy.

Digg This Story