-
Notifications
You must be signed in to change notification settings - Fork 2
/
Lastfm.class.php
96 lines (85 loc) · 2.72 KB
/
Lastfm.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/*************************************************************************
* Last.fm
*
* Class to easily display Last.fm information on your page
*
* @author Nathan Kowald
* @version 1.0
*
************************************************************************/
class Lastfm
{
private static $username;
private static $num_songs;
private static $error;
/**
* getLovedSongsFeed()
* Builds feed URL for last.fm loved tracks
*
* @return string $url Last.fm feed to grab loved songs from
*/
private static function getLovedSongsFeed()
{
$url = sprintf("http://ws.audioscrobbler.com/2.0/user/%s/lovedtracks.rss", self::$username);
return $url;
}
/**
* formatSongData()
* Formats songs. Links to the song on last.fm
*
* @param array $song_data Song data to format
* @return array $songs Returns formatted songs
*/
private static function formatSongData(Array $song_data)
{
$songs = array();
foreach ($song_data as $song) {
$songs[] = "<a href=\"".$song['link']."\">".$song['title']."</a>";
}
return $songs;
}
/**
* isValidUsername()
* Superficial check to see if given username is valid.
*
* @return boolean True if name is not blank, False if blank
*/
private static function isValidUsername($username)
{
if ($username == '') {
self::$error = 'Username cannot be blank';
return false;
}
return true;
}
/**
* getLovedSongs()
* Gets user's loved songs. From cache file or from the feed.
*
* @return array $songs Returns formatted songs
*/
public static function getLovedSongs($username='', $num_songs = 30)
{
if (!self::isValidUsername($username)) return array(self::$error);
// Check Feeder and Cache classes are accessible
if (!class_exists('Feeder')) return array('Could not find Feeder.class.php.');
if (!class_exists('Cache')) return array('Could not find Cache.class.php.');
self::$username = $username;
self::$num_songs = $num_songs;
$cache_filename = self::$username . '-loved-tracks.cache';
$cache_life = 86400; // 1 day
Cache::init($cache_filename, $cache_life);
if (Cache::cacheFileExists()) {
return Cache::getCache();
}
$song_data = Feeder::getItems(self::getLovedSongsFeed(), self::$num_songs, array('title', 'link'));
if (!is_array($song_data)) {
self::$error = "Last.fm loved tracks feed not found";
return array(self::$error);
}
$songs = self::formatSongData($song_data);
Cache::setCache($songs);
return $songs;
}
}