Last.fm Web Services » Discussions

Yet another 'Recent Tracks'-script in PHP

 
  • Yet another 'Recent Tracks'-script in PHP

    After my old one stopped working (yeah, thanks for the notice) I had to write a new one. This script caches data, removes all dat number junk (tm) and lets you specify how many tracks to post. Even though I suppose most people in here will write their own I'm hoping it might be useful to someone.

    http://www.astoever.no/code/lastfm.recent.phps
    http://www.astoever.no/code/lastfm.topartists.phps
    More free code for you

    INSTALLATION

    1) Copy the code into a blank document
    2) Rename it to something.php
    3) Upload the file(s) to your server
    4) The cache files should be created if the script has proper permissions
    NB: Some servers will not allow this, then do this:

    1) Make an empty file (a .txt document will do)
    2) Rename it to 'lastfm.recent.cache' (or whatever you called it)
    3) Upload it into the same directory you placed the script/call it from
    4) Do a CHMOD 777 'cachefile' (your ftp proggy prolly has this on a right-click menu)
    5) Cross your fingers!

    CONTRIBUTIONS

    Timestamping latest tracks

    $timestamp = date('j. n. Y, H:i:s',$trackArray[0]);
    echo "<li>".$timestamp." <B>".$trackArray[1]."</B></li>";

    Thanks to simulacra and Martin.

    Top artist tag-cloud

    amoebaz has made a cool script based on this one that actually generates a top artist tag-cloud (!) of your favorite artists. It's groovy as hell. Check it out now.

    file_get_contents disabled? Fix!

    <?php
    $ch = curl_init();
    $timeout = 5; // set to zero for no timeout
    curl_setopt ($ch, CURLOPT_URL, 'http://ws.audioscrobbler.com/1.0/user/'.$lastfmUsername.'/recenttracks.txt');
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $recentlyPlayedSongs = curl_exec($ch);
    curl_close($ch);
    ?>

    Thanks to cvax.

    Edited by astoever on 28 May 2006, 16:29
    • mokele said...
    • Alumni
    • 23 Sep 2005, 08:24

    Re: Yet another 'Recent Tracks'-script in PHP

    Quoth astoever:
    After my old one stopped working (yeah, thanks for the notice) I had to write a new one. This script caches data, removes all dat number junk (tm) and lets you specify how many tracks to post. Even though I suppose most people in here will write their own I'm hoping it might be useful to someone.

    http://www.astoever.no/misc/code/lastfm.phps


    I take it you meant the free one I had running.

    There was a whole weeks notice thanks very much. There was no way to inform everyone using it (thousands) in one go apart from making the first line of the image say something along the lines of "this will stop working on the 21st" or something like that. It then got cut off due to bandwidth usage on the 20th. The host was a free host, and I tried my best to inform everyone.

  • Not at all. I've never used any service provided by you.

    • mokele said...
    • Alumni
    • 1 Oct 2005, 11:23

    Re:

    Quoth astoever:
    Not at all. I've never used any service provided by you.



    ah ok ;)

    • moe2i6 said...
    • User
    • 3 Oct 2005, 01:41
    is there anyway I can see an example and how do I get that to work... Im a newb sorry

  • @mokele: AFAIK they redesigned how data was published on ws.audioscrobbler (surely due to the new API's) and that's why most scripts stopped working.

    @moe2i6: The script is intended to use in conjunction with a webpage. You need a site that supports PHP and then include the script where you want the Last.FM info to be displayed. Like with PHP you'd write <? include 'scriptname'; ?> in the page you'd want it running. You can see it in action on http://www.astoever.no

    Edit: Included a link.

    Edited by astoever on 3 Oct 2005, 02:27
    • gonace said...
    • User
    • 31 Oct 2005, 10:45

    Hello

    Why do I get "Maybe I'm not listening to anything, maybe the server is down. Either way, the local cache is empty." when chase is really not empty?=)

  • Re: Yet another 'Recent Tracks'-script in PHP

    i'm getting the "cache is empty" error.

    i'm sure it's something trivial... i'm probably not understanding the cache functionality... actually i don't know why it's needed at all.

    http://www.alternapop.com

    • mokele said...
    • Alumni
    • 22 Nov 2005, 23:25

    Re: Re: Yet another 'Recent Tracks'-script in PHP

    Quoth alternapop:
    i'm getting the "cache is empty" error.

    i'm sure it's something trivial... i'm probably not understanding the cache functionality... actually i don't know why it's needed at all.

    http://www.alternapop.com


    I assume the cache functionality is to reduce the amount of unecessary hits on audioscrobbler feed where the data probably wouldn't have changed since the last request.

  • that makes sense... but i haven't a clue why it isn't working. i just copied the php script to my webpage and added my user info.

    what else do i need to do?

  • i figured it out...

    apparently an empty file named, "lastfm.recent.cache" needs to be in the same directory as the file that has the php script. this is mentioned nowhere on the site that provides the script. :p
    or the file that contains the script should create it upon its first run but i don't think that's how it works.

    also, the link in the php script is wrong. don't just change the user name. the rest of the link is out of date too.

  • The cache would be created automagically provided that the script has rights to do so:

    if(!file_exists($lastfmCache)) touch($lastfmCache);

    I probably should have mentioned that some servers will not allow this, and then you'd have to make it yourself. Happy you figured it out though.

    Updated the links to reflect teh brand new two different versions.

    If anyone else gets the "cache is empty" massage(?), do this:

    1) make an empty file (a .txt document will do)
    2) rename it to 'lastfm.recent.cache' (or whatever you called it)
    3) upload it into the same directory you placed the script/call it from
    4) do a CHMOD 777 'cachefile' (your ftp proggy prolly has this on a right-click menu)
    5) cross your fingers

    And there you have it.

    • chasba said...
    • User
    • 22 Dec 2005, 23:49

    Still having a permission problem with this script.

    Should this script work with PHP 5? I'm using PHP 5.1.1 and I'm only seeing "Cache is empty." even with lastfm.recent.cache chmodded 777 (-rwxrwxrwx).

    If wanted, I can provide a URL with the errors I'm getting. Thanks for a sweet script tho, looks the business.

  • I will help you through the power of Last.FM messaging.

  • wow. i've made it work. unbelievable. :o)

    (i'm a total newbie to php.)

    astoever, is there a way how to add the timestamps there, too? (are they in the txt file? i mean those strange numbers give me some hope)

    and, thanks for writing it. :)

    cheers
    J.


    edit: okay, it is possible. e.g. like this:


    $timestamp = date('j. n. Y, H:i:s',$trackArray[0]);
    echo "<li>".$timestamp." <B>".$trackArray[1]."</B></li>";


    which is giving a list looking like this:


    • 7. 1. 2006, 19:46:08 The Mary Janes - Simple times

    • 7. 1. 2006, 19:42:50 Mic Christopher - Kids' Song

    • 7. 1. 2006, 19:28:50 The Mary Janes - Nearly Dead

    • 7. 1. 2006, 19:24:44 Mic Christopher - I've Got Your Back

    • 7. 1. 2006, 19:19:27 The Mary Janes - Friends

    • 7. 1. 2006, 19:15:38 Mic Christopher - Heyday

    • 7. 1. 2006, 19:11:49 The Mary Janes - H.U.M.F.R.E.A.

    • 7. 1. 2006, 19:08:03 Mic Christopher - Looking For Jude

    • 7. 1. 2006, 19:04:15 The Mary Janes - Short A Few

    • 7. 1. 2006, 19:00:21 Mic Christopher - Listen Girl



    thanks to Martin for help :)

    • cvax said...
    • User
    • 11 Jan 2006, 10:05
    i made the lastfm.recent.cache file but im still getting the "Cache is empty" deal. can you help?

  • @simulacra: That's cool :) I'll throw it into the script with credit to you and Martin (of course).

    @cvax: Have you checked permissions? Some FTP clients have a 'properties' pane (instead of CHMOD) where you can set permissions. Make sure it's set to 0777. If you still can't get it to work, send me a message.

    • cvax said...
    • User
    • 12 Jan 2006, 02:13
    for people having problems with file_get_contents() being disabled by your servers here is an alternative that uses the cURL library

    Alternative for file_get_contents()

    Use this:

    <?php
    $ch = curl_init();
    $timeout = 5; // set to zero for no timeout
    curl_setopt ($ch, CURLOPT_URL, 'http://ws.audioscrobbler.com/1.0/user/yourusernamehere/recenttracks.txt');
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $recentlyPlayedSongs = curl_exec($ch);
    curl_close($ch);

    ?>

    note: in the url place i had to use the direct url instead of the username variable because for some reason it wasnt parsing the variable

  • :)

    Just do a:

    curl_setopt ($ch, CURLOPT_URL, 'http://ws.audioscrobbler.com/1.0/user/'.$lastfmUsername.'/recenttracks.txt');

    • Lolitka said...
    • User
    • 24 Jan 2006, 14:49
    Can you make it as xml parser? (:
    I looked at PHP XML parser and it was like WTF, so if you could learn it and make it and i will understand it from your script ... (:

    [(((o)))] Foo Bar [(((o)))] Petite (:
  • Haha, quite the bold request. Luckily for you I have to start mucking about with XML soon. But soon != now.

    • mbmn said...
    • User
    • 4 Feb 2006, 11:16
    I was just thinking about writing a XML-parser. I'm no PHP-guru, though I wrote a parser for Delineateit in both C# and PHP, so it shouldn't be that much harder to write one for last.fm :)

  • Cleaned up the thread with all the little fixes and hacks and whatnot. Cool to see so many people use these scripts.

    • gehx said...
    • User
    • 22 Mar 2006, 20:57

    blank output

    I don't get any output from this script. lastfm.recent.cache isn't created. No error! What's the problem do you suppose??

    I like music and computers and food and video games and Xbox Media Center.

    Thx for playing.....
  • Try:

    1) Make an empty file (a .txt document will do)
    2) Rename it to 'lastfm.recent.cache' (or whatever you called it)
    3) Upload it into the same directory you placed the script/call it from
    4) Do a CHMOD 777 'cachefile' (your ftp proggy prolly has this on a right-click menu)

    I haven't bothered with errors :P

Anonymous users may not post messages. Please log in or create an account to post in the forums.