-
Notifications
You must be signed in to change notification settings - Fork 3
/
lastfmrss-working.php
112 lines (98 loc) · 3.38 KB
/
lastfmrss-working.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
<?php
// http://simplehtmldom.sourceforge.net/
require_once ('simple_html_dom.php');
// Optionally set the default Last.fm username and real name
$user = '';
$name = '';
if (isset($_GET['user'])) {
$user = urlencode ($_GET['user']);
}
// Specify criteria to handle timeouts and certificate issues
$context = stream_context_create(
array(
'http' => array(
'follow_location' => false,
'timeout' => 10,
),
'ssl' => array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
)
);
// Grab the HTML for the tracks
if (isset($_GET['loved'])) {
$type = 'loved';
$html = file_get_html("https://www.last.fm/user/{$user}/loved?page=1", false, $context);
} else {
$type = 'played';
$html = file_get_html("https://www.last.fm/user/{$user}/library?page=1", false, $context);
}
// Grab the HTML for the real name
$profile_html = file_get_html("https://www.last.fm/user/{$user}");
foreach($profile_html->find('span[class=header-title-display-name]') as $getname) {
$name = trim($getname->plaintext);
}
// Start the output
header("Content-Type: application/rss+xml");
header("Content-type: text/xml; charset=utf-8");
header("Cache-Control:s-maxage=600");
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<lastBuildDate><?php echo gmdate('D, d M Y H:i:s O', time()) ?></lastBuildDate>
<language>en</language>
<title><?php echo $name ?> on Last.fm</title>
<description>Recently <?php echo $type ?> tracks scrobbled on Last.fm by <?php echo $name ?></description>
<link>https://www.last.fm/user/<?php echo $user ?></link>
<ttl>30</ttl>
<generator>splo.me</generator>
<category>Personal</category>
<?php
$i = 0;
foreach($html->find('.js-focus-controls-container') as $row) {
foreach($row->find('.chartlist-artist') as $content) {
$artist = $content->find('a',0)->plaintext;
}
foreach($row->find('.chartlist-name') as $content) {
$title = $content->find('a',0)->plaintext;
$link = $content->find('a',0)->href;
$cover = 'https://lastfm-img2.akamaized.net/i/u/174s/4128a6eb29f94943c9d206c08e625904';
}
// $desc = 'https://www.last.fm'. $link;
// $desc = '<a href="'.$desc.'">'.$artist.'</a>';
// Grab the HTML for the current track page to extract the cover art
// $track_html = file_get_html("https://www.last.fm" . $content->find('a',1)->href);
// foreach($track_html->find('.header-avatar-playlink]') as $track_avatar) {
// $cover = $track_avatar->find('img',0)->src;
// }
foreach($row->find('.chartlist-timestamp') as $timestamp) {
$span = str_get_html(trim($timestamp->innertext)); // don't ask
$span->find('span');
$arr = (array)$span;
$node = (array) $arr['nodes'][1];
$timestamp = ($node['attr']['title']);
$playdate = gmdate("D, d M Y H:i:s O", strtotime($timestamp));
$desc = gmdate("l, F jS g:i:s A", strtotime($timestamp));
}
?>
<item>
<title><?php echo $artist.' — '.$title ?></title>
<pubDate><?php echo $playdate; ?></pubDate>
<link>https://www.last.fm<?php echo $link ?></link>
<guid isPermaLink="false"><?php echo $artist . ' — ' . $title . ' — ' . $playdate ?></guid>
<description><![CDATA[<?php echo $desc?>]]></description>
<media:content
xmlns:media="http://search.yahoo.com/mrss/"
url="<?php echo $cover ?>"
medium="image"
type="image/jpeg"
width="174"
height="174" />
</item>
<?php
$i++;
}
?>
</channel>
</rss>