Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 KB | None | 0 0
  1. var allsongs = []
  2. var outText = "";
  3. var songsToText = function(style, csv, likedonly){
  4. if (style === undefined){
  5. console.log("style is undefined.");
  6. return;
  7. }
  8. var csv = csv || false; // defaults to false
  9. var likedonly = likedonly || false; // defaults to false
  10. if (likedonly) {
  11. console.log("Only selecting liked songs");
  12. }
  13. if (style == "all" && !csv){
  14. console.log("Duration, ratings, and playcount will only be exported with the CSV flag");
  15. }
  16. outText = "";
  17. if (csv) {
  18. if (style == "all") {
  19. //extra line
  20. outText = "artist,album,title,duration,playcount,rating,rating_interpretation" + "\n";
  21. } else if (style == "artist") {
  22. } else if (style == "artistsong") {
  23. } else if (style == "artistalbum") {
  24. } else if (style == "artistalbumsong") {
  25. } else {
  26. console.log("style not defined");
  27. }
  28. }
  29. var numEntries = 0;
  30. var seen = {};
  31. for (var i = 0; i < allsongs.length; i++) {
  32. var curr = "";
  33. var properTitle = allsongs[i].title.replace(/[\n\r!]/g, '').trim();
  34. if (!likedonly || (likedonly && allsongs[i].rating >= 5)){
  35. if (csv) {
  36. if (style == "all") {
  37. //extra line
  38. curr += '"' + allsongs[i].artist.replace(/"/g, '""').trim() + '"' + ",";
  39. curr += '"' + allsongs[i].album.replace(/"/g, '""').trim() + '"' + ",";
  40. curr += '"' + properTitle.replace(/"/g, '""').trim() + '"' + ",";
  41. curr += '"' + allsongs[i].duration.replace(/"/g, '""').trim() + '"' + ",";
  42. curr += '"' + allsongs[i].playcount.replace(/"/g, '""').trim() + '"' + ",";
  43. curr += '"' + allsongs[i].rating.replace(/"/g, '""').trim() + '"' + ",";
  44. curr += '"' + allsongs[i].rating_interpretation.replace(/"/g, '""').trim() + '"';
  45. } else if (style == "artist") {
  46. curr += '"' + allsongs[i].artist.replace(/"/g, '""').trim() + '"';
  47. } else if (style == "artistsong") {
  48. curr += '"' + allsongs[i].artist.replace(/"/g, '""').trim() + '"' + ",";
  49. curr += '"' + properTitle.replace(/"/g, '""').trim() + '"';
  50. } else if (style == "artistalbum") {
  51. curr += '"' + allsongs[i].artist.replace(/"/g, '""').trim() + '"' + ",";
  52. curr += '"' + allsongs[i].album.replace(/"/g, '""').trim() + '"';
  53. } else if (style == "artistalbumsong") {
  54. curr += '"' + allsongs[i].artist.replace(/"/g, '""').trim() + '"' + ",";
  55. curr += '"' + allsongs[i].album.replace(/"/g, '""').trim() + '"' + ",";
  56. curr += '"' + properTitle.replace(/"/g, '""').trim() + '"';
  57. } else {
  58. console.log("style not defined");
  59. }
  60. } else {
  61. if (style == "all"){
  62. curr = allsongs[i].artist + " - " + allsongs[i].album + " - " + properTitle + " [[playcount: " + allsongs[i].playcount + ", rating: " + allsongs[i].rating_interpretation + "]]" ;
  63. } else if (style == "artist"){
  64. curr = allsongs[i].artist;
  65. } else if (style == "artistalbum"){
  66. curr = allsongs[i].artist + " - " + allsongs[i].album;
  67. } else if (style == "artistsong"){
  68. curr = allsongs[i].artist + " - " + properTitle;
  69. } else if (style == "artistalbumsong"){
  70. curr = allsongs[i].artist + " - " + allsongs[i].album + " - " + properTitle;
  71. } else {
  72. console.log("style not defined");
  73. }
  74. }
  75. if (!seen.hasOwnProperty(curr)){ // hashset
  76. outText = outText + curr + "\n";
  77. numEntries++;
  78. seen[curr] = true;
  79. } else {
  80. //console.log("Skipping (duplicate) " + curr);
  81. }
  82. }
  83. }
  84. console.log("=============================================================");
  85. console.log(outText);
  86. console.log("=============================================================");
  87. try {
  88. copy(outText);
  89. console.log("copy(outText) to clipboard succeeded.");
  90. } catch (e) {
  91. console.log(e);
  92. console.log("copy(outText) to clipboard failed, please type copy(outText) on the console or copy the log output above.");
  93. }
  94. console.log("Done! " + numEntries + " lines in output. Used " + numEntries + " unique entries out of " + allsongs.length + ".");
  95. };
  96. var scrapeSongs = function(){
  97. var intervalms = 1; //in ms
  98. var timeoutms = 3000; //in ms
  99. var retries = timeoutms / intervalms;
  100. var total = [];
  101. var seen = {};
  102. var topId = "";
  103. document.querySelector("#mainContainer").scrollTop = 0; //scroll to top
  104. var interval = setInterval(function(){
  105. var songs = document.querySelectorAll("table.song-table tbody tr.song-row");
  106. if (songs.length > 0) {
  107. // detect order
  108. var colNames = {
  109. index: -1,
  110. title: -1,
  111. duration: -1,
  112. artist: -1,
  113. album: -1,
  114. playcount: -1,
  115. rating: -1
  116. };
  117. for (var i = 0; i < songs[0].childNodes.length; i++) {
  118. colNames.index = songs[0].childNodes[i].getAttribute("data-col") == "index" ? i : colNames.index;
  119. colNames.title = songs[0].childNodes[i].getAttribute("data-col") == "title" ? i : colNames.title;
  120. colNames.duration = songs[0].childNodes[i].getAttribute("data-col") == "duration" ? i : colNames.duration;
  121. colNames.artist = songs[0].childNodes[i].getAttribute("data-col") == "artist" ? i : colNames.artist;
  122. colNames.album = songs[0].childNodes[i].getAttribute("data-col") == "album" ? i : colNames.album;
  123. colNames.playcount = songs[0].childNodes[i].getAttribute("data-col") == "play-count" ? i : colNames.playcount;
  124. colNames.rating = songs[0].childNodes[i].getAttribute("data-col") == "rating" ? i : colNames.rating;
  125. }
  126. // check if page has updated/scrolled
  127. var currId = songs[0].getAttribute("data-id");
  128. if (currId == topId){ // page has not yet changed
  129. retries--;
  130. scrollDiv = document.querySelector("#mainContainer");
  131. isAtBottom = scrollDiv.scrollTop == (scrollDiv.scrollHeight - scrollDiv.offsetHeight)
  132. if (isAtBottom || retries <= 0) {
  133. clearInterval(interval); //done
  134. allsongs = total;
  135. console.log("Got " + total.length + " songs and stored them in the allsongs variable.");
  136. console.log("Calling songsToText with style all, csv flag true, likedonly false: songsToText(\"all\", false).");
  137. songsToText("artistalbumsong", false, false);
  138. }
  139. } else {
  140. retries = timeoutms / intervalms;
  141. topId = currId;
  142. // read page
  143. for (var i = 0; i < songs.length; i++) {
  144. var curr = {
  145. dataid: songs[i].getAttribute("data-id"),
  146. index: (colNames.index != -1 ? songs[i].childNodes[colNames.index].textContent : ""),
  147. title: (colNames.title != -1 ? songs[i].childNodes[colNames.title].textContent : ""),
  148. duration: (colNames.duration != -1 ? songs[i].childNodes[colNames.duration].textContent : ""),
  149. artist: (colNames.artist != -1 ? songs[i].childNodes[colNames.artist].textContent : ""),
  150. album: (colNames.album != -1 ? songs[i].childNodes[colNames.album].textContent : ""),
  151. playcount: (colNames.playcount != -1 ? songs[i].childNodes[colNames.playcount].textContent : ""),
  152. rating: (colNames.rating != -1 ? songs[i].childNodes[colNames.rating].getAttribute("data-rating") : ""),
  153. rating_interpretation: "",
  154. }
  155. if(curr.rating == "undefined") {
  156. curr.rating_interpretation = "never-rated"
  157. }
  158. if(curr.rating == "0") {
  159. curr.rating_interpretation = "not-rated"
  160. }
  161. if(curr.rating == "1") {
  162. curr.rating_interpretation = "thumbs-down"
  163. }
  164. if(curr.rating == "5") {
  165. curr.rating_interpretation = "thumbs-up"
  166. }
  167. if (!seen.hasOwnProperty(curr.dataid)){ // hashset
  168. total.push(curr);
  169. seen[curr.dataid] = true;
  170. }
  171. }
  172. songs[songs.length-1].scrollIntoView(true); // go to next page
  173. }
  174. }
  175. }, intervalms);
  176. };
  177. scrapeSongs();
  178. // for the full CSV version you can now call songsToText("all", true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement