Import.vue 17 KB

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