Last.fm Web Services » Discussions

Yet another 'Recent Tracks'-script in PHP

 
    • dukny said...
    • User
    • 24 Apr 2007, 04:33
    marc2003 said:
    i have no idea what script you are using but this little snippet fetches the last 10 tracks and displays the same results as you want. it also has caching support. :)

    <?php
    $user = 'marc2003';
    $cache = 120; //in seconds, how long to cache data locally
    $remotefile = "http://ws.audioscrobbler.com/1.0/user/$user/recenttracks.xml";;
    $localfile = $user.'.xml';
    if ((!file_exists($localfile)) || (time()-filemtime($localfile)>$cache)) {
    $contents = @file_get_contents($remotefile);
    $fp = fopen($localfile, "w");
    fwrite($fp, $contents);
    fclose($fp);
    }
    $xml = @simplexml_load_file($localfile);
    for ($t = 0; $t <= 9; $t++) {
    echo "<li>".$xml->track[$t]->artist." - ".$xml->track[$t]->name."</li>";
    }
    ?>





    thanks mark2003
    but, cache is empty! perhaps, file_get_contents disabled
    any idea??

  • dukny said:
    thanks mark2003
    but, cache is empty! perhaps, file_get_contents disabled
    any idea??


    well your "recenttracks.xml" file on lastfm is empty. nothing the script can do about that!! just scrobble some more tracks and fill it up again. or maybe lastfm is a little behind and it's temporarily not showing your recent suubmissions?

    the code i posted certainly works on my username at this minute in time....



    i'm also using the same, albeit slightly more advanced code for my signature.

    • dukny said...
    • User
    • 24 Apr 2007, 06:58
    marc2003 said:
    dukny said:
    thanks mark2003
    but, cache is empty! perhaps, file_get_contents disabled
    any idea??


    well your "recenttracks.xml" file on lastfm is empty. nothing the script can do about that!! just scrobble some more tracks and fill it up again. or maybe lastfm is a little behind and it's temporarily not showing your recent suubmissions?

    the code i posted certainly works on my username at this minute in time....
    i'm also using the same, albeit slightly more advanced code for my signature.




    hmm...
    yes, my "recnttracks.xml" is empty. i use to roblogic (another user name)
    what problem? my server is php4.4.2


  • oops, you need php5 to use the simplexml function.

    to parse xml using php4, take a look at the wiki. there are quite a few examples there to get you going. :)

    http://www.audioscrobbler.net/wiki/

    • jye said...
    • User
    • 28 Apr 2007, 15:56
    My version is PHP Version 5.2.1 and I have :

    SimpleXML
    Revision : $Revision: 1.151.2.22.2.20 $
    Schema support : enabled

    But I still have a cache file empty.. where's the matter ?

    Keep your ears clear, go to Visual-Music.org
  • jye said:
    But I still have a cache file empty.. where's the matter ?


    saves me typing it again. copy/pasted from above... :p

    marc2003 said:

    well your "recenttracks.xml" file on lastfm is empty. nothing the script can do about that!! just scrobble some more tracks and fill it up again. or maybe lastfm is a little behind and it's temporarily not showing your recent suubmissions?


    try another username and you can see it working. :)

    edit: you've barely scrobbled any tracks this week.

    Edited by marc2003 on 29 Apr 2007, 10:01
    • jye said...
    • User
    • 29 Apr 2007, 00:54
    Well i'm triyin with my friend xcuntx which have some files but it changes anything.... :/ look here : http://www.visual-music.org/infos.php and http://www.visual-music.org/scripts/get_lastfm.php which contains this : <?php
    $user = 'xcuntx';
    $cache = 120; //in seconds, how long to cache data locally
    $remotefile = "http://ws.audioscrobbler.com/1.0/user/$user/recenttracks.xml";;
    $localfile = $user.'.xml';
    if ((!file_exists($localfile)) || (time()-filemtime($localfile)>$cache)) {
    $contents = @file_get_contents($remotefile);
    $fp = fopen($localfile, "w");
    fwrite($fp, $contents);
    fclose($fp);
    }
    $xml = @simplexml_load_file($localfile);
    for ($t = 0; $t <= 9; $t++) {
    echo "<li>".$xml->track[$t]->artist." - ".$xml->track[$t]->name."</li>";
    }
    ?>


    I don't understand where is the matter

    Keep your ears clear, go to Visual-Music.org
  • no problems with that userid running the script on my server. i've not changed a single thing from what you posted above....

    http://merrydown.dyndns.org/temp/new.php

    maybe it's a permissions issue? the script will need write access to the server to create the local xml file? maybe you can test simplexml is working by bypassing the caching for now and reading directly from lastfm?

    try this....


    <?php
    $user = 'xcuntx';
    $remotefile = "http://ws.audioscrobbler.com/1.0/user/$user/recenttracks.xml";;
    $xml = @simplexml_load_file($remotefile);
    for ($t = 0; $t <= 9; $t++) {
    echo "<li>".$xml->track[$t]->artist." - ".$xml->track[$t]->name."</li>";
    }
    ?>

    • jye said...
    • User
    • 29 Apr 2007, 10:36
    Well I chmod to 755 ant try your script and it doesn't work

    I try without the @ and here is my warnings :
    Warning: file_get_contents(http://ws.audioscrobbler.com/1.0/user/xcuntx/recenttracks.xml) [function.file-get-contents]: failed to open stream: No route to host in /home/xxx/get_lastfm.php on line 276

    So it's the file_get_contents which doesnt work...

    It seems to be a server configuration problem but I don't know which option I have to activate

    Keep your ears clear, go to Visual-Music.org
    • jye said...
    • User
    • 29 Apr 2007, 14:14
    I made some test and it seems like if I try to access to a copy of the audioscrobbler xml file I made on an other server it works. But it doesnt works for xml file from audioscrobbler really strange...

    Maybe I'm blacklisted...

    Keep your ears clear, go to Visual-Music.org
    • dukny said...
    • User
    • 1 May 2007, 08:58
    what is this error?
    how do i ?

    Fatal error: Unknown function: curl_init() in /home/hosting/dukny/public_html/lastfm/recenttracks.php on line 27

    • spc0000 said...
    • User
    • 23 Jun 2007, 19:09
    astoever said:
    @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


    FIRST:
    I don't see any 'Recent Tracks' listed at the URI you provided: http://www.astoever.no

    SECOND:
    When you give the example of <? include 'scriptname'; ?> what do I put in for 'scriptname'?

    Thanks.

  • Whenever I haven't played music in a while (maybe 12 hours or so), the list that displays on my blog shows only 10 of these: @Dec 31, 7:00PM

    What's up with this? How do I fix the problem so that no matter how long it's been since I've listened to music the list will remain?

    Thanks.

    • rolle- said...
    • Subscriber
    • 15 Aug 2007, 16:04

    Little something

    I wanted "time x ago"-timestamp, so I did one. I'm not good at php but here's the result:


    $trackArray = explode(",", $track[$i]);

    $entry = explode("?", $trackArray[1]);

    echo "<li class=\"song\"><span class=\"artist\">".$entry[0]."</span><br /><span class=\"title\">".$entry[1]."</li>";

    $now = time();
    $played = date($trackArray[0]);
    $lastplayed = $now - $played;

    $minutes = $lastplayed / 60;
    $hours = $lastplayed / 3600;
    $days = $lastplayed / 86400;

    if ($minutes > 60) {

    if (round($hours, 0) == "1") {
    echo "<br /><span class=\"time\">".round($hours, 0)." hour ago</span></li>";
    } else { echo "<br /><span class=\"time\">".round($hours, 0)." hours ago</span></li>"; }

    } else {
    echo "<br /><span class=\"time\">".round($minutes, 0)." minutes ago</span></li>";
    }
    if ($hours > 24) {

    if (round($days, 0) == "1") {
    echo "<br /><span class=\"time\">".round($days, 0)." day ago</span></li>";
    } else { echo "<br /><span class=\"time\">".round($days, 0)." days ago</span></li>"; }

    }

    // $timestamp = date('j. n. Y, H:i:s',$trackArray[0]);
    }
    echo $wrapperEnd;
    }

    (if you want to try, replace this with the end of the code; note "echo $wrapperEnd; -> replace the original ending with this...)

    the Code is yet under construction. I'll develop it a bit but yet decided to tell this...

    Btw what's the point with question marks between the artist and song (for example "Dream Theater ? Octavarium"). I split those because I wanted them to be on different lines.

    I also have band-artwork but that aint gonna tell you (yet) :)

    What do you think?
    Seems to work for me for now.

    \,,/
    Edited by rolle- on 24 Aug 2007, 17:00
    • rolle- said...
    • Subscriber
    • 24 Aug 2007, 16:54

    Updated demo-pages

    \,,/
  • You can use PHP4 and parse plaintext or xml manually (using explode, substr and strpos).

    I have my own image charts for last.fm (here - http://i0ngunn3r.livejournal.com/profile), and I'm using plaintext.

    I can share code with you %)

    • rolle- said...
    • Subscriber
    • 18 Jun 2009, 00:32

    Updates

    \,,/
    • Sandi-G said...
    • User
    • 17 Jun 2010, 03:49
    Hi good job on the script. I would like to adapt it so I can do something useful with the data. Can you point me in the right direction of how to make it $_POST able?

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