]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-add-components/video-import-url.component.ts
Add getSubs to YoutubeDL video import
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add-components / video-import-url.component.ts
1 import { Component, EventEmitter, OnInit, Output } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos'
4 import { AuthService, Notifier, ServerService } from '../../../core'
5 import { VideoService } from '../../../shared/video/video.service'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { LoadingBarService } from '@ngx-loading-bar/core'
8 import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
9 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
10 import { VideoEdit } from '@app/shared/video/video-edit.model'
11 import { FormValidatorService } from '@app/shared'
12 import { VideoCaptionService } from '@app/shared/video-caption'
13 import { VideoImportService } from '@app/shared/video-import'
14 import { scrollToTop } from '@app/shared/misc/utils'
15 import { switchMap, map } from 'rxjs/operators'
16
17 @Component({
18 selector: 'my-video-import-url',
19 templateUrl: './video-import-url.component.html',
20 styleUrls: [
21 '../shared/video-edit.component.scss',
22 './video-send.scss'
23 ]
24 })
25 export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate {
26 @Output() firstStepDone = new EventEmitter<string>()
27 @Output() firstStepError = new EventEmitter<void>()
28
29 targetUrl = ''
30
31 isImportingVideo = false
32 hasImportedVideo = false
33 isUpdatingVideo = false
34
35 video: VideoEdit
36 error: string
37
38 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
39
40 constructor (
41 protected formValidatorService: FormValidatorService,
42 protected loadingBar: LoadingBarService,
43 protected notifier: Notifier,
44 protected authService: AuthService,
45 protected serverService: ServerService,
46 protected videoService: VideoService,
47 protected videoCaptionService: VideoCaptionService,
48 private router: Router,
49 private videoImportService: VideoImportService,
50 private i18n: I18n
51 ) {
52 super()
53 }
54
55 ngOnInit () {
56 super.ngOnInit()
57 }
58
59 canDeactivate () {
60 return { canDeactivate: true }
61 }
62
63 isTargetUrlValid () {
64 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
65 }
66
67 importVideo () {
68 this.isImportingVideo = true
69
70 const videoUpdate: VideoUpdate = {
71 privacy: this.firstStepPrivacyId,
72 waitTranscoding: false,
73 commentsEnabled: true,
74 downloadEnabled: true,
75 channelId: this.firstStepChannelId
76 }
77
78 this.loadingBar.start()
79
80 this.videoImportService
81 .importVideoUrl(this.targetUrl, videoUpdate)
82 .pipe(
83 switchMap(res => {
84 return this.videoCaptionService
85 .listCaptions(res.video.id)
86 .pipe(
87 map(result => ({ video: res.video, videoCaptions: result.data }))
88 )
89 })
90 )
91 .subscribe(
92 ({ video, videoCaptions }) => {
93 this.loadingBar.complete()
94 this.firstStepDone.emit(video.name)
95 this.isImportingVideo = false
96 this.hasImportedVideo = true
97
98 this.video = new VideoEdit(Object.assign(video, {
99 commentsEnabled: videoUpdate.commentsEnabled,
100 downloadEnabled: videoUpdate.downloadEnabled,
101 support: null,
102 thumbnailUrl: null,
103 previewUrl: null
104 }))
105
106 this.videoCaptions = videoCaptions
107
108 this.hydrateFormFromVideo()
109 },
110
111 err => {
112 this.loadingBar.complete()
113 this.isImportingVideo = false
114 this.firstStepError.emit()
115 this.notifier.error(err.message)
116 }
117 )
118 }
119
120 updateSecondStep () {
121 if (this.checkForm() === false) {
122 return
123 }
124
125 this.video.patch(this.form.value)
126
127 this.isUpdatingVideo = true
128
129 // Update the video
130 this.updateVideoAndCaptions(this.video)
131 .subscribe(
132 () => {
133 this.isUpdatingVideo = false
134 this.notifier.success(this.i18n('Video to import updated.'))
135
136 this.router.navigate([ '/my-account', 'video-imports' ])
137 },
138
139 err => {
140 this.error = err.message
141 scrollToTop()
142 console.error(err)
143 }
144 )
145
146 }
147
148 private hydrateFormFromVideo () {
149 this.form.patchValue(this.video.toFormPatch())
150 }
151 }