From 45defd706eca9188a92a6167ff828f3f61887b77 Mon Sep 17 00:00:00 2001 From: Frankie B Date: Tue, 15 Nov 2022 22:21:02 +0000 Subject: [PATCH] Add Last.fm and config --- inc/config.example.inc.php | 5 +++ inc/lastfm.inc.php | 65 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 inc/config.example.inc.php create mode 100644 inc/lastfm.inc.php diff --git a/inc/config.example.inc.php b/inc/config.example.inc.php new file mode 100644 index 0000000..433a60d --- /dev/null +++ b/inc/config.example.inc.php @@ -0,0 +1,5 @@ + "key" +); diff --git a/inc/lastfm.inc.php b/inc/lastfm.inc.php new file mode 100644 index 0000000..d0753b7 --- /dev/null +++ b/inc/lastfm.inc.php @@ -0,0 +1,65 @@ +

Last.fm

+ "https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=".CONF['lastfm_user']."&nowplaying=true&format=json&api_key=".CONF['lastfm_key'], + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 30, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "GET", +]); + +$current_response = curl_exec($curl_current); +$err = curl_error($curl_current); + +curl_close($curl_current); + +if ($err) { + die("cURL Error #:" . $err); +} +$current_response = json_decode($current_response, true); +$nowplaying = $current_response['recenttracks']['track'][0]; + +echo ' Now Playing: '.$nowplaying['name'].' • '.$nowplaying['artist']['#text'].''.PHP_EOL; + +$tracks_to_show = 10; + +$curl = curl_init(); + +curl_setopt_array($curl, [ + CURLOPT_URL => "https://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user=".CONF['lastfm_user']."&format=json&period=7day&api_key=".CONF['lastfm_key'], + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 30, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "GET", +]); + +$toptracks_response = curl_exec($curl); +$err = curl_error($curl); + +curl_close($curl); + +if ($err) { + die("cURL Error #:" . $err); +} +echo '

Top '.$tracks_to_show.' Tracks (Last 7 days) Profile

'.PHP_EOL; +echo '
    '.PHP_EOL; +$toptracks_response = json_decode($toptracks_response, true); +$tracks = $toptracks_response['toptracks']['track']; +$count = 0; +foreach ($tracks as $track) { + echo '
  1. '.PHP_EOL; + echo ' '.$track['name'].' • '.$track['artist']['name'].''.PHP_EOL; + echo ' ('.$track['playcount'].' plays)'.PHP_EOL; + echo '
  2. '.PHP_EOL; + if ($count >= $tracks_to_show - 1) break; + $count++; +} +echo '
'; \ No newline at end of file