Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $USERNAME = "FILLIN";
- $PASSOWRD = "FILLIN";
- function extractTrackIdsFromApiResponse($response)
- {
- return array_map(function ($play) {
- return $play['track']['id'];
- }, $response['results']);
- }
- function apiRequest($method, $path, $content = null)
- {
- $HOST = "https://funkwhale.xn--kll-sna.net";
- return json_decode(file_get_contents($HOST . $path, false, stream_context_create([
- 'http' => [
- 'header' => 'Content-type: application/json',
- 'method' => $method,
- 'content' => json_encode($content)
- ]
- ])), true);
- }
- function sortByOccurances($input){
- $array = array_count_values($input); //get all occurrences of each values
- arsort($array);
- print_r($array);//print occurrences array
- $final_array = array();
- foreach($array as $key=>$val){ // iterate over occurrences array
- for($i=0;$i<$val;$i++){ //apply loop based on occurrences number
- $final_array[] = $key; // assign same name to the final array
- }
- }
- print_r($final_array); // print final array
- return $final_array;
- }
- function authenticatedApiRequest($token, $method, $path, $content = null)
- {
- $HOST = "https://funkwhale.xn--kll-sna.net";
- return json_decode(file_get_contents($HOST . $path, false, stream_context_create([
- 'http' => [
- 'method' => $method,
- 'header' =>
- 'Authorization: JWT ' . $token . PHP_EOL
- . 'Content-Type: application/json',
- 'content' => json_encode($content)
- ]
- ])), true);
- }
- $token = apiRequest('POST', '/api/v1/token', [
- 'username' => $USERNAME,
- 'password' => $PASSOWRD
- ])['token'];
- $PAGE_SIZE = 50;
- $qres = authenticatedApiRequest($token, 'GET', '/api/v1/history/listenings/?scope=me&ordering=creation_date&page_size=' . $PAGE_SIZE);
- $TOTAL_RESULTS = $qres['count'];
- $TRACK_IDS = extractTrackIdsFromApiResponse($qres);
- //var_dump($TRACK_IDS);
- $NUM_PAGES = ($TOTAL_RESULTS + $PAGE_SIZE - 1) / $PAGE_SIZE;
- for ($n = 2; $n <= $NUM_PAGES; $n++) {
- $path = '/api/v1/history/listenings/?scope=me&ordering=creation_date&page_size=' . $PAGE_SIZE . '&page=' . $n;
- var_dump($path);
- $loopRes = authenticatedApiRequest($token, 'GET', $path);
- $TRACK_IDS = array_merge($TRACK_IDS, extractTrackIdsFromApiResponse($loopRes));
- }
- $newPlayListData = authenticatedApiRequest($token, 'POST', '/api/v1/playlists/', [
- 'name' => 'PLGEN_TOP_100',
- 'privacy_level' => 'me'
- ]);
- $newPlaylistId = $newPlayListData['id'];
- $res = authenticatedApiRequest($token, 'POST', '/api/v1/playlists/' . $newPlaylistId . '/add', [
- 'tracks' => array_slice(array_unique(sortByOccurances($TRACK_IDS)), 0, 50),
- 'allow_duplicates' => false
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement