Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. <?php header('Content-Type: application/json');
  2. session_start();
  3.  
  4. // ini_set('display_errors', 1);
  5. // ini_set('display_startup_errors', 1);
  6. // error_reporting(E_ALL);
  7. require 'vendor/Spotify-Web-Api-Php/autoload.php';
  8. require 'configuration/config.php';
  9.  
  10. $session = new SpotifyWebAPI\Session(
  11. '63df7e2e6d5041b188f0d4d01471d9cc',
  12. '010530cbd2c34a418624ffd26f3bc2a6',
  13. 'admin/spotify/callback.php'
  14. );
  15.  
  16. // TEST LE LIEN
  17. function get_http_response_code($url) {
  18. $headers = get_headers($url);
  19. return substr($headers[0], 9, 3);
  20. }
  21.  
  22. $reponse = $db->query("SELECT spotify_refresh FROM configuration");
  23. while ($donnees = $reponse->fetch())
  24. {
  25. $session->refreshAccessToken($donnees['spotify_refresh']);
  26.  
  27. $_SESSION['accessToken'] = $session->getAccessToken();
  28. }
  29.  
  30.  
  31. // RECUPERE LES INFOS DE LA POCHETTE
  32. function search($searchTerm){
  33.  
  34. $opts = [
  35. "http" => [
  36. "header" => "Authorization: Bearer ".$_SESSION['accessToken']
  37. ]
  38. ];
  39.  
  40. $context = stream_context_create($opts);
  41.  
  42. $string = $searchTerm;
  43. if ( preg_match("~\b&amp;\b~",$string) ){
  44.  
  45. $str = str_replace("&amp;", "&", $searchTerm, $searchTerm);
  46. }
  47. $url = "https://api.spotify.com/v1/search?q=". urlencode($searchTerm)."&type=track&market=FR&limit=1";
  48.  
  49. $result = file_get_contents($url, false, $context);
  50.  
  51. //print_r($result);
  52.  
  53. if($result !== false){
  54. //print_r($result);
  55. //echo $result['tracks']['items'][0]['albums']['images'][0]['url'];
  56. return json_decode($result, true);
  57. }
  58. return false;
  59. }
  60.  
  61. function propre($string) {
  62.  
  63. $string = preg_replace('#\((.+)\)#U', '', $string);
  64. $string = preg_replace('#\[(.+)\]#U', '', $string);
  65. $string = preg_replace('#\FT(.+)\-#U', '', $string);
  66. $string = str_replace(';', ' ', $string);
  67. $string = str_replace('_', ' ', $string);
  68. $string = htmlspecialchars($string);
  69. return $string;
  70. }
  71.  
  72. function limitString($chaine) {
  73. $max = 25;
  74.  
  75. if (strlen($chaine) >= $max) {
  76. $chaine = substr($chaine, 0, $max);
  77. $espace = strrpos($chaine, " ");
  78. $chaine = substr($chaine, 0, $espace)."...";
  79. }
  80. return $chaine;
  81. }
  82.  
  83. if(!empty($_GET['url'])):
  84.  
  85. $url = $_GET['url'];
  86.  
  87. if($_GET['type'] == "/stream"): // SHOUTCAST
  88.  
  89. if (!$data = file_get_contents($url.'/stats?sid=1&json=1')) {
  90. $error = error_get_last();
  91. echo "HTTP request failed. Error was: " . $error['message'];
  92. }
  93. $shoutcast = json_decode($source);
  94.  
  95. $track = propre($shoutcast->{'songtitle'});
  96.  
  97. elseif($_GET['type'] == "/live"): // ICECAST
  98.  
  99. $source = file_get_contents($url.'/status-json.xsl');
  100. $icecast = json_decode($source);
  101.  
  102. if (!$data = file_get_contents($url.'/status-json.xsl')) {
  103. $error = error_get_last();
  104. echo "HTTP request failed. Error was: " . $error['message'];
  105. }
  106. $track = propre($icecast->{'icestats'}->{'source'}->{'title'});
  107. else:
  108. $track = propre($icecast->{'icestats'}->{'source'}[0]->{'title'});
  109. endif;
  110.  
  111.  
  112.  
  113. endif;
  114.  
  115.  
  116. $recherche = search($track);
  117.  
  118. $image = $pochette_img;
  119.  
  120. //print_r($recherche['tracks']);
  121. //$image=$recherche['results']['tracks']['items'][0]['album']['images'][0]['url'];
  122. if(isset($recherche['tracks']['items'][0]['album']['images'][0]['url'])){
  123. $image=$recherche['tracks']['items'][0]['album']['images'][0]['url'];
  124. }
  125.  
  126. $decoupe = explode(" - ", $track);
  127.  
  128. if(!$decoupe[0] or !$decoupe[1]):
  129.  
  130. $artist = $track;
  131.  
  132. endif;
  133.  
  134. $artist = limitString($decoupe[0]);
  135. $titre = limitString($decoupe[1]);
  136.  
  137. $tableau = ['track'=>$track, 'title'=>$titre, 'artist'=>$artist, 'picture'=>$image];
  138. $tableau_json = json_encode($tableau);
  139. echo($tableau_json);
  140.  
  141. endif;
  142. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement