PageRenderTime 108ms CodeModel.GetById 32ms RepoModel.GetById 3ms app.codeStats 0ms

/dupes.php

https://github.com/Allikin/SpotifyTools.php
PHP | 145 lines | 138 code | 5 blank | 2 comment | 20 complexity | a17eeee90f765737324d5a5db59087e4 MD5 | raw file
  1. <?php
  2. if (($_POST['playlist'] > '') || ($_POST['tracks'] > '')) { // checks that at least one field is filled out
  3. $correct_format = false;
  4. $starred = false;
  5. $playlist_empty = true;
  6. $message = '';
  7. $playlist = Array(
  8. 'Name' => "",
  9. 'Tracks' => Array(
  10. 'Names' => Array(""),
  11. 'Addresses' => Array(""),
  12. 'Duration' => Array(0)
  13. )
  14. );
  15. if ($_POST['playlist'] > '') {
  16. $pl_input = trim($_POST['playlist']);
  17. if (correctPlaylistFormat($pl_input)) {
  18. $playlist_empty = false;
  19. $correct_format = true;
  20. $playlist_uri = getPlaylistURI($pl_input); // returns a Spotify playlist URI: spotify:user:<user>:playlist:<uri>
  21. $playlist = playlistGetSpotifyURIs($playlist_uri); // get the list of names, addresses (URIs) and duration
  22. if (count($playlist['Tracks']['Addresses']) > 1000) {
  23. echo '<div class="warning">The playlist option (top) will only accept 1000 tracks from your playlist. If your list has more than 1000 tracks, please use the bottom option.<br/><br/>
  24. Tip: Alternativly use the <a href="split/">split up</a> feature to split up your playlist into lists of maximum 1000 tracks (make sure you sort your playlist alphabetically first).</div><br/>';
  25. }
  26. }
  27. } elseif ($_POST['tracks'] > '') {
  28. $tracks = $_POST['tracks'];
  29. if (correctTracksFormat($tracks)) {
  30. $correct_format = true;
  31. $list = preg_split('/[\n\r]+/',$tracks); // get rid of
  32. $list = array_filter($list,'strlen'); // garbage
  33. $playlist['Tracks']['Addresses'] = tracksGetSpotifyURIs($list); // returns an array of Spotif URIs: spotify:track:<URI>
  34. }
  35. }
  36. if ($correct_format) {
  37. if ($tracks = findDupes($playlist['Tracks'])) { // returns false if there are no dupes, else returns the dupes
  38. $new_playlist = removeDupes($playlist['Tracks']); // create a new playlist without dupes
  39. if ($playlist['Name'] > "") {
  40. echo "<h3>Results for '".$playlist['Name']."'</h3><br/>";
  41. } else {
  42. echo "<h3>Results</h3><br/>";
  43. }
  44. $count_removed = count($playlist['Tracks']['Addresses'])- count($new_playlist['Addresses']); // number of tracks removed from new playlist
  45. if ($count_removed > 0) {
  46. echo "<b>New playlist (".count($new_playlist['Addresses'])." tracks - ".$count_removed." duplicates removed):</b></p>
  47. <textarea name='newlist' rows='10' cols='52' onClick='select(this)' wrap='off' readonly>";
  48. // List the new playlist..
  49. foreach ($new_playlist['Addresses'] as $uri) {
  50. echo $uri."\r\n";
  51. }
  52. echo "</textarea><br/>
  53. <p>
  54. <i>Select the tracks by clicking inside the textarea and use CTRL-C or right click to copy, or just drag them to an empty playlist. - Will reset the tracks' added date.</i><br/>&nbsp;<br/>
  55. </p>";
  56. // ..unless no dupes were removed
  57. } //if ($count_removed > 0)
  58. else {
  59. echo "<br/>No duplicates removed; no new playlist generated.<br/><br/>";
  60. }
  61. echo "<p><br/><b>Duplicates as tracks (".count($tracks['Addresses'])." tracks):</b></p>
  62. <textarea name='newlist' rows='6' cols='52' onClick='select(this)' wrap='off' readonly>";
  63. // List the dupes by track USI
  64. foreach ($tracks['Addresses'] as $uri) {
  65. echo $uri."\r\n";
  66. }
  67. echo "</textarea><br/>
  68. <p>
  69. <i>Select the tracks by clicking inside the textarea and use CTRL-C or right click to copy, or just drag them to an empty playlist.</i>
  70. <br/>&nbsp;<br/>
  71. </p>";
  72. // embed or ws.spotify.com down? If so, let the user know
  73. if (isset($tracks['Names']) && preg_match('/<i>/',$tracks['Names'][0])) {
  74. $message = $tracks['Names'][0].'<br/><br/>';
  75. } else {
  76. if ($playlist_empty) {
  77. $tracks = tracksGetTrackNames($tracks); // gets the track names by looking up the URIs against the API
  78. }
  79. $rows = count($tracks['Addresses'])+1;
  80. echo "<p>&nbsp;<br/>
  81. <b>Duplicates by track name and artist (".count($tracks['Addresses'])." tracks):</b><br/><br/>
  82. <textarea name='newlist' rows='".$rows."' cols='52' wrap='off' readonly>";
  83. /*
  84. if ($testSocket = @fsockopen("ws.spotify.com", 80, $errno, $errstr, 2)) {
  85. foreach ($tracks['Addresses'] as $i => $dupe) {
  86. if(preg_match('/:local:/', $dupe)) {
  87. $tracks['Names'][$i] = getLocalTrackName($dupe);
  88. } else {
  89. $url = trim("http://ws.spotify.com/lookup/1/?uri=".$dupe);
  90. $content = file_get_contents($url);
  91. if ($content) {
  92. $doc = new SimpleXmlElement($content);
  93. echo $doc->name." by ".$doc->artist->name.'&#13;';
  94. } else {
  95. echo "Error looking up in the Spotify API";
  96. }
  97. }
  98. }
  99. fclose($testSocket);
  100. } else {
  101. $tracks['Names'][0] = '&nbsp;<br/><i>Unfortunately it\'s not possible to lookup tracknames at this time (Spotify site down), please try again later.</i>';
  102. $tracks['Addresses'][0] = '999';
  103. }
  104. */
  105. // List the dupes by name and artist
  106. if (!isset($tracks['Names'])) {
  107. echo "Something happened looking up the track names. \r\n
  108. Try refreshing the page.";
  109. } else {
  110. foreach ($tracks['Names'] as $name) {
  111. if (preg_match('/\* /',$name)) { // check if the track is starred
  112. $starredTracks[] = $name;
  113. $starred = true; // ah, need to display special message later
  114. } else{
  115. echo $name."\n";
  116. }
  117. }
  118. if ($starred) {
  119. echo "\nPossible dupes (not removed from the new playlist):\n";
  120. foreach ($starredTracks as $name) {
  121. echo $name."\n";
  122. }
  123. }
  124. }
  125. echo "</textarea><br/><br/>".$message; // display error message (I should make this an array *notes*)
  126. echo "<br/><i>Sort your playlist alphabetically or use the filter function (ctrl-f) to find the tracks easily.</i><br/><br/>
  127. </p>";
  128. }
  129. } else {
  130. $message = "<p>&nbsp;<br/>No duplicates found</p>";
  131. }
  132. } else {
  133. $message = "<p>&nbsp;<br/>Please apply valid playlist (top field) or tracks (bottom field) by HTTP or Spotify URI.</p>";
  134. }
  135. echo $message;
  136. }
  137. ?>