useSearchYoutube.ts 2.6 KB

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