Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.77 KB | None | 0 0
  1. <?php
  2.  
  3. $USERNAME = "FILLIN";
  4. $PASSOWRD = "FILLIN";
  5.  
  6. function extractTrackIdsFromApiResponse($response)
  7. {
  8.     return array_map(function ($play) {
  9.         return $play['track']['id'];
  10.     }, $response['results']);
  11. }
  12.  
  13. function apiRequest($method, $path, $content = null)
  14. {
  15.     $HOST = "https://funkwhale.xn--kll-sna.net";
  16.     return json_decode(file_get_contents($HOST . $path, false, stream_context_create([
  17.         'http' => [
  18.             'header' => 'Content-type: application/json',
  19.             'method' => $method,
  20.             'content' => json_encode($content)
  21.         ]
  22.     ])), true);
  23. }
  24.  
  25. function sortByOccurances($input){
  26.     $array = array_count_values($input); //get all occurrences of each values
  27.     arsort($array);
  28.     print_r($array);//print occurrences array
  29.     $final_array = array();
  30.  
  31.     foreach($array as $key=>$val){ // iterate over occurrences array
  32.         for($i=0;$i<$val;$i++){ //apply loop based on occurrences number
  33.             $final_array[] = $key; // assign same name to the final array
  34.         }
  35.     }
  36.  
  37.     print_r($final_array); // print final array
  38.     return $final_array;
  39. }
  40. function authenticatedApiRequest($token, $method, $path, $content = null)
  41. {
  42.     $HOST = "https://funkwhale.xn--kll-sna.net";
  43.     return json_decode(file_get_contents($HOST . $path, false, stream_context_create([
  44.         'http' => [
  45.             'method' => $method,
  46.             'header' =>
  47.                 'Authorization: JWT ' . $token . PHP_EOL
  48.                 . 'Content-Type: application/json',
  49.             'content' => json_encode($content)
  50.         ]
  51.     ])), true);
  52. }
  53.  
  54. $token = apiRequest('POST', '/api/v1/token', [
  55.     'username' => $USERNAME,
  56.     'password' => $PASSOWRD
  57. ])['token'];
  58.  
  59. $PAGE_SIZE = 50;
  60. $qres = authenticatedApiRequest($token, 'GET', '/api/v1/history/listenings/?scope=me&ordering=creation_date&page_size=' . $PAGE_SIZE);
  61. $TOTAL_RESULTS = $qres['count'];
  62. $TRACK_IDS = extractTrackIdsFromApiResponse($qres);
  63. //var_dump($TRACK_IDS);
  64. $NUM_PAGES = ($TOTAL_RESULTS + $PAGE_SIZE - 1) / $PAGE_SIZE;
  65.  
  66. for ($n = 2; $n <= $NUM_PAGES; $n++) {
  67.     $path = '/api/v1/history/listenings/?scope=me&ordering=creation_date&page_size=' . $PAGE_SIZE . '&page=' . $n;
  68.     var_dump($path);
  69.     $loopRes = authenticatedApiRequest($token, 'GET', $path);
  70.     $TRACK_IDS = array_merge($TRACK_IDS, extractTrackIdsFromApiResponse($loopRes));
  71. }
  72.  
  73. $newPlayListData = authenticatedApiRequest($token, 'POST', '/api/v1/playlists/', [
  74.     'name' => 'PLGEN_TOP_100',
  75.     'privacy_level' => 'me'
  76. ]);
  77. $newPlaylistId = $newPlayListData['id'];
  78.  
  79. $res = authenticatedApiRequest($token, 'POST', '/api/v1/playlists/' . $newPlaylistId . '/add', [
  80.     'tracks' => array_slice(array_unique(sortByOccurances($TRACK_IDS)), 0, 50),
  81.     'allow_duplicates' => false
  82. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement