ImportPlaylists.vue 8.3 KB

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