diff options
author | Frankie B <git@diskfloppy.me> | 2024-06-11 18:02:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-11 18:02:01 +0100 |
commit | 0f52d80ca67a49258b235f5831163dd72fbd54cf (patch) | |
tree | 9c5cd36b6e0a233e09ac88a4409fb68c63e4781a /app/Http/Controllers/MusicController.php | |
parent | a64bcc2c4639d5804b6dada23151bfcb8b198121 (diff) |
Merge MVC rewrite into master (#21)
* Just commit it all
* Require auth
* crap
* Update homepage
* Block AI scrapers
* Update cache update script
* Add dummy file
* Remove unnecessary lastfm config var
* Use withQueryParameters for LastFM API
* Fix embeds
* Update example env
* Smard
Diffstat (limited to 'app/Http/Controllers/MusicController.php')
-rw-r--r-- | app/Http/Controllers/MusicController.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/app/Http/Controllers/MusicController.php b/app/Http/Controllers/MusicController.php new file mode 100644 index 0000000..5e31d86 --- /dev/null +++ b/app/Http/Controllers/MusicController.php @@ -0,0 +1,69 @@ +<?php + +namespace App\Http\Controllers; + +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::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"], + 'artist' => $track_data["artist"]["#text"], + 'url' => $track_data["url"], + ]; + Cache::put('current_track', $current_track, now()->addSeconds(15)); + return $current_track; + } + + public function getTopTracks() { + // If it's already cached just return that + if (Cache::has('top_tracks')) { + return Cache::get('top_tracks'); + } + + $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) { + $topTracks[] = [ + 'title' => $track["name"], + 'artist' => $track["artist"]["name"], + 'url' => $track["url"], + 'plays' => $track["playcount"], + ]; + } + Cache::put('top_tracks', $topTracks, now()->addSeconds(15)); + return $topTracks; + } + public function show() : View { + return view('music') + ->with('current_track', $this->getCurrentTrack()) + ->with('top_tracks', $this->getTopTracks()); + } +} |