ImportPlaylists.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 { useSearchSoundcloud } from "@/composables/useSearchSoundcloud";
  7. import { useSearchSpotify } from "@/composables/useSearchSpotify";
  8. import { useWebsocketsStore } from "@/stores/websockets";
  9. import { useConfigStore } from "@/stores/config";
  10. import { useLongJobsStore } from "@/stores/longJobs";
  11. import { useEditPlaylistStore } from "@/stores/editPlaylist";
  12. const props = defineProps({
  13. modalUuid: { type: String, required: true }
  14. });
  15. const { socket } = useWebsocketsStore();
  16. const configStore = useConfigStore();
  17. const { experimental } = storeToRefs(configStore);
  18. const editPlaylistStore = useEditPlaylistStore({ modalUuid: props.modalUuid });
  19. const { playlist } = storeToRefs(editPlaylistStore);
  20. const { setJob } = useLongJobsStore();
  21. const { youtubeSearch } = useSearchYoutube();
  22. const { soundcloudSearch } = useSearchSoundcloud();
  23. const { spotifySearch } = useSearchSpotify();
  24. const importMusarePlaylistFileInput = ref();
  25. const importMusarePlaylistFileContents = ref(null);
  26. const importYoutubePlaylist = () => {
  27. let id;
  28. let title;
  29. // import query is blank
  30. if (!youtubeSearch.value.playlist.query)
  31. return new Toast("Please enter a YouTube playlist URL.");
  32. const playlistRegex = /[\\?&]list=([^&#]*)/;
  33. const channelRegex =
  34. /\.[\w]+\/(?:(?:channel\/(UC[0-9A-Za-z_-]{21}[AQgw]))|(?:user\/?([\w-]+))|(?:c\/?([\w-]+))|(?:\/?([\w-]+)))/;
  35. if (
  36. !playlistRegex.exec(youtubeSearch.value.playlist.query) &&
  37. !channelRegex.exec(youtubeSearch.value.playlist.query)
  38. ) {
  39. return new Toast({
  40. content: "Please enter a valid YouTube playlist URL.",
  41. timeout: 4000
  42. });
  43. }
  44. return socket.dispatch(
  45. "playlists.addYoutubeSetToPlaylist",
  46. youtubeSearch.value.playlist.query,
  47. playlist.value._id,
  48. youtubeSearch.value.playlist.isImportingOnlyMusic,
  49. {
  50. cb: () => {},
  51. onProgress: res => {
  52. if (res.status === "started") {
  53. id = res.id;
  54. title = res.title;
  55. }
  56. if (id)
  57. setJob({
  58. id,
  59. name: title,
  60. ...res
  61. });
  62. }
  63. }
  64. );
  65. };
  66. const importSoundcloudPlaylist = () => {
  67. let id;
  68. let title;
  69. // import query is blank
  70. if (!soundcloudSearch.value.playlist.query)
  71. return new Toast("Please enter a SoundCloud playlist URL.");
  72. return socket.dispatch(
  73. "playlists.addSoundcloudSetToPlaylist",
  74. soundcloudSearch.value.playlist.query,
  75. playlist.value._id,
  76. {
  77. cb: () => {},
  78. onProgress: res => {
  79. if (res.status === "started") {
  80. id = res.id;
  81. title = res.title;
  82. }
  83. if (id)
  84. setJob({
  85. id,
  86. name: title,
  87. ...res
  88. });
  89. }
  90. }
  91. );
  92. };
  93. const importSpotifyPlaylist = () => {
  94. let id;
  95. let title;
  96. // import query is blank
  97. if (!spotifySearch.value.playlist.query)
  98. return new Toast("Please enter a Spotify playlist URL.");
  99. return socket.dispatch(
  100. "playlists.addSpotifySetToPlaylist",
  101. spotifySearch.value.playlist.query,
  102. playlist.value._id,
  103. {
  104. cb: () => {},
  105. onProgress: res => {
  106. if (res.status === "started") {
  107. id = res.id;
  108. title = res.title;
  109. }
  110. if (id)
  111. setJob({
  112. id,
  113. name: title,
  114. ...res
  115. });
  116. }
  117. }
  118. );
  119. };
  120. const onMusarePlaylistFileChange = () => {
  121. const reader = new FileReader();
  122. const fileInput = importMusarePlaylistFileInput.value as HTMLInputElement;
  123. const file = fileInput.files.item(0);
  124. reader.readAsText(file, "UTF-8");
  125. reader.onload = ({ target }) => {
  126. const { result } = target;
  127. try {
  128. const parsed = JSON.parse(result.toString());
  129. if (!parsed)
  130. new Toast(
  131. "An error occured whilst parsing the playlist file. Is it valid?"
  132. );
  133. else importMusarePlaylistFileContents.value = parsed;
  134. } catch (err) {
  135. new Toast(
  136. "An error occured whilst parsing the playlist file. Is it valid?"
  137. );
  138. }
  139. };
  140. reader.onerror = evt => {
  141. console.log(evt);
  142. new Toast(
  143. "An error occured whilst reading the playlist file. Is it valid?"
  144. );
  145. };
  146. };
  147. const importMusarePlaylistFile = () => {
  148. let id;
  149. let title;
  150. let mediaSources = [];
  151. if (!importMusarePlaylistFileContents.value)
  152. return new Toast("Please choose a Musare playlist file first.");
  153. if (importMusarePlaylistFileContents.value.playlist) {
  154. mediaSources =
  155. importMusarePlaylistFileContents.value.playlist.songs.map(song =>
  156. song.youtubeId ? `youtube:${song.youtubeId}` : song.mediaSource
  157. );
  158. } else if (importMusarePlaylistFileContents.value.songs) {
  159. mediaSources = importMusarePlaylistFileContents.value.songs.map(song =>
  160. song.youtubeId ? `youtube:${song.youtubeId}` : song.mediaSource
  161. );
  162. }
  163. if (mediaSources.length === 0) return new Toast("No songs to import.");
  164. return socket.dispatch(
  165. "playlists.addSongsToPlaylist",
  166. playlist.value._id,
  167. mediaSources,
  168. {
  169. cb: res => {
  170. new Toast(res.message);
  171. },
  172. onProgress: res => {
  173. if (res.status === "started") {
  174. id = res.id;
  175. title = res.title;
  176. }
  177. if (id)
  178. setJob({
  179. id,
  180. name: title,
  181. ...res
  182. });
  183. }
  184. }
  185. );
  186. };
  187. </script>
  188. <template>
  189. <div class="import-playlist-tab section">
  190. <label class="label"> Import songs from YouTube playlist </label>
  191. <div class="control is-grouped input-with-button">
  192. <p class="control is-expanded">
  193. <input
  194. class="input"
  195. type="text"
  196. placeholder="Enter YouTube Playlist URL here..."
  197. v-model="youtubeSearch.playlist.query"
  198. @keyup.enter="importYoutubePlaylist()"
  199. />
  200. </p>
  201. <p class="control has-addons">
  202. <span class="select" id="playlist-import-type">
  203. <select
  204. v-model="youtubeSearch.playlist.isImportingOnlyMusic"
  205. >
  206. <option :value="false">Import all</option>
  207. <option :value="true">Import only music</option>
  208. </select>
  209. </span>
  210. <button
  211. class="button is-info"
  212. @click.prevent="importYoutubePlaylist()"
  213. >
  214. <i class="material-icons icon-with-button">publish</i>Import
  215. </button>
  216. </p>
  217. </div>
  218. <template v-if="experimental.soundcloud">
  219. <label class="label"> Import songs from SoundCloud playlist </label>
  220. <div class="control is-grouped input-with-button">
  221. <p class="control is-expanded">
  222. <input
  223. class="input"
  224. type="text"
  225. placeholder="Enter SoundCloud Playlist URL here..."
  226. v-model="soundcloudSearch.playlist.query"
  227. @keyup.enter="importSoundcloudPlaylist()"
  228. />
  229. </p>
  230. <p class="control has-addons">
  231. <button
  232. class="button is-info"
  233. @click.prevent="importSoundcloudPlaylist()"
  234. >
  235. <i class="material-icons icon-with-button">publish</i
  236. >Import
  237. </button>
  238. </p>
  239. </div>
  240. </template>
  241. <template v-if="experimental.spotify">
  242. <label class="label"> Import songs from Spotify playlist </label>
  243. <div class="control is-grouped input-with-button">
  244. <p class="control is-expanded">
  245. <input
  246. class="input"
  247. type="text"
  248. placeholder="Enter Spotify Playlist URL here..."
  249. v-model="spotifySearch.playlist.query"
  250. @keyup.enter="importSpotifyPlaylist()"
  251. />
  252. </p>
  253. <p class="control has-addons">
  254. <button
  255. class="button is-info"
  256. @click.prevent="importSpotifyPlaylist()"
  257. >
  258. <i class="material-icons icon-with-button">publish</i
  259. >Import
  260. </button>
  261. </p>
  262. </div>
  263. </template>
  264. <label class="label"> Import songs from a Musare playlist file </label>
  265. <div class="control is-grouped input-with-button">
  266. <p class="control is-expanded">
  267. <input
  268. class="input"
  269. type="file"
  270. placeholder="Enter YouTube Playlist URL here..."
  271. @change="onMusarePlaylistFileChange"
  272. ref="importMusarePlaylistFileInput"
  273. @keyup.enter="importMusarePlaylistFile()"
  274. />
  275. </p>
  276. <p class="control">
  277. <button
  278. class="button is-info"
  279. @click.prevent="importMusarePlaylistFile()"
  280. >
  281. <i class="material-icons icon-with-button">publish</i>Import
  282. </button>
  283. </p>
  284. </div>
  285. </div>
  286. </template>
  287. <style lang="less" scoped>
  288. #playlist-import-type select {
  289. border-radius: 0;
  290. }
  291. input[type="file"] {
  292. padding-left: 0;
  293. }
  294. input[type="file"]::file-selector-button {
  295. background: var(--light-grey);
  296. border: none;
  297. height: 100%;
  298. border-right: 1px solid var(--light-grey-3);
  299. margin-right: 8px;
  300. padding: 0 8px;
  301. cursor: pointer;
  302. }
  303. input[type="file"]::file-selector-button:hover {
  304. background: var(--light-grey-2);
  305. }
  306. @media screen and (max-width: 1300px) {
  307. .import-playlist-tab #song-query-results,
  308. .section {
  309. max-width: 100% !important;
  310. }
  311. }
  312. </style>