ImportPlaylists.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <script setup lang="ts">
  2. import Toast from "toasters";
  3. import { storeToRefs } from "pinia";
  4. import { ref } from "vue";
  5. import { useSearchYoutube } from "@/composables/useSearchYoutube";
  6. import { useWebsocketsStore } from "@/stores/websockets";
  7. import { useLongJobsStore } from "@/stores/longJobs";
  8. import { useEditPlaylistStore } from "@/stores/editPlaylist";
  9. const props = defineProps({
  10. modalUuid: { type: String, required: true }
  11. });
  12. const { socket } = useWebsocketsStore();
  13. const editPlaylistStore = useEditPlaylistStore({ modalUuid: props.modalUuid });
  14. const { playlist } = storeToRefs(editPlaylistStore);
  15. const { setJob } = useLongJobsStore();
  16. const { youtubeSearch } = useSearchYoutube();
  17. const importMusarePlaylistFileInput = ref();
  18. const importMusarePlaylistFileContents = ref(null);
  19. const importYoutubePlaylist = () => {
  20. let id;
  21. let title;
  22. // import query is blank
  23. if (!youtubeSearch.value.playlist.query)
  24. return new Toast("Please enter a YouTube playlist URL.");
  25. const playlistRegex = /[\\?&]list=([^&#]*)/;
  26. const channelRegex =
  27. /\.[\w]+\/(?:(?:channel\/(UC[0-9A-Za-z_-]{21}[AQgw]))|(?:user\/?([\w-]+))|(?:c\/?([\w-]+))|(?:\/?([\w-]+)))/;
  28. if (
  29. !playlistRegex.exec(youtubeSearch.value.playlist.query) &&
  30. !channelRegex.exec(youtubeSearch.value.playlist.query)
  31. ) {
  32. return new Toast({
  33. content: "Please enter a valid YouTube playlist URL.",
  34. timeout: 4000
  35. });
  36. }
  37. return socket.dispatch(
  38. "playlists.addSetToPlaylist",
  39. youtubeSearch.value.playlist.query,
  40. playlist.value._id,
  41. youtubeSearch.value.playlist.isImportingOnlyMusic,
  42. {
  43. cb: () => {},
  44. onProgress: res => {
  45. if (res.status === "started") {
  46. id = res.id;
  47. title = res.title;
  48. }
  49. if (id)
  50. setJob({
  51. id,
  52. name: title,
  53. ...res
  54. });
  55. }
  56. }
  57. );
  58. };
  59. const onMusarePlaylistFileChange = () => {
  60. const reader = new FileReader();
  61. const fileInput = importMusarePlaylistFileInput.value as HTMLInputElement;
  62. const file = fileInput.files.item(0);
  63. reader.readAsText(file, "UTF-8");
  64. reader.onload = ({ target }) => {
  65. const { result } = target;
  66. try {
  67. const parsed = JSON.parse(result.toString());
  68. if (!parsed)
  69. new Toast(
  70. "An error occured whilst parsing the playlist file. Is it valid?"
  71. );
  72. else importMusarePlaylistFileContents.value = parsed;
  73. } catch (err) {
  74. new Toast(
  75. "An error occured whilst parsing the playlist file. Is it valid?"
  76. );
  77. }
  78. };
  79. reader.onerror = evt => {
  80. console.log(evt);
  81. new Toast(
  82. "An error occured whilst reading the playlist file. Is it valid?"
  83. );
  84. };
  85. };
  86. const importMusarePlaylistFile = () => {
  87. let id;
  88. let title;
  89. let youtubeIds = [];
  90. if (!importMusarePlaylistFileContents.value)
  91. return new Toast("Please choose a Musare playlist file first.");
  92. if (importMusarePlaylistFileContents.value.playlist) {
  93. youtubeIds = importMusarePlaylistFileContents.value.playlist.songs.map(
  94. song => song.youtubeId
  95. );
  96. } else if (importMusarePlaylistFileContents.value.songs) {
  97. youtubeIds = importMusarePlaylistFileContents.value.songs.map(
  98. song => song.youtubeId
  99. );
  100. }
  101. if (youtubeIds.length === 0) return new Toast("No songs to import.");
  102. return socket.dispatch(
  103. "playlists.addSongsToPlaylist",
  104. playlist.value._id,
  105. youtubeIds,
  106. {
  107. cb: res => {
  108. new Toast(res.message);
  109. },
  110. onProgress: res => {
  111. if (res.status === "started") {
  112. id = res.id;
  113. title = res.title;
  114. }
  115. if (id)
  116. setJob({
  117. id,
  118. name: title,
  119. ...res
  120. });
  121. }
  122. }
  123. );
  124. };
  125. </script>
  126. <template>
  127. <div class="youtube-tab section">
  128. <label class="label"> Import songs from YouTube playlist </label>
  129. <div class="control is-grouped input-with-button">
  130. <p class="control is-expanded">
  131. <input
  132. class="input"
  133. type="text"
  134. placeholder="Enter YouTube Playlist URL here..."
  135. v-model="youtubeSearch.playlist.query"
  136. @keyup.enter="importYoutubePlaylist()"
  137. />
  138. </p>
  139. <p class="control has-addons">
  140. <span class="select" id="playlist-import-type">
  141. <select
  142. v-model="youtubeSearch.playlist.isImportingOnlyMusic"
  143. >
  144. <option :value="false">Import all</option>
  145. <option :value="true">Import only music</option>
  146. </select>
  147. </span>
  148. <button
  149. class="button is-info"
  150. @click.prevent="importYoutubePlaylist()"
  151. >
  152. <i class="material-icons icon-with-button">publish</i>Import
  153. </button>
  154. </p>
  155. </div>
  156. <label class="label"> Import songs from a Musare playlist file </label>
  157. <div class="control is-grouped input-with-button">
  158. <p class="control is-expanded">
  159. <input
  160. class="input"
  161. type="file"
  162. placeholder="Enter YouTube Playlist URL here..."
  163. @change="onMusarePlaylistFileChange"
  164. ref="importMusarePlaylistFileInput"
  165. @keyup.enter="importMusarePlaylistFile()"
  166. />
  167. </p>
  168. <p class="control">
  169. <button
  170. class="button is-info"
  171. @click.prevent="importMusarePlaylistFile()"
  172. >
  173. <i class="material-icons icon-with-button">publish</i>Import
  174. </button>
  175. </p>
  176. </div>
  177. </div>
  178. </template>
  179. <style lang="less" scoped>
  180. #playlist-import-type select {
  181. border-radius: 0;
  182. }
  183. input[type="file"] {
  184. padding-left: 0;
  185. }
  186. input[type="file"]::file-selector-button {
  187. background: var(--light-grey);
  188. border: none;
  189. height: 100%;
  190. border-right: 1px solid var(--light-grey-3);
  191. margin-right: 8px;
  192. padding: 0 8px;
  193. cursor: pointer;
  194. }
  195. input[type="file"]::file-selector-button:hover {
  196. background: var(--light-grey-2);
  197. }
  198. @media screen and (max-width: 1300px) {
  199. .youtube-tab #song-query-results,
  200. .section {
  201. max-width: 100% !important;
  202. }
  203. }
  204. </style>