O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Yahoo! Hacks
By Paul Bausch
October 2005
More Info

HACK
#96
Syndicate Rich Media
Media RSS can tell Yahoo! and the world about your audio and video
The Code
[Discuss (0) | Link to this hack]

The Code

This code is an ASP file written with VBScript that can be used on Windows servers. Save the following code to a file called mRSS.asp and be sure to alter the three values at the top to match your setup:

	<%
	'-----------------------------------------------------------------
	' mRSS.asp
	'
	' Finds video files in a directory and generates a Media RSS file
	' describing those video files.
	'-----------------------------------------------------------------
	' Change these values to fit your setup.
	'
	' BASE_URL is the publicly available address for the directory
	' MEDIA_ADULT indicates whether or not the files are for adults only
	' MEDIA_EXTS is a list of recognized media extensions that you want
	'			 to include in the Media RSS file
	'
	Const BASE_URL = "http://insert/path/to/videos/"
	Const MEDIA_ADULT = "false"
	Const MEDIA_EXTS = "avi,mov,mp3,mpg,mpeg,wmv"

	' Set content type for the page
	Response.ContentType = "text/xml"

	%><?xml version="1.0"?>
	<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss">
	<channel>
	<title>My Site Videos</title>
	<link>http://www.example.com/</link>
	<description>Various videos I've produced</description>
	<%
	' Start the FileSystem Object
	Set	fs = Server.CreateObject("Scripting.FileSystemObject")

	' Find the current directory
	thisPath = Request.ServerVariables("path_info")
	lastS = InStrRev(thisPath,"/")
	thisPath = Mid(thisPath,1,lastS)
	thisFullPath = Server.MapPath(thisPath)

	' Load the current directory
	Set fsFolder = fs.GetFolder(thisFullPath)
	Set fsFiles = fsFolder.Files

		' Loop through each file in the directory and
		' check the extension. If it's a recognized
		' type, build the RSS item
		For Each Item in fsFiles
			strName = Item.Name

			intDot = InStrRev(strName,".") + 1
			strShortName = Left(strName,intDot - 2)
			strExtension = LCase(Mid(strName,intDot,Len(strName)))
			If InStr(MEDIA_EXTS,strExtension) Then
				strSize = Item.Size
				dtmDate = Item.DateCreated
				dtmMod = Item.DateLastModified
				strType = Item.Type
				response.write "<item>" & Chr(13)
				response.write " <title>" & strShortName & "</title>" & Chr(13)
				response.write " <link>" & BASE_URL & strName & "</link>" & 
Chr(13)
				response.write " <media:content url=""" & BASE_URL & strName 
& """"
				response.write " fileSize=""" & strSize & """"
				response.write " type=""" & getType(strExtension) & """ />" 
& Chr(13)
				response.write " <media:adult>" & MEDIA_ADULT & "</media: 
adult>"
				response.write Chr(13) & "</item>" & Chr(13)
			End If
		Next

	' Clean up the objects
	Set fsFiles = Nothing
	Set fsFolder = Nothing
	Set fs = Nothing
	%>
	</channel>
	</rss>
	<%
	' This function translates a file extension
	' into its content type
	Function getType(ext)
		Select Case ext
			Case "avi"
				getType = "video/avi"
			Case "mov"
				getType = "video/quicktime"
			Case "mp3"
				getType = "audio/mpeg"
			Case "mpeg","mpg"
				getType = "video/mpeg"
			Case "wmv"
				getType = "video/x-ms-wmv"
		End Select
	End Function
	%>

This code loops through the files in a directory and checks the file extension against the list of extensions set as MEDIA_EXTS. If the extension is one of the set types, the script creates an RSS item using the filename as the <title>, and the value set in BASE_URL plus the filename as the <link>.

The script then adds a minimal <media:content>tag to describe the file and the <media:adult>tag to indicate whether the file has adult content. This value is hardcoded into the script, so you'll need to keep your adult files separate from your kid-friendly files. You can change how the script treats the file by setting the MEDIA_ADULT variable to true or false at the top of the script.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.