ImportPlaylists.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 mediaSources = [];
  90. if (!importMusarePlaylistFileContents.value)
  91. return new Toast("Please choose a Musare playlist file first.");
  92. if (importMusarePlaylistFileContents.value.playlist) {
  93. mediaSources =
  94. importMusarePlaylistFileContents.value.playlist.songs.map(
  95. song => `youtube:${song.youtubeId}`
  96. );
  97. } else if (importMusarePlaylistFileContents.value.songs) {
  98. mediaSources = importMusarePlaylistFileContents.value.songs.map(
  99. song => `youtube:${song.youtubeId}`
  100. );
  101. }
  102. if (mediaSources.length === 0) return new Toast("No songs to import.");
  103. return socket.dispatch(
  104. "playlists.addSongsToPlaylist",
  105. playlist.value._id,
  106. mediaSources,
  107. {
  108. cb: res => {
  109. new Toast(res.message);
  110. },
  111. onProgress: res => {
  112. if (res.status === "started") {
  113. id = res.id;
  114. title = res.title;
  115. }
  116. if (id)
  117. setJob({
  118. id,
  119. name: title,
  120. ...res
  121. });
  122. }
  123. }
  124. );
  125. };
  126. </script>
  127. <template>
  128. <div class="youtube-tab section">
  129. <label class="label"> Import songs from YouTube playlist </label>
  130. <div class="control is-grouped input-with-button">
  131. <p class="control is-expanded">
  132. <input
  133. class="input"
  134. type="text"
  135. placeholder="Enter YouTube Playlist URL here..."
  136. v-model="youtubeSearch.playlist.query"
  137. @keyup.enter="importYoutubePlaylist()"
  138. />
  139. </p>
  140. <p class="control has-addons">
  141. <span class="select" id="playlist-import-type">
  142. <select
  143. v-model="youtubeSearch.playlist.isImportingOnlyMusic"
  144. >
  145. <option :value="false">Import all</option>
  146. <option :value="true">Import only music</option>
  147. </select>
  148. </span>
  149. <button
  150. class="button is-info"
  151. @click.prevent="importYoutubePlaylist()"
  152. >
  153. <i class="material-icons icon-with-button">publish</i>Import
  154. </button>
  155. </p>
  156. </div>
  157. <label class="label"> Import songs from a Musare playlist file </label>
  158. <div class="control is-grouped input-with-button">
  159. <p class="control is-expanded">
  160. <input
  161. class="input"
  162. type="file"
  163. placeholder="Enter YouTube Playlist URL here..."
  164. @change="onMusarePlaylistFileChange"
  165. ref="importMusarePlaylistFileInput"
  166. @keyup.enter="importMusarePlaylistFile()"
  167. />
  168. </p>
  169. <p class="control">
  170. <button
  171. class="button is-info"
  172. @click.prevent="importMusarePlaylistFile()"
  173. >
  174. <i class="material-icons icon-with-button">publish</i>Import
  175. </button>
  176. </p>
  177. </div>
  178. </div>
  179. </template>
  180. <style lang="less" scoped>
  181. #playlist-import-type select {
  182. border-radius: 0;
  183. }
  184. input[type="file"] {
  185. padding-left: 0;
  186. }
  187. input[type="file"]::file-selector-button {
  188. background: var(--light-grey);
  189. border: none;
  190. height: 100%;
  191. border-right: 1px solid var(--light-grey-3);
  192. margin-right: 8px;
  193. padding: 0 8px;
  194. cursor: pointer;
  195. }
  196. input[type="file"]::file-selector-button:hover {
  197. background: var(--light-grey-2);
  198. }
  199. @media screen and (max-width: 1300px) {
  200. .youtube-tab #song-query-results,
  201. .section {
  202. max-width: 100% !important;
  203. }
  204. }
  205. </style>