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
Tom-Muck.com Blog | CMXTraneous Blog | Flash Remoting Blog
About the site

Blog

Tom Muck's Blog: Now PlayingTom 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.

<cfsilent>
<!--- 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 />
 </cfif>
</cffunction>

<!--- 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"
   stoponerror="yes"
   remotefile="#filename#"
   localfile="#filenewlocation#"
   connection="itunesconnection"
   username="#username#"
   password="#password#"
   passive="yes" failifexists="no">
 </cfif>
 <cfftp action = "close"
  connection = "itunesconnection"
  stopOnError = "Yes">
</cffunction>

<!--- Get the file --->
<cfif getFromFTP EQ true>
 <cfset getItunesXMLFromFTP('#ftpserver#', '#ftpusername#', '#ftppassword#', '#xmlFileName#', '#localFolder##xmlFileName#')>
</cfif>
<!--- Get the file on the local file system and make XML object --->
<cfset itunesXML = parseItunesXML("#localFolder##xmlFileName#")>
</cfsilent>
<cfif itunesXML.now_playing.XmlAttributes.playing GT 0>
 <cfset numberSongs = ArrayLen(itunesXML.now_playing.XmlChildren)>
 <div id="itunes" class="modules">
  <h3>Now Playing</h3>
  <cfoutput>
  <!--- Loop through all songs in xml file --->
  <cfloop index="i" from = "1" to = #numberSongs#>
   <cfset itunesurl = "">
   <cfset currentSong = itunesXML.now_playing.song[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>
   <p>
   <cfif IsDefined("currentSong.title") AND
currentSong.title.xmltext NEQ "">
    <cfif itunesurl NEQ "">
     <a href="#itunesurl#">#currentSong.title.xmltext#</a>
    <cfelse>
     #currentSong.title.xmltext#
    </cfif>
    <br/>
   </cfif>
   <cfif IsDefined("currentSong.artist") AND
currentSong.artist.xmltext NEQ "">
    by #currentSong.artist.xmltext#<br/>
   </cfif>
   <cfif IsDefined("currentSong.album") AND
currentSong.album.xmltext NEQ "">
    from #currentSong.album.xmltext#<br/>
   </cfif>
   <cfif IsDefined("currentSong.year") AND
currentSong.year.xmltext NEQ "">
    (#currentSong.year.xmltext#)
   </cfif>
   </p>
   <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>
    <cfelse>
     <img src="#currentSong.image.xmltext#" alt="#currentSong.title.xmltext#"/>
    </cfif>
   </div>
   <hr/>
  </cfif>
 </cfloop>
 </cfoutput>
 <div style="text-align:center"><a href="http://brandon.fuller.name/archives/hacks/nowplaying/">
 <img src="http://brandon.fuller.name/images/now_playing.png" alt="Now Playing" /></a></div>
 </div>
</cfif>

Update 2/23/06: Steven Erat built a Now Playing pod and posted the CF code on his blog also: http://www.talkingtree.com/blog/index.cfm/2006/2/22/Now-Playing-on-iTunes-Pod.

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-2019 Tom Muck | Dreamweaver Extensions