Import.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. <script setup lang="ts">
  2. import { defineAsyncComponent, ref } from "vue";
  3. import { useStore } from "vuex";
  4. import { useRouter } from "vue-router";
  5. import Toast from "toasters";
  6. const AdvancedTable = defineAsyncComponent(
  7. () => import("@/components/AdvancedTable.vue")
  8. );
  9. const store = useStore();
  10. const router = useRouter();
  11. const { socket } = store.state.websockets;
  12. const createImport = ref({
  13. stage: 2,
  14. importMethod: "youtube",
  15. youtubeUrl: "",
  16. isImportingOnlyMusic: false
  17. });
  18. const columnDefault = ref({
  19. sortable: true,
  20. hidable: true,
  21. defaultVisibility: "shown",
  22. draggable: true,
  23. resizable: true,
  24. minWidth: 200,
  25. maxWidth: 600
  26. });
  27. const columns = ref([
  28. {
  29. name: "options",
  30. displayName: "Options",
  31. properties: ["_id", "status"],
  32. sortable: false,
  33. hidable: false,
  34. resizable: false,
  35. minWidth: 160,
  36. defaultWidth: 160
  37. },
  38. {
  39. name: "type",
  40. displayName: "Type",
  41. properties: ["type"],
  42. sortProperty: "type",
  43. minWidth: 120,
  44. defaultWidth: 120
  45. },
  46. {
  47. name: "requestedBy",
  48. displayName: "Requested By",
  49. properties: ["requestedBy"],
  50. sortProperty: "requestedBy"
  51. },
  52. {
  53. name: "requestedAt",
  54. displayName: "Requested At",
  55. properties: ["requestedAt"],
  56. sortProperty: "requestedAt"
  57. },
  58. {
  59. name: "successful",
  60. displayName: "Successful",
  61. properties: ["response"],
  62. sortProperty: "response.successful",
  63. minWidth: 120,
  64. defaultWidth: 120
  65. },
  66. {
  67. name: "alreadyInDatabase",
  68. displayName: "Existing",
  69. properties: ["response"],
  70. sortProperty: "response.alreadyInDatabase",
  71. minWidth: 120,
  72. defaultWidth: 120
  73. },
  74. {
  75. name: "failed",
  76. displayName: "Failed",
  77. properties: ["response"],
  78. sortProperty: "response.failed",
  79. minWidth: 120,
  80. defaultWidth: 120
  81. },
  82. {
  83. name: "status",
  84. displayName: "Status",
  85. properties: ["status"],
  86. sortProperty: "status",
  87. defaultVisibility: "hidden"
  88. },
  89. {
  90. name: "url",
  91. displayName: "URL",
  92. properties: ["query.url"],
  93. sortProperty: "query.url"
  94. },
  95. {
  96. name: "musicOnly",
  97. displayName: "Music Only",
  98. properties: ["query.musicOnly"],
  99. sortProperty: "query.musicOnly",
  100. minWidth: 120,
  101. defaultWidth: 120
  102. },
  103. {
  104. name: "_id",
  105. displayName: "Import ID",
  106. properties: ["_id"],
  107. sortProperty: "_id",
  108. minWidth: 215,
  109. defaultWidth: 215,
  110. defaultVisibility: "hidden"
  111. }
  112. ]);
  113. const filters = ref([
  114. {
  115. name: "_id",
  116. displayName: "Import ID",
  117. property: "_id",
  118. filterTypes: ["exact"],
  119. defaultFilterType: "exact"
  120. },
  121. {
  122. name: "type",
  123. displayName: "Type",
  124. property: "type",
  125. filterTypes: ["exact"],
  126. defaultFilterType: "exact",
  127. dropdown: [["youtube", "YouTube"]]
  128. },
  129. {
  130. name: "requestedBy",
  131. displayName: "Requested By",
  132. property: "requestedBy",
  133. filterTypes: ["contains", "exact", "regex"],
  134. defaultFilterType: "contains"
  135. },
  136. {
  137. name: "requestedAt",
  138. displayName: "Requested At",
  139. property: "requestedAt",
  140. filterTypes: ["datetimeBefore", "datetimeAfter"],
  141. defaultFilterType: "datetimeBefore"
  142. },
  143. {
  144. name: "response.successful",
  145. displayName: "Successful",
  146. property: "response.successful",
  147. filterTypes: [
  148. "numberLesserEqual",
  149. "numberLesser",
  150. "numberGreater",
  151. "numberGreaterEqual",
  152. "numberEquals"
  153. ],
  154. defaultFilterType: "numberLesser"
  155. },
  156. {
  157. name: "response.alreadyInDatabase",
  158. displayName: "Existing",
  159. property: "response.alreadyInDatabase",
  160. filterTypes: [
  161. "numberLesserEqual",
  162. "numberLesser",
  163. "numberGreater",
  164. "numberGreaterEqual",
  165. "numberEquals"
  166. ],
  167. defaultFilterType: "numberLesser"
  168. },
  169. {
  170. name: "response.failed",
  171. displayName: "Failed",
  172. property: "response.failed",
  173. filterTypes: [
  174. "numberLesserEqual",
  175. "numberLesser",
  176. "numberGreater",
  177. "numberGreaterEqual",
  178. "numberEquals"
  179. ],
  180. defaultFilterType: "numberLesser"
  181. },
  182. {
  183. name: "status",
  184. displayName: "Status",
  185. property: "status",
  186. filterTypes: ["contains", "exact", "regex"],
  187. defaultFilterType: "contains"
  188. },
  189. {
  190. name: "url",
  191. displayName: "URL",
  192. property: "query.url",
  193. filterTypes: ["contains", "exact", "regex"],
  194. defaultFilterType: "contains"
  195. },
  196. {
  197. name: "musicOnly",
  198. displayName: "Music Only",
  199. property: "query.musicOnly",
  200. filterTypes: ["exact"],
  201. defaultFilterType: "exact",
  202. dropdown: [
  203. [true, "True"],
  204. [false, "False"]
  205. ]
  206. },
  207. {
  208. name: "status",
  209. displayName: "Status",
  210. property: "status",
  211. filterTypes: ["exact"],
  212. defaultFilterType: "exact",
  213. dropdown: [
  214. ["success", "Success"],
  215. ["in-progress", "In Progress"],
  216. ["failed", "Failed"]
  217. ]
  218. }
  219. ]);
  220. const events = ref({
  221. adminRoom: "import",
  222. updated: {
  223. event: "admin.importJob.updated",
  224. id: "importJob._id",
  225. item: "importJob"
  226. },
  227. removed: {
  228. event: "admin.importJob.removed",
  229. id: "jobId"
  230. }
  231. });
  232. const openModal = payload =>
  233. store.dispatch("modalVisibility/openModal", payload);
  234. const setJob = payload => store.dispatch("longJobs/setJob", payload);
  235. const openAdvancedTable = importJob => {
  236. const filter = {
  237. appliedFilters: [
  238. {
  239. data: importJob._id,
  240. filter: {
  241. name: "importJob",
  242. displayName: "Import Job",
  243. property: "importJob",
  244. filterTypes: ["special"],
  245. defaultFilterType: "special"
  246. },
  247. filterType: { name: "special", displayName: "Special" }
  248. }
  249. ],
  250. appliedFilterOperator: "or"
  251. };
  252. router.push({
  253. path: `/admin/youtube/videos`,
  254. query: { filter: JSON.stringify(filter) }
  255. });
  256. };
  257. const resetCreateImport = () => {
  258. createImport.value = {
  259. stage: 2,
  260. importMethod: "youtube",
  261. youtubeUrl: "",
  262. isImportingOnlyMusic: false
  263. };
  264. };
  265. const importFromYoutube = () => {
  266. if (!createImport.value.youtubeUrl)
  267. return new Toast("Please enter a YouTube URL.");
  268. let id;
  269. let title;
  270. return socket.dispatch(
  271. "youtube.requestSetAdmin",
  272. createImport.value.youtubeUrl,
  273. createImport.value.isImportingOnlyMusic,
  274. true,
  275. {
  276. cb: () => {},
  277. onProgress: res => {
  278. if (res.status === "started") {
  279. id = res.id;
  280. title = res.title;
  281. }
  282. if (id)
  283. setJob({
  284. id,
  285. name: title,
  286. ...res
  287. });
  288. }
  289. }
  290. );
  291. };
  292. const submitCreateImport = stage => {
  293. if (stage === 2) {
  294. const playlistRegex = /[\\?&]list=([^&#]*)/;
  295. const channelRegex =
  296. /\.[\w]+\/(?:(?:channel\/(UC[0-9A-Za-z_-]{21}[AQgw]))|(?:user\/?([\w-]+))|(?:c\/?([\w-]+))|(?:\/?([\w-]+)))/;
  297. if (
  298. playlistRegex.exec(createImport.value.youtubeUrl) ||
  299. channelRegex.exec(createImport.value.youtubeUrl)
  300. )
  301. importFromYoutube();
  302. else
  303. return new Toast({
  304. content: "Please enter a valid YouTube URL.",
  305. timeout: 4000
  306. });
  307. }
  308. if (stage === 3) resetCreateImport();
  309. else createImport.value.stage += 1;
  310. return createImport.value.stage;
  311. };
  312. // const prevCreateImport = stage => {
  313. // if (stage === 2) createImport.value.stage = 1;
  314. // };
  315. const getDateFormatted = createdAt => {
  316. const date = new Date(createdAt);
  317. const year = date.getFullYear();
  318. const month = `${date.getMonth() + 1}`.padStart(2, 0);
  319. const day = `${date.getDate()}`.padStart(2, 0);
  320. const hour = `${date.getHours()}`.padStart(2, 0);
  321. const minute = `${date.getMinutes()}`.padStart(2, 0);
  322. return `${year}-${month}-${day} ${hour}:${minute}`;
  323. };
  324. const editSongs = videos => {
  325. const songs = videos.map(youtubeId => ({ youtubeId }));
  326. if (songs.length === 1)
  327. openModal({ modal: "editSong", data: { song: songs[0] } });
  328. else openModal({ modal: "editSongs", data: { songs } });
  329. };
  330. const importAlbum = youtubeIds => {
  331. socket.dispatch("songs.getSongsFromYoutubeIds", youtubeIds, res => {
  332. if (res.status === "success") {
  333. openModal({
  334. modal: "importAlbum",
  335. data: { songs: res.data.songs }
  336. });
  337. } else new Toast("Could not get songs.");
  338. });
  339. };
  340. const removeImportJob = jobId => {
  341. socket.dispatch("media.removeImportJobs", jobId, res => {
  342. new Toast(res.message);
  343. });
  344. };
  345. const handleConfirmed = ({ action, params }) => {
  346. if (typeof action === "function") {
  347. if (params) action(params);
  348. else action();
  349. }
  350. };
  351. const confirmAction = ({ message, action, params }) => {
  352. openModal({
  353. modal: "confirm",
  354. data: {
  355. message,
  356. action,
  357. params,
  358. onCompleted: handleConfirmed
  359. }
  360. });
  361. };
  362. </script>
  363. <template>
  364. <div>
  365. <page-metadata title="Admin | Songs | Import" />
  366. <div class="admin-tab import-tab">
  367. <div class="card">
  368. <h1>Import Songs</h1>
  369. <p>Import songs from YouTube playlists or channels</p>
  370. </div>
  371. <div class="section-row">
  372. <div class="card left-section">
  373. <h4>Start New Import</h4>
  374. <hr class="section-horizontal-rule" />
  375. <div v-if="false && createImport.stage === 1" class="stage">
  376. <label class="label">Import Method</label>
  377. <div class="control is-expanded select">
  378. <select v-model="createImport.importMethod">
  379. <option value="youtube">YouTube</option>
  380. </select>
  381. </div>
  382. <div class="control is-expanded">
  383. <button
  384. class="button is-primary"
  385. @click.prevent="submitCreateImport(1)"
  386. >
  387. <i class="material-icons">navigate_next</i>
  388. Next
  389. </button>
  390. </div>
  391. </div>
  392. <div
  393. v-else-if="
  394. createImport.stage === 2 &&
  395. createImport.importMethod === 'youtube'
  396. "
  397. class="stage"
  398. >
  399. <label class="label"
  400. >YouTube URL
  401. <info-icon
  402. tooltip="YouTube playlist or channel URLs may be provided"
  403. />
  404. </label>
  405. <div class="control is-expanded">
  406. <input
  407. class="input"
  408. type="text"
  409. placeholder="YouTube Playlist or Channel URL"
  410. v-model="createImport.youtubeUrl"
  411. />
  412. </div>
  413. <div class="control is-expanded checkbox-control">
  414. <label class="switch">
  415. <input
  416. type="checkbox"
  417. id="import-music-only"
  418. v-model="createImport.isImportingOnlyMusic"
  419. />
  420. <span class="slider round"></span>
  421. </label>
  422. <label class="label" for="import-music-only">
  423. Import Music Only
  424. <info-icon
  425. tooltip="Only import videos from YouTube identified as music"
  426. @click.prevent
  427. />
  428. </label>
  429. </div>
  430. <div class="control is-expanded">
  431. <button
  432. class="control is-expanded button is-primary"
  433. @click.prevent="submitCreateImport(2)"
  434. >
  435. <i class="material-icons icon-with-button"
  436. >publish</i
  437. >
  438. Import
  439. </button>
  440. </div>
  441. </div>
  442. <div v-if="createImport.stage === 3" class="stage">
  443. <p class="has-text-centered import-started">
  444. Import Started
  445. </p>
  446. <div class="control is-expanded">
  447. <button
  448. class="button is-info"
  449. @click.prevent="submitCreateImport(3)"
  450. >
  451. <i class="material-icons icon-with-button"
  452. >restart_alt</i
  453. >
  454. Start Again
  455. </button>
  456. </div>
  457. </div>
  458. </div>
  459. <div class="card right-section">
  460. <h4>Manage Imports</h4>
  461. <hr class="section-horizontal-rule" />
  462. <advanced-table
  463. :column-default="columnDefault"
  464. :columns="columns"
  465. :filters="filters"
  466. :events="events"
  467. data-action="media.getImportJobs"
  468. name="admin-songs-import"
  469. :max-width="1060"
  470. >
  471. <template #column-options="slotProps">
  472. <div class="row-options">
  473. <button
  474. class="button is-primary icon-with-button material-icons"
  475. @click="openAdvancedTable(slotProps.item)"
  476. :disabled="
  477. slotProps.item.removed ||
  478. slotProps.item.status !== 'success'
  479. "
  480. content="Manage imported videos"
  481. v-tippy
  482. >
  483. table_view
  484. </button>
  485. <button
  486. class="button is-primary icon-with-button material-icons"
  487. @click="
  488. editSongs(
  489. slotProps.item.response
  490. .successfulVideoIds
  491. )
  492. "
  493. :disabled="
  494. slotProps.item.removed ||
  495. slotProps.item.status !== 'success'
  496. "
  497. content="Create/edit song from videos"
  498. v-tippy
  499. >
  500. music_note
  501. </button>
  502. <button
  503. class="button icon-with-button material-icons import-album-icon"
  504. @click="
  505. importAlbum(
  506. slotProps.item.response
  507. .successfulVideoIds
  508. )
  509. "
  510. :disabled="
  511. slotProps.item.removed ||
  512. slotProps.item.status !== 'success'
  513. "
  514. content="Import album from videos"
  515. v-tippy
  516. >
  517. album
  518. </button>
  519. <button
  520. class="button is-danger icon-with-button material-icons"
  521. @click.prevent="
  522. confirmAction({
  523. message:
  524. 'Note: Removing an import will not remove any videos or songs.',
  525. action: removeImportJob,
  526. params: slotProps.item._id
  527. })
  528. "
  529. :disabled="
  530. slotProps.item.removed ||
  531. slotProps.item.status === 'in-progress'
  532. "
  533. content="Remove Import"
  534. v-tippy
  535. >
  536. delete_forever
  537. </button>
  538. </div>
  539. </template>
  540. <template #column-type="slotProps">
  541. <span :title="slotProps.item.type">{{
  542. slotProps.item.type
  543. }}</span>
  544. </template>
  545. <template #column-requestedBy="slotProps">
  546. <user-link :user-id="slotProps.item.requestedBy" />
  547. </template>
  548. <template #column-requestedAt="slotProps">
  549. <span
  550. :title="new Date(slotProps.item.requestedAt)"
  551. >{{
  552. getDateFormatted(slotProps.item.requestedAt)
  553. }}</span
  554. >
  555. </template>
  556. <template #column-successful="slotProps">
  557. <span :title="slotProps.item.response.successful">{{
  558. slotProps.item.response.successful
  559. }}</span>
  560. </template>
  561. <template #column-alreadyInDatabase="slotProps">
  562. <span
  563. :title="
  564. slotProps.item.response.alreadyInDatabase
  565. "
  566. >{{
  567. slotProps.item.response.alreadyInDatabase
  568. }}</span
  569. >
  570. </template>
  571. <template #column-failed="slotProps">
  572. <span :title="slotProps.item.response.failed">{{
  573. slotProps.item.response.failed
  574. }}</span>
  575. </template>
  576. <template #column-status="slotProps">
  577. <span :title="slotProps.item.status">{{
  578. slotProps.item.status
  579. }}</span>
  580. </template>
  581. <template #column-url="slotProps">
  582. <a
  583. :href="slotProps.item.query.url"
  584. target="_blank"
  585. >{{ slotProps.item.query.url }}</a
  586. >
  587. </template>
  588. <template #column-musicOnly="slotProps">
  589. <span :title="slotProps.item.query.musicOnly">{{
  590. slotProps.item.query.musicOnly
  591. }}</span>
  592. </template>
  593. <template #column-_id="slotProps">
  594. <span :title="slotProps.item._id">{{
  595. slotProps.item._id
  596. }}</span>
  597. </template>
  598. </advanced-table>
  599. </div>
  600. </div>
  601. </div>
  602. </div>
  603. </template>
  604. <style lang="less" scoped>
  605. .admin-tab.import-tab {
  606. .section-row {
  607. display: flex;
  608. flex-wrap: wrap;
  609. height: 100%;
  610. .card {
  611. max-height: 100%;
  612. overflow-y: auto;
  613. flex-grow: 1;
  614. .control.is-expanded {
  615. .button {
  616. width: 100%;
  617. }
  618. &:not(:last-of-type) {
  619. margin-bottom: 10px !important;
  620. }
  621. &:last-of-type {
  622. margin-bottom: 0 !important;
  623. }
  624. }
  625. .control.is-grouped > .button {
  626. &:not(:last-child) {
  627. border-radius: @border-radius 0 0 @border-radius;
  628. }
  629. &:last-child {
  630. border-radius: 0 @border-radius @border-radius 0;
  631. }
  632. }
  633. }
  634. .left-section {
  635. height: 100%;
  636. max-width: 400px;
  637. margin-right: 20px !important;
  638. .checkbox-control label.label {
  639. margin-left: 10px;
  640. }
  641. .import-started {
  642. font-size: 18px;
  643. font-weight: 600;
  644. margin-bottom: 10px;
  645. }
  646. }
  647. .right-section {
  648. max-width: calc(100% - 400px);
  649. .row-options .material-icons.import-album-icon {
  650. background-color: var(--purple);
  651. color: var(--white);
  652. border-color: var(--purple);
  653. font-size: 20px;
  654. }
  655. }
  656. @media screen and (max-width: 1200px) {
  657. .card {
  658. flex-basis: 100%;
  659. max-height: unset;
  660. &.left-section {
  661. max-width: unset;
  662. margin-right: 0 !important;
  663. margin-bottom: 10px !important;
  664. }
  665. &.right-section {
  666. max-width: unset;
  667. }
  668. }
  669. }
  670. }
  671. }
  672. </style>