Tom Muck

Alpha Dog Blues Band
Home page
All articles
All Extensions | Extension News | Extension FAQs | Customer Login
Books authored or co-authored by Tom Muck
Extensions, books, and other products | Customer Login
Your current cart contents Blog | CMXTraneous Blog | Flash Remoting Blog
About the site


Tom Muck's BlogTom Muck's Blog

News and Views

Now Playing

Saturday, February 04, 2006 2:59:22 PM

I noticed Dan Short had a "Now Playing" module on his blog, so I decided I better get one too. The Now Playing plugin for iTunes, written by Brandon Fuller, is available for $10 and automatically creates an XML file on your computer (or FTP server) every time the song changes as you are listening to iTunes. I threw together some CF code to parse the XML file and create the pod in my left column. The getItunesXMLFromFTP function allows you to grab the file from an FTP server. Using the FTP option, I can work anywhere and keep the file updated. As with all the code I post here, it's basic and serves my needs, but feel free to modify it for your own use.

<!--- Set up some parameters file/folder info --->
<cfset xmlFileName = 'itunes.xml'>
<cfset localFolder = 'd:/yourfolder/'>
<cfset getFromFTP = true/>
<!--- If FTP is needed, supply the following --->
<cfset ftpserver = 'yourftplocation'>
<cfset ftpusername = 'yourftpusername'>
<cfset ftppassword = 'yourftppassword'>

<!--- Create XML from xml file --->
<cffunction name="parseItunesXml">
 <cfargument name="location" type="string">
 <cffile action="read" variable="itunes" file="#location#">
 <cfif isdefined("itunes")>
  <cfset itunesxml = XMLParse(itunes)>
 <cfreturn itunesxml />

<!--- Grabs file from an FTP site if needed --->
<cffunction name="getItunesXMLFromFTP">
 <cfargument name="location" type="string">
 <cfargument name="username" type="string">
 <cfargument name="password" type="string">
 <cfargument name="filename" type="string">
 <cfargument name="filenewlocation" type="string">
 <cfftp action = "open"
  username = "#username#"
  connection = "itunesconnection"
  password = "#password#"
  server = "#location#"
  stopOnError = "Yes">
 <cfif cfftp.succeeded>
  <cfftp action="getfile"
   passive="yes" failifexists="no">
 <cfftp action = "close"
  connection = "itunesconnection"
  stopOnError = "Yes">

<!--- Get the file --->
<cfif getFromFTP EQ true>
 <cfset getItunesXMLFromFTP('#ftpserver#', '#ftpusername#', '#ftppassword#', '#xmlFileName#', '#localFolder##xmlFileName#')>
<!--- Get the file on the local file system and make XML object --->
<cfset itunesXML = parseItunesXML("#localFolder##xmlFileName#")>
<cfif itunesXML.now_playing.XmlAttributes.playing GT 0>
 <cfset numberSongs = ArrayLen(itunesXML.now_playing.XmlChildren)>
 <div id="itunes" class="modules">
  <h3>Now Playing</h3>
  <!--- Loop through all songs in xml file --->
  <cfloop index="i" from = "1" to = #numberSongs#>
   <cfset itunesurl = "">
   <cfset currentSong =[i]>
   <cfif isdefined("currentSong.urlamazon") AND
currentSong.urlamazon.xmltext NEQ "">
    <cfset itunesurl = currentSong.urlamazon.xmltext>
   <cfelseif IsDefined("currentSong.urlapple") AND
currentSong.urlapple.xmltext NEQ "">
    <cfset itunesurl = currentSong.urlapple.xmltext>
   <cfif IsDefined("currentSong.title") AND
currentSong.title.xmltext NEQ "">
    <cfif itunesurl NEQ "">
     <a href="#itunesurl#">#currentSong.title.xmltext#</a>
   <cfif IsDefined("currentSong.artist") AND
currentSong.artist.xmltext NEQ "">
    by #currentSong.artist.xmltext#<br/>
   <cfif IsDefined("currentSong.album") AND
currentSong.album.xmltext NEQ "">
    from #currentSong.album.xmltext#<br/>
   <cfif IsDefined("currentSong.year") AND
currentSong.year.xmltext NEQ "">
   <cfif IsDefined("currentSong.image") AND currentSong.image.xmltext NEQ "">
    <div style="text-align:center">
    <cfif itunesurl NEQ "">
     <a href="#itunesurl#"><img src="#currentSong.image.xmltext#" alt="#currentSong.title.xmltext#"/></a>
     <img src="#currentSong.image.xmltext#" alt="#currentSong.title.xmltext#"/>
 <div style="text-align:center"><a href="">
 <img src="" alt="Now Playing" /></a></div>

Update 2/23/06: Steven Erat built a Now Playing pod and posted the CF code on his blog also:

Category tags: Music, Dreamweaver, ColdFusion

Before posting comments or trackbacks, please read the posting policy.

Full Blog Calendar

Pay me securely with your Visa, MasterCard, Discover, or American Express card through PayPal!
Pay me securely with your Visa, MasterCard, Discover, or American Express card through PayPal!
About | Privacy Policy | Contact | License Agreement | ©2002-2024 Tom Muck | Dreamweaver Extensions