Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 449c3a1

Browse files
committed
✨ LastFM: automatic endpoint map creation
1 parent 0388800 commit 449c3a1

File tree

3 files changed

+323
-115
lines changed

3 files changed

+323
-115
lines changed

examples/endpoint-map/LastFM.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* @created 23.03.2021
4+
* @author smiley <smiley@chillerlan.net>
5+
* @copyright 2021 smiley
6+
* @license MIT
7+
*/
8+
9+
use chillerlan\OAuth\Providers\LastFM\LastFM;
10+
use chillerlan\PrototypeDOM\Document;
11+
12+
$ENVVAR = 'LASTFM';
13+
14+
require_once __DIR__.'/create-endpointmap-common.php';
15+
16+
/**
17+
* @var \Psr\Http\Client\ClientInterface $http
18+
* @var \chillerlan\OAuth\Storage\OAuthStorageInterface $storage
19+
* @var \chillerlan\Settings\SettingsContainerInterface $options
20+
* @var \Psr\Log\LoggerInterface $logger
21+
*/
22+
23+
$lastfm = new LastFM($http, $storage, $options, $logger);
24+
$url = 'https://www.last.fm/api';
25+
$root = new Document(file_get_contents($url));
26+
$urls = $root->querySelectorAll('ul.sidebar-links:nth-child(1) > li:nth-child(3) a');
27+
$content = [];
28+
29+
foreach($urls as $a){
30+
$param = new stdClass;
31+
$params = [];
32+
# $str = [];
33+
34+
$href = $a->getAttribute('href');
35+
$path = str_replace('/api/show/', '' ,$href);
36+
37+
if(in_array($path, ['auth.getMobileSession', 'auth.getSession', 'auth.getToken', 'track.scrobble'])){
38+
continue;
39+
}
40+
41+
$link = 'https://www.last.fm'.$href;
42+
$page = (new Document(file_get_contents($link)));
43+
$name = lcfirst(implode('', array_map('ucfirst', explode('.', $path))));
44+
$desc = trim($page->select(['.theme-default-content h1'])[0]->next('p')->nodeValue);
45+
46+
foreach($page->select(['.theme-default-content #params'])[0]->next('p')->childNodes as $nodeList){
47+
48+
if($nodeList->nodeName === 'strong'){
49+
$param->name = str_replace('[0|1]', '',trim($nodeList->nodeValue));
50+
}
51+
# elseif($nodeList->nodeName === '#text'){
52+
# $str[] = $nodeList->nodeValue;
53+
# }
54+
elseif($nodeList->nodeName === 'br'){
55+
56+
if(in_array($param->name, ['api_key', 'api_sig', 'sk'])){
57+
continue;
58+
}
59+
60+
# [$required, $param->desc] = array_map('trim', explode(':', implode('', $str)));
61+
# $param->required = strpos($required, 'Required') !== false;
62+
# $str = [];
63+
$params[] = $param;
64+
$param = new stdClass;
65+
}
66+
}
67+
68+
$method = strpos($page->select(['.theme-default-content #auth'])[0]->next('p')->nodeValue, 'HTTP POST') !== false
69+
? 'POST' : 'GET';
70+
71+
$param_names = array_column($params, 'name');
72+
73+
sort($param_names);
74+
75+
if($param_names === ['']){
76+
$param_names = null;
77+
}
78+
79+
$content[] = '
80+
/**
81+
* '.$desc.'
82+
*
83+
* @link '.$link.'
84+
*/
85+
protected array $'.$name.' = [
86+
\'path\' => \''.$path.'\',
87+
\'method\' => \''.$method.'\',
88+
\'query\' => '.($method === 'GET' && $param_names ? '[\''.implode('\', \'', $param_names).'\']' : 'null').',
89+
\'body\' => '.($method === 'POST' && $param_names ? '[\''.implode('\', \'', $param_names).'\']' : 'null').',
90+
];';
91+
92+
$logger->info($path . ' - '. $href);
93+
}
94+
95+
// and replace the class
96+
createEndpointMap($content, $url, $lastfm->endpoints);
97+
98+
exit;

src/LastFM/LastFM.php

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,58 +21,59 @@
2121
use const PHP_QUERY_RFC1738;
2222

2323
/**
24-
* @method \Psr\Http\Message\ResponseInterface albumAddTags(array $body = ['mbid', 'album', 'artist', 'tags'])
25-
* @method \Psr\Http\Message\ResponseInterface albumGetInfo(array $params = ['mbid', 'album', 'artist', 'username', 'lang', 'autocorrect'])
26-
* @method \Psr\Http\Message\ResponseInterface albumGetTags(array $params = ['mbid', 'album', 'artist', 'user', 'autocorrect'])
27-
* @method \Psr\Http\Message\ResponseInterface albumGetTopTags(array $params = ['mbid', 'album', 'artist', 'autocorrect'])
28-
* @method \Psr\Http\Message\ResponseInterface albumRemoveTag(array $body = ['mbid', 'album', 'artist', 'tag'])
24+
* @method \Psr\Http\Message\ResponseInterface albumAddTags(array $body = ['album', 'artist', 'tags'])
25+
* @method \Psr\Http\Message\ResponseInterface albumGetInfo(array $params = ['album', 'artist', 'autocorrect[0|1]', 'lang', 'mbid', 'username'])
26+
* @method \Psr\Http\Message\ResponseInterface albumGetTags(array $params = ['album', 'artist', 'autocorrect[0|1]', 'mbid', 'user'])
27+
* @method \Psr\Http\Message\ResponseInterface albumGetTopTags(array $params = ['album', 'artist', 'autocorrect[0|1]', 'mbid'])
28+
* @method \Psr\Http\Message\ResponseInterface albumRemoveTag(array $body = ['album', 'artist', 'tag'])
2929
* @method \Psr\Http\Message\ResponseInterface albumSearch(array $params = ['album', 'limit', 'page'])
30-
* @method \Psr\Http\Message\ResponseInterface artistAddTags(array $body = ['mbid', 'artist', 'tags'])
30+
* @method \Psr\Http\Message\ResponseInterface artistAddTags(array $body = ['artist', 'tags'])
3131
* @method \Psr\Http\Message\ResponseInterface artistGetCorrection(array $params = ['artist'])
32-
* @method \Psr\Http\Message\ResponseInterface artistGetInfo(array $params = ['mbid', 'artist', 'username', 'lang', 'autocorrect'])
33-
* @method \Psr\Http\Message\ResponseInterface artistGetSimilar(array $params = ['mbid', 'artist', 'limit', 'autocorrect'])
34-
* @method \Psr\Http\Message\ResponseInterface artistGetTags(array $params = ['mbid', 'artist', 'user', 'autocorrect'])
35-
* @method \Psr\Http\Message\ResponseInterface artistGetTopAlbums(array $params = ['mbid', 'artist', 'autocorrect', 'page', 'limit'])
36-
* @method \Psr\Http\Message\ResponseInterface artistGetTopTags(array $params = ['mbid', 'artist', 'autocorrect'])
37-
* @method \Psr\Http\Message\ResponseInterface artistGetTopTracks(array $params = ['mbid', 'artist', 'autocorrect', 'page', 'limit'])
38-
* @method \Psr\Http\Message\ResponseInterface artistRemoveTag(array $body = ['mbid', 'artist', 'tag'])
32+
* @method \Psr\Http\Message\ResponseInterface artistGetInfo(array $params = ['artist', 'autocorrect[0|1]', 'lang', 'mbid', 'username'])
33+
* @method \Psr\Http\Message\ResponseInterface artistGetSimilar(array $params = ['artist', 'autocorrect[0|1]', 'limit', 'mbid'])
34+
* @method \Psr\Http\Message\ResponseInterface artistGetTags(array $params = ['artist', 'autocorrect[0|1]', 'mbid', 'user'])
35+
* @method \Psr\Http\Message\ResponseInterface artistGetTopAlbums(array $params = ['artist', 'autocorrect[0|1]', 'limit', 'mbid', 'page'])
36+
* @method \Psr\Http\Message\ResponseInterface artistGetTopTags(array $params = ['artist', 'autocorrect[0|1]', 'mbid'])
37+
* @method \Psr\Http\Message\ResponseInterface artistGetTopTracks(array $params = ['artist', 'autocorrect[0|1]', 'limit', 'mbid', 'page'])
38+
* @method \Psr\Http\Message\ResponseInterface artistRemoveTag(array $body = ['artist', 'tag'])
3939
* @method \Psr\Http\Message\ResponseInterface artistSearch(array $params = ['artist', 'limit', 'page'])
4040
* @method \Psr\Http\Message\ResponseInterface chartGetTopArtists(array $params = ['limit', 'page'])
4141
* @method \Psr\Http\Message\ResponseInterface chartGetTopTags(array $params = ['limit', 'page'])
4242
* @method \Psr\Http\Message\ResponseInterface chartGetTopTracks(array $params = ['limit', 'page'])
43-
* @method \Psr\Http\Message\ResponseInterface geoGetTopArtists(array $params = ['country', 'location', 'limit', 'page'])
44-
* @method \Psr\Http\Message\ResponseInterface geoGetTopTracks(array $params = ['country', 'location', 'limit', 'page'])
45-
* @method \Psr\Http\Message\ResponseInterface libraryGetArtists(array $params = ['user', 'limit', 'page'])
46-
* @method \Psr\Http\Message\ResponseInterface tagGetInfo(array $params = ['tag', 'lang'])
43+
* @method \Psr\Http\Message\ResponseInterface geoGetTopArtists(array $params = ['country', 'limit', 'page'])
44+
* @method \Psr\Http\Message\ResponseInterface geoGetTopTracks(array $params = ['country', 'limit', 'location', 'page'])
45+
* @method \Psr\Http\Message\ResponseInterface libraryGetArtists(array $params = ['limit', 'page', 'user'])
46+
* @method \Psr\Http\Message\ResponseInterface tagGetInfo(array $params = ['lang', 'tag'])
4747
* @method \Psr\Http\Message\ResponseInterface tagGetSimilar(array $params = ['tag'])
48-
* @method \Psr\Http\Message\ResponseInterface tagGetTopAlbums(array $params = ['tag', 'limit', 'page'])
49-
* @method \Psr\Http\Message\ResponseInterface tagGetTopArtists(array $params = ['tag', 'limit', 'page'])
48+
* @method \Psr\Http\Message\ResponseInterface tagGetTopAlbums(array $params = ['limit', 'page', 'tag'])
49+
* @method \Psr\Http\Message\ResponseInterface tagGetTopArtists(array $params = ['limit', 'page', 'tag'])
5050
* @method \Psr\Http\Message\ResponseInterface tagGetTopTags()
51-
* @method \Psr\Http\Message\ResponseInterface tagGetTopTracks(array $params = ['tag', 'limit', 'page'])
51+
* @method \Psr\Http\Message\ResponseInterface tagGetTopTracks(array $params = ['limit', 'page', 'tag'])
5252
* @method \Psr\Http\Message\ResponseInterface tagGetWeeklyChartList(array $params = ['tag'])
53-
* @method \Psr\Http\Message\ResponseInterface trackAddTags(array $body = ['mbid', 'artist', 'track', 'tags'])
53+
* @method \Psr\Http\Message\ResponseInterface trackAddTags(array $body = ['artist', 'tags', 'track'])
5454
* @method \Psr\Http\Message\ResponseInterface trackGetCorrection(array $params = ['artist', 'track'])
55-
* @method \Psr\Http\Message\ResponseInterface trackGetInfo(array $params = ['mbid', 'artist', 'track', 'username', 'autocorrect'])
56-
* @method \Psr\Http\Message\ResponseInterface trackGetSimilar(array $params = ['mbid', 'artist', 'track', 'autocorrect', 'limit'])
57-
* @method \Psr\Http\Message\ResponseInterface trackGetTags(array $params = ['mbid', 'artist', 'track', 'autocorrect', 'user'])
58-
* @method \Psr\Http\Message\ResponseInterface trackGetTopTags(array $params = ['mbid', 'artist', 'track', 'autocorrect'])
59-
* @method \Psr\Http\Message\ResponseInterface trackLove(array $body = ['mbid', 'artist', 'track'])
60-
* @method \Psr\Http\Message\ResponseInterface trackRemoveTag(array $body = ['mbid', 'artist', 'track', 'tag'])
61-
* @method \Psr\Http\Message\ResponseInterface trackSearch(array $params = ['artist', 'track', 'limit', 'page'])
62-
* @method \Psr\Http\Message\ResponseInterface trackUnlove(array $body = ['mbid', 'artist', 'track'])
63-
* @method \Psr\Http\Message\ResponseInterface trackUpdateNowPlaying(array $body = ['mbid', 'artist', 'track', 'album', 'trackNumber', 'context', 'duration', 'albumArtist'])
64-
* @method \Psr\Http\Message\ResponseInterface userGetArtistTracks(array $params = ['user', 'artist', 'limit', 'page', 'startTimestamp', 'endTimestamp'])
65-
* @method \Psr\Http\Message\ResponseInterface userGetFriends(array $params = ['user', 'limit', 'page', 'recenttracks'])
55+
* @method \Psr\Http\Message\ResponseInterface trackGetInfo(array $params = ['artist', 'autocorrect[0|1]', 'mbid', 'track', 'username'])
56+
* @method \Psr\Http\Message\ResponseInterface trackGetSimilar(array $params = ['artist', 'autocorrect[0|1]', 'limit', 'mbid', 'track'])
57+
* @method \Psr\Http\Message\ResponseInterface trackGetTags(array $params = ['artist', 'autocorrect[0|1]', 'mbid', 'track', 'user'])
58+
* @method \Psr\Http\Message\ResponseInterface trackGetTopTags(array $params = ['artist', 'autocorrect[0|1]', 'mbid', 'track'])
59+
* @method \Psr\Http\Message\ResponseInterface trackLove(array $body = ['artist', 'track'])
60+
* @method \Psr\Http\Message\ResponseInterface trackRemoveTag(array $body = ['artist', 'tag', 'track'])
61+
* @method \Psr\Http\Message\ResponseInterface trackSearch(array $params = ['artist', 'limit', 'page', 'track'])
62+
* @method \Psr\Http\Message\ResponseInterface trackUnlove(array $body = ['artist', 'track'])
63+
* @method \Psr\Http\Message\ResponseInterface trackUpdateNowPlaying(array $body = ['album', 'albumArtist', 'artist', 'context', 'duration', 'mbid', 'track', 'trackNumber'])
64+
* @method \Psr\Http\Message\ResponseInterface userGetFriends(array $params = ['limit', 'page', 'recenttracks', 'user'])
6665
* @method \Psr\Http\Message\ResponseInterface userGetInfo(array $params = ['user'])
67-
* @method \Psr\Http\Message\ResponseInterface userGetLovedTracks(array $params = ['user', 'limit', 'page'])
68-
* @method \Psr\Http\Message\ResponseInterface userGetPersonalTags(array $params = ['user', 'limit', 'page', 'tag', 'taggingtype'])
69-
* @method \Psr\Http\Message\ResponseInterface userGetRecentTracks(array $params = ['user', 'limit', 'page', 'from', 'to', 'extended'])
70-
* @method \Psr\Http\Message\ResponseInterface userGetTopAlbums(array $params = ['user', 'limit', 'page', 'period'])
71-
* @method \Psr\Http\Message\ResponseInterface userGetTopArtists(array $params = ['user', 'limit', 'page', 'period'])
72-
* @method \Psr\Http\Message\ResponseInterface userGetTopTags(array $params = ['user', 'limit', 'page'])
73-
* @method \Psr\Http\Message\ResponseInterface userGetWeeklyAlbumChart(array $params = ['user', 'from', 'to'])
74-
* @method \Psr\Http\Message\ResponseInterface userGetWeeklyArtistChart(array $params = ['user', 'from', 'to'])
75-
* @method \Psr\Http\Message\ResponseInterface userGetWeeklyTrackChart(array $params = ['user', 'from', 'to'])
66+
* @method \Psr\Http\Message\ResponseInterface userGetLovedTracks(array $params = ['limit', 'page', 'user'])
67+
* @method \Psr\Http\Message\ResponseInterface userGetPersonalTags(array $params = ['limit', 'page', 'tag', 'taggingtype[artist|album|track]', 'user'])
68+
* @method \Psr\Http\Message\ResponseInterface userGetRecentTracks(array $params = ['extended', 'from', 'limit', 'page', 'to', 'user'])
69+
* @method \Psr\Http\Message\ResponseInterface userGetTopAlbums(array $params = ['limit', 'page', 'period', 'user'])
70+
* @method \Psr\Http\Message\ResponseInterface userGetTopArtists(array $params = ['limit', 'page', 'period', 'user'])
71+
* @method \Psr\Http\Message\ResponseInterface userGetTopTags(array $params = ['limit', 'user'])
72+
* @method \Psr\Http\Message\ResponseInterface userGetTopTracks(array $params = ['limit', 'page', 'period', 'user'])
73+
* @method \Psr\Http\Message\ResponseInterface userGetWeeklyAlbumChart(array $params = ['from', 'to', 'user'])
74+
* @method \Psr\Http\Message\ResponseInterface userGetWeeklyArtistChart(array $params = ['from', 'to', 'user'])
75+
* @method \Psr\Http\Message\ResponseInterface userGetWeeklyChartList(array $params = ['user'])
76+
* @method \Psr\Http\Message\ResponseInterface userGetWeeklyTrackChart(array $params = ['from', 'to', 'user'])
7677
*/
7778
class LastFM extends OAuthProvider{
7879

0 commit comments

Comments
 (0)