Monday, July 14, 2008

Media Feeds for Piclens Plugin using .NET 2.0

About this post
This post is about the "MediaRSS" standard and how you can use it for your own website. If you have never heard of it - never mind. But maybe you have heard of a really cool Firefox Plugin called "PicLens".

"PicLens"?
"PicLens" is a incredible surface for some internet services, like YouTube, Google picture search, Flickr, Amazon, Deviant Art. Now you can have your favorite sites in Full-screen. 3D.

Media RSS
The Piclens guys have implemented the "MediaRSS" standard - that means: Each site with an MediaRSS can be viewed in Piclens.
If you are a webmaster, you should take a look at this site.

Bellow is the Media Feeds Example for ASP.NET 2.0 C# you can create a file with name
MediaRss.ashx on the root of your site and following line of code in the Head section of your pages.
Put a Link tag with href="MediaRss.ashx" type="application/rss+xml" id="gallery" and that's all

Bellow is the code of the MediaRss.ashx


<%@ WebHandler Language="C#" Class="MediaRss" %>
using System;
using System.Xml;
using System.IO;
using System.Web;

[System.Web.Services.WebService(Namespace = "http://tempuri.org/")]
[System.Web.Services.WebServiceBinding(ConformsTo = System.Web.Services.WsiProfiles.BasicProfile1_1)]
public class MediaRss : IHttpHandler {

string media = "http://search.yahoo.com/mrss";
string atom = "http://www.w3.org/2005/Atom";

public void ProcessRequest (HttpContext context)
{
XmlDocument xmlDocument = new XmlDocument();
XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", "yes");
xmlDocument.AppendChild(xmlDeclaration);

XmlElement rssElement = xmlDocument.CreateElement("rss");
rssElement.SetAttribute("version", "2.0");
rssElement.SetAttribute("xmlns:media",media);
rssElement.SetAttribute("xmlns:atom", atom);
xmlDocument.AppendChild(rssElement);

XmlElement channelElement = xmlDocument.CreateElement("channel");
rssElement.AppendChild(channelElement);

GenerateItems(channelElement, xmlDocument, context);

context.Response.ContentType = "text/xml";
xmlDocument.Save(context.Response.Output);
context.Response.End();
}

public void GenerateItems(XmlElement channelElement,XmlDocument xmlDocument,HttpContext context)
{
string[] imageFiles = Directory.GetFiles(context.Server.MapPath("~/Images/"));

foreach(string imageFile in imageFiles)
{
FileInfo fileInfo = new FileInfo(imageFile);

XmlElement itemElement = xmlDocument.CreateElement("item");

XmlElement titleElement = xmlDocument.CreateElement("title");
titleElement.InnerText = fileInfo.Name;

XmlElement linkElement = xmlDocument.CreateElement("link");
string link = "http://localhost/myapp/Images/" + fileInfo.Name;
linkElement.InnerText = link;

XmlElement thumbnailElement = xmlDocument.CreateElement("media","thumbnail",media);
thumbnailElement.SetAttribute("url", link);
XmlElement mediaElement = xmlDocument.CreateElement("media","content",media);
mediaElement.SetAttribute("url", link);
itemElement.AppendChild(titleElement);
itemElement.AppendChild(linkElement);
itemElement.AppendChild(thumbnailElement);
itemElement.AppendChild(mediaElement);

channelElement.AppendChild(itemElement);
}
}

public static string Utf8BytesToString(byte[] InBytes)
{
System.Text.UTF8Encoding utf8encoder = new System.Text.UTF8Encoding(false, true);
return utf8encoder.GetString(InBytes, 0, InBytes.Length);
}

public bool IsReusable {
get {
return false;
}
}
}


3 comments:

Anonymous said...

good help man! keep it up.. have a nice day..

Unknown said...

so this creates a dynamic media rss file for each webpage that I put that string in the head? Then how do I reference that rss file, what will it be called?

Mazhar said...

For this you need to put a link tag to this feed in your website header.
For example Put link tag in head section then point its href property to ashx file and finally set its type to application/rss+xml and that's it