SearchYoutube.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <script>
  2. import Toast from "toasters";
  3. export default {
  4. data() {
  5. return {
  6. youtubeSearch: {
  7. songs: {
  8. results: [],
  9. query: "",
  10. nextPageToken: ""
  11. },
  12. playlist: {
  13. query: "",
  14. isImportingOnlyMusic: true
  15. }
  16. }
  17. };
  18. },
  19. methods: {
  20. searchForSongs() {
  21. let { query } = this.youtubeSearch.songs;
  22. if (query.indexOf("&index=") !== -1) {
  23. query = query.split("&index=");
  24. query.pop();
  25. query = query.join("");
  26. }
  27. if (query.indexOf("&list=") !== -1) {
  28. query = query.split("&list=");
  29. query.pop();
  30. query = query.join("");
  31. }
  32. this.socket.dispatch("apis.searchYoutube", query, res => {
  33. if (res.status === "success") {
  34. this.youtubeSearch.songs.nextPageToken =
  35. res.data.nextPageToken;
  36. this.youtubeSearch.songs.results = [];
  37. res.data.items.forEach(result => {
  38. this.youtubeSearch.songs.results.push({
  39. id: result.id.videoId,
  40. url: `https://www.youtube.com/watch?v=${this.id}`,
  41. title: result.snippet.title,
  42. thumbnail: result.snippet.thumbnails.default.url,
  43. channelId: result.snippet.channelId,
  44. channelTitle: result.snippet.channelTitle,
  45. isAddedToQueue: false
  46. });
  47. });
  48. } else if (res.status === "error") new Toast(res.message);
  49. });
  50. },
  51. loadMoreSongs() {
  52. this.socket.dispatch(
  53. "apis.searchYoutubeForPage",
  54. this.youtubeSearch.songs.query,
  55. this.youtubeSearch.songs.nextPageToken,
  56. res => {
  57. if (res.status === "success") {
  58. this.youtubeSearch.songs.nextPageToken =
  59. res.data.nextPageToken;
  60. res.data.items.forEach(result => {
  61. this.youtubeSearch.songs.results.push({
  62. id: result.id.videoId,
  63. url: `https://www.youtube.com/watch?v=${this.id}`,
  64. title: result.snippet.title,
  65. thumbnail:
  66. result.snippet.thumbnails.default.url,
  67. channelId: result.snippet.channelId,
  68. channelTitle: result.snippet.channelTitle,
  69. isAddedToQueue: false
  70. });
  71. });
  72. } else if (res.status === "error") new Toast(res.message);
  73. }
  74. );
  75. },
  76. addSongToPlaylist(id, index) {
  77. this.socket.dispatch(
  78. "playlists.addSongToPlaylist",
  79. false,
  80. id,
  81. this.playlist._id,
  82. res => {
  83. new Toast(res.message);
  84. if (res.status === "success")
  85. this.youtubeSearch.songs.results[
  86. index
  87. ].isAddedToQueue = true;
  88. }
  89. );
  90. }
  91. }
  92. };
  93. </script>