Unfinished activity ('quantified self') tracker
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

audioscrobber.php 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?PHP
  2. class AudioscrobblerSource implements Source {
  3. private $data = array('last' => 0, 'data' => array());
  4. public function __construct() {
  5. $cache = dirname(dirname(__FILE__)) . '/cache/lastfm.dat';
  6. if (file_exists($cache)) {
  7. $this->data = unserialize(file_get_contents($cache));
  8. if (filemtime($cache) > strtotime('-1 day')) { return; }
  9. }
  10. $data = file_get_contents('http://ws.audioscrobbler.com/1.0/user/MD87/weeklychartlist.xml');
  11. $data = new SimpleXMLElement($data);
  12. $mtime = $data['last'];
  13. foreach($data->chart as $chart) {
  14. $from = (int) $chart['from'];
  15. $to = (int) $chart['to'];
  16. if ($to > $this->data['last']) {
  17. $mtime = $to;
  18. $newdata = "http://ws.audioscrobbler.com/1.0/user/MD87/weeklytrackchart.xml?from=$from&to=$to";
  19. echo "Retrieving $newdata<br>"; flush();
  20. $newdata = new SimpleXMLElement(file_get_contents($newdata));
  21. $top = array();
  22. foreach ($newdata->track as $track) {
  23. $title = (String) $track->name;
  24. $artist = (String) $track->artist;
  25. $link = (String) $track->url;
  26. $count = (int) $track->playcount;
  27. $message = '<li><a href="' . $link. '">' . $title .'</a> by ';
  28. $message .= $artist . ' (' . $count . ' play';
  29. $message .= ($count == 1 ? '' : 's') . ')';
  30. $top[] = $message;
  31. if (count($top) == 5) { break; }
  32. }
  33. $message = 'Top tracks this week: <ul>' . implode(', ', $top) . '</ul>';
  34. $this->data['data'][] = new Event(strtotime('23:59:59', $to), $message);
  35. $newdata = "http://ws.audioscrobbler.com/1.0/user/MD87/weeklyartistchart.xml?from=$from&to=$to";
  36. echo "Retrieving $newdata<br>"; flush();
  37. $newdata = new SimpleXMLElement(file_get_contents($newdata));
  38. $top = array();
  39. foreach ($newdata->artist as $track) {
  40. $artist = (String) $track->name;
  41. $link = (String) $track->url;
  42. $count = (int) $track->playcount;
  43. $message = '<li><a href="' . $link. '">' . $artist.'</a> ';
  44. $message .= ' (' . $count . ' play';
  45. $message .= ($count == 1 ? '' : 's') . ')';
  46. $top[] = $message;
  47. if (count($top) == 3) { break; }
  48. }
  49. $message = 'Top artists this week: <ul>' . implode(', ', $top) . '</ul>';
  50. $this->data['data'][] = new Event(strtotime('23:59:59', $to), $message);
  51. }
  52. }
  53. $this->data['last'] = $mtime;
  54. file_put_contents($cache, serialize($this->data));
  55. }
  56. public function getData() {
  57. return $this->data['data'];
  58. }
  59. }
  60. $_SOURCES[] =& new AudioscrobblerSource();
  61. ?>