-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLastFM.php
139 lines (111 loc) · 3.85 KB
/
LastFM.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Nette Control for receiving last 10 tracks from LastFM.
*
* @author Tom Nedvěd (http://programator.ne2d.cz)
*/
class LastFM extends /*Nette::Application::*/Control {
/** @var int */
public static $cacheExpire = 60; // 1 min
/** @var string */
public static $cacheDir;
const LASTFM_RECENTTRACKS = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&api_key=b25b959554ed76058ac220b7b2e0a026';
private $user;
private $pass;
/**
* Creates object using your credentials.
* @param string user name
* @throws Exception
*/
public function __construct($user)
{
if (!extension_loaded('curl')) {
throw new TwitterException('PHP extension CURL is not loaded.');
}
$this->user = $user;
$this->pass = Null;
}
public function render()
{
$template = $this->getTemplate();
$template->setFile(dirname(__FILE__) . '/lastfmrecenttracks.phtml');
$template->registerFilter(new LatteFilter);
$result = $this->cachedHttpRequest(self::LASTFM_RECENTTRACKS.'&user='.$this->user);
$template->recenttracks = $result->recenttracks;
$template->user = $this->user;
$template->render(TRUE);
}
/**
* Process HTTP request.
* @param string URL
* @param array of post data
* @return mixed
*/
private function httpRequest($url, $postData = NULL)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERPWD, "$this->user:$this->pass");
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // no echo, just return result
if ($postData) {
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
}
$result = curl_exec($curl);
if (curl_errno($curl)) {
throw new LastFMException('Server error: ' . curl_error($curl));
}
$type = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
if (strpos($type, 'xml')) {
$payload = new SimpleXMLIterator($result);
} elseif (strpos($type, 'json')) {
$payload = @json_decode($result); // intentionally @
}
if (empty($payload)) {
throw new LastFMException('Invalid server response');
}
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($code >= 400) {
throw new LastFMException(isset($payload->error) ? $payload->error : "Server error #$code", $code);
}
return $payload;
}
/**
* Cached HTTP request.
* @param string URL
* @return mixed
*/
private function cachedHttpRequest($url)
{
if (!self::$cacheDir) {
return $this->httpRequest($url);
}
$cacheFile = self::$cacheDir . '/lastFM_'. $this->user .'.'. md5($url);
$cache = @file_get_contents($cacheFile); // intentionally @
$cache = strncmp($cache, '<', 1) ? @json_decode($cache) : new SimpleXMLIterator($cache); // intentionally @
if ($cache && @filemtime($cacheFile) + self::$cacheExpire > time()) { // intentionally @
return $cache;
}
try {
$payload = $this->httpRequest($url);
file_put_contents($cacheFile, $payload instanceof SimpleXMLIterator ? $payload->asXml() : json_encode($payload));
return $payload;
} catch (LastFMException $e) {
if ($cache) {
return $cache;
}
throw $e;
}
}
}
/**
* An exception generated by LastFM.
*/
class LastFMException extends Exception
{
}
?>