All pastes #98735 Raw Edit

chasba

public text v1 · immutable
#98735 ·published 2006-07-24 19:27 UTC
rendered paste body
<?php

// settings
$lastfmUsername = "chasba";
$lastfmCache = "content/scripts/lastfm.recent.cache";
$secondsBeforeUpdate = 180; // be nice to their link
$numberOfSongs = 10; // 10 is max
$socketTimeout = 10; // seconds to wait for response from audioscrobbler
$emptyCache = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8226; last.fm har problemer (eller s&aring; har ikke jeg spilt musikk i det siste). pr&oslash;v senere.";
$wrapperStart = "";
$wrapperEnd = "";

// grab the stuff
if(!file_exists($lastfmCache)) touch($lastfmCache);
$lastModified = filemtime($lastfmCache);
if(time() - $lastModified > $secondsBeforeUpdate) {
  @ini_set("default_socket_timeout", $socketTimeout);
  $recentlyPlayedSongs = @file_get_contents("http://ws.audioscrobbler.com/1.0/user/$lastfmUsername/recenttracks.txt");
  if(strlen($recentlyPlayedSongs) == 1) {
    touch($lastfmCache);
  }
  else {
    $handle = fopen($lastfmCache, "w");
    fwrite($handle, $recentlyPlayedSongs);
    fclose($handle);
  }
}
// post the info
$cacheSize = filesize($lastfmCache);
if($cacheSize < 5) echo $emptyCache;
else {
  $recentlyPlayedSongs = file_get_contents($lastfmCache);
  $recentlyPlayedSongs = utf8_decode($recentlyPlayedSongs); // UTF8 h8
  echo $wrapperStart;
  $track = explode("\n", $recentlyPlayedSongs);
  for ($i = 0; $i < $numberOfSongs; $i++) {
    $trackArray = explode(",", $track[$i]);
	$timestamp = strftime('kl. %T, %d/%m%/%y',$trackArray[0]);
	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8226; ".$trackArray[1]." (spilt ".$timestamp.")<br />";
}
  echo $wrapperEnd;
}
?>