PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/split/split.php

https://github.com/Allikin/SpotifyTools.php
PHP | 58 lines | 50 code | 7 blank | 1 comment | 12 complexity | 420accccb37f9a27a5583be4fb723ce1 MD5 | raw file
  1. <?php
  2. if ((isset($_POST['submit'])) && ($_POST['tracks'] > '')) {
  3. $correct_format = true;
  4. $message = '';
  5. $split = 1000;
  6. $count = 0;
  7. $tracks = $_POST['tracks'];
  8. if (correctTracksFormat($tracks)) {
  9. $list = preg_split('/[\n\r]+/',$tracks);
  10. $list = array_filter($list,'strlen');
  11. $list = tracksGetSpotifyURIs($list); // returns an array of Spotif URIs: spotify:track:<URI>
  12. } else {
  13. $correct_format = false;
  14. $messages[] = "<p style='font-size:12px;'>&nbsp;<br/>Please apply valid tracks by HTTP or Spotify URI.</p>";
  15. }
  16. if ($_POST['split'] > 0) {
  17. $split = $_POST['split'];
  18. }
  19. if ($correct_format) {
  20. if (isset($_POST['random'])) {
  21. shuffle($list); //randomize the list of tracks
  22. }
  23. // split list
  24. $number_of_tracks = count($list);
  25. foreach ($list as $uri) {
  26. $tracklist[$count] = $uri;
  27. $count++;
  28. if (($count == $split) || ($count == $number_of_tracks)){
  29. $number_of_tracks = $number_of_tracks - $count;
  30. $lists[] = $tracklist;
  31. unset($tracklist);
  32. $count = 0;
  33. }
  34. }
  35. echo "<h3>".count($list)." tracks split up into ".count($lists)." lists:</h3><br/>
  36. 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.<br/>&nbsp;<br/></p>";
  37. foreach ($lists as $i => $newlist) {
  38. echo "List ".($i+1).": ".count($newlist)." tracks:
  39. <textarea name='randomlist' rows='10' cols='52' wrap='off' onClick='select(this)' wrap='off' readonly>";
  40. foreach ($newlist as $uri) {
  41. echo $uri."\r\n";
  42. }
  43. echo "</textarea><br/><br/>";
  44. }
  45. } else {
  46. foreach ($messages as $message) {
  47. echo $message;
  48. }
  49. }
  50. }
  51. ?>