Advertisement
Guest User

Untitled

a guest
Feb 13th, 2014
2,340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php
  2. // Account & API Account Information
  3. $user = "JordanC26"; // <---- Your username goes here
  4. $key = "HIDDEN"; //<-- Your API key goes here
  5.  
  6. // The URL of the request to API Service
  7. $url = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=$user&api_key=$key&format=json";
  8.  
  9. echo ($url);
  10.  
  11. // Enable Shortening
  12. $short_titles = false;
  13.  
  14. // Setup cURL for request
  15. $ch = curl_init( $url );
  16. $options = array(
  17.     CURLOPT_RETURNTRANSFER => true
  18.     );
  19. curl_setopt_array($ch, $options);
  20.  
  21. // Execute cURL
  22. $json = curl_exec($ch);
  23. curl_close($ch);
  24. $data = json_decode($json,true);
  25.  
  26. // Get only the latest track information
  27. $last = $data['recenttracks']['track'][0];
  28.  
  29. // Is now playing attribute present?
  30. if (isset($last['@attr']['nowplaying'])) {
  31.  
  32.     $output = "I am currently listening to " . $last['name'] . ' by ' . $last['artist']['#text'] . " on Spotify.";
  33.  
  34.     if ($short_titles) {
  35.         $output = sTrim($output);
  36.     }
  37.  
  38.     echo trim($output);
  39. } else {
  40.     $played = $last['date']['uts']; $now = time();
  41.     $diff = abs($now - $played);
  42.  
  43.     // Time formatting
  44.     $hours = intval(intval($diff) / 3600);
  45.     $minutes = intval(($diff / 60) % 60);
  46.  
  47.     // String formatting based on time
  48.     if (!empty($hours)) {
  49.         if ($hours > 24)
  50.             $time = "over a day ago";
  51.         else
  52.             $time = $hours . " hours and " . $minutes . " minutes ago";
  53.     } else
  54.         $time = $minutes . " minutes ago";
  55.  
  56.     $output = "I last listened to " . $last['name'] . ' by ' . $last['artist']['#text'] . " $time on Spotify";
  57.  
  58.     if ($short_titles) {
  59.         $output = sTrim($output);
  60.     }
  61.  
  62.     echo trim($output);
  63. }
  64.  
  65. exit();
  66.  
  67. function sTrim($output) {
  68.     $output = preg_replace("/- Live/i", "", $output);
  69.     $output = preg_replace("/- Album Version Explicit/i", "", $output);
  70.     $output = preg_replace("/- Explicit Album Version/i", "", $output);
  71.     $output = preg_replace("/- Explicit Version/i", "", $output);
  72.     $output = preg_replace("/- Album Version/i", "", $output);
  73.     $output = preg_replace("/\\[(.*?)\]/i", "", $output);
  74.     $output = preg_replace("/\\((.*?)\)/i", "", $output);
  75.     return $output;
  76. }
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement