Use withQueryParameters for LastFM API

This commit is contained in:
Frankie B 2024-05-21 01:00:30 +01:00
parent 647f535fa4
commit 474ff1ea73
No known key found for this signature in database

View file

@ -2,19 +2,28 @@
namespace App\Http\Controllers;
use App\Models\Track;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use Illuminate\View\View;
class MusicController extends Controller
{
public function getCurrentTrack() {
// If it's already cached just return that
if (Cache::has('current_track')) {
return Cache::get('current_track');
}
$response = Http::get('https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user='.env('LASTFM_USER').'&nowplaying=true&format=json&api_key='.env('LASTFM_KEY'));
$response = Http::withQueryParameters([
'method' => 'user.getrecenttracks',
'user' => Config::get('services.lastfm.user'),
'format' => 'json',
'nowplaying' => 'true',
'api_key' => Config::get('services.lastfm.key')
])->get('https://ws.audioscrobbler.com/2.0/');
$data = $response->json();
error_log($response->body());
$track_data = $data["recenttracks"]["track"][0];
$current_track = [
'title' => $track_data["name"],
@ -26,10 +35,19 @@ public function getCurrentTrack() {
}
public function getTopTracks() {
// If it's already cached just return that
if (Cache::has('top_tracks')) {
return Cache::get('top_tracks');
}
$response = Http::get('https://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user='.env('LASTFM_USER').'&format=json&period=1month&limit=10&api_key='.env('LASTFM_KEY'));
$response = Http::withQueryParameters([
'method' => 'user.gettoptracks',
'user' => Config::get('services.lastfm.user'),
'format' => 'json',
'period' => '1month',
'limit' => 10,
'api_key' => Config::get('services.lastfm.key')
])->get('https://ws.audioscrobbler.com/2.0/');
$data = $response->json();
$topTracks = [];
foreach ($data["toptracks"]["track"] as $track) {