Search

Hello All,

I am having some trouble and thought I would ask the experts. What I'me doing is using my Last FM rss feed to display the artist and title of the song... easy and done. I'm then passing them to the Last FM API to return info about the album; image,name, id etc.

Everything works fine for artists/song containing no spaces or odd characters as shown at the link below with the first two artists but anything else dies:

http://blog.devkinetic.com/music/

here is my XSLT:

 <xsl:template name="last-fm">

<xsl:variable name="last" select="document('http://ws.audioscrobbler.com/1.0/user/devkinetic/recenttracks.rss')" />
    <h2>Recently Played Tracks - <a href="{$last/rss/channel/link}" alt="My Last FM Profile">Profile</a></h2>

    <ul class="menu">
        <xsl:for-each select="$last/rss/channel/item[position() &lt; 5]">
            <xsl:variable name="one"><xsl:value-of select="substring-before(title, ' –')"/></xsl:variable>
            <xsl:variable name="two"><xsl:value-of select="substring-after(title, '– ')"/></xsl:variable>
            <xsl:variable name="info" select="document(concat('http://ws.audioscrobbler.com/2.0/?method=track.getinfo&amp;api_key=dee3f1878b81d9157e6d365e4307acd2&amp;artist=', $one, '&amp;track=', $two))"/>
            <li>
                Artist: <xsl:value-of select="$one"/><br />
                SONG: <xsl:value-of select="$two"/><br />
                Album: <xsl:value-of select="$info/lfm/track/album/mbid" />
            </li>
        </xsl:for-each>
    </ul>

 </xsl:template>

I realize if the external feeds go down the page will break, once this is working I will transfer it over to a locally cached feed.

Can you write out $one and look at its value? Last.FM URLs require the artist name to be URL encoded (e.g. Michael Jackson becomes Michael+Jackson). Perhaps you could try replacing spaces with +?

 translate($one, ' ', '+')

EDITED: I had a question then answered it myself! You got me on the right track though with the url encoding.

I should note this is on 1.7

I created a new component called url-encode based on this script and edited my code as follows:

 <xsl:template name="last-fm">

<xsl:variable name="last" select="document('http://ws.audioscrobbler.com/1.0/user/devkinetic/recenttracks.rss')" />
    <h2>Recently Played Tracks - <a href="{$last/rss/channel/link}" alt="My Last FM Profile">Profile</a></h2>

    <ul class="menu">
        <xsl:for-each select="$last/rss/channel/item[position() &lt; 5]">
            <xsl:variable name="artist"><xsl:value-of select="substring-before(title, ' –')"/></xsl:variable>
            <xsl:variable name="track"><xsl:value-of select="substring-after(title, '– ')"/></xsl:variable>
            <xsl:variable name="api-key" select="'*******my api key*********'" />
            <xsl:variable name="artist-encoded">
                <xsl:call-template name="url-encode">
                    <xsl:with-param name="str" select="$artist"/>
                </xsl:call-template>                
            </xsl:variable>
            <xsl:variable name="track-encoded">
                <xsl:call-template name="url-encode">
                    <xsl:with-param name="str" select="$track"/>
                </xsl:call-template>                
            </xsl:variable>
            <xsl:variable name="info" select="document(concat('http://ws.audioscrobbler.com/2.0/?method=track.getinfo&amp;api_key=', $api-key, '&amp;artist=', $artist-encoded, '&amp;track=', $track-encoded))"/>

            <li>
                Artist: <xsl:value-of select="$artist"/><br />
                Song: <xsl:value-of select="$track"/><br />
                Album: <xsl:value-of select="$info/lfm/track/album/mbid" /><br />
                Encoded Artist: <xsl:value-of select="$artist-encoded"/><br />
                Encoded Song: <xsl:value-of select="$track-encoded"/><br />
            </li>
        </xsl:for-each>
    </ul>

 </xsl:template>

Is there a better way to do this? I'm still new and am sure my code though working is hackish at best

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details