]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/import-youtube.ts
Support thumbnails in youtube import
[github/Chocobozzz/PeerTube.git] / server / tools / import-youtube.ts
CommitLineData
a7fea183 1import * as program from 'commander'
a7fea183 2import { join } from 'path'
a7fea183
C
3import * as youtubeDL from 'youtube-dl'
4import { VideoPrivacy } from '../../shared/models/videos'
5import { unlinkPromise } from '../helpers/core-utils'
1d791a26 6import { doRequestAndSaveToFile } from '../helpers/requests'
a7fea183
C
7import { getClient, getVideoCategories, login, searchVideo, uploadVideo } from '../tests/utils'
8
9program
10 .option('-u, --url <url>', 'Server url')
11 .option('-U, --username <username>', 'Username')
12 .option('-p, --password <token>', 'Password')
e7872038 13 .option('-y, --youtube-url <youtubeUrl>', 'Youtube URL')
a7fea183
C
14 .parse(process.argv)
15
16if (
17 !program['url'] ||
18 !program['username'] ||
19 !program['password'] ||
20 !program['youtubeUrl']
21) {
a87d467a
C
22 console.error('All arguments are required.')
23 process.exit(-1)
a7fea183
C
24}
25
26run().catch(err => console.error(err))
27
28let accessToken: string
29
30async function run () {
31 const res = await getClient(program['url'])
32 const client = {
33 id: res.body.client_id,
34 secret: res.body.client_secret
35 }
36
37 const user = {
38 username: program['username'],
39 password: program['password']
40 }
41
42 const res2 = await login(program['url'], client, user)
43 accessToken = res2.body.access_token
44
45 youtubeDL.getInfo(program['youtubeUrl'], [ '-j', '--flat-playlist' ], async (err, info) => {
46 if (err) throw err
47
e7872038
C
48 // Normalize utf8 fields
49 info = info.map(i => normalizeObject(i))
50
a7fea183
C
51 const videos = info.map(i => {
52 return { url: 'https://www.youtube.com/watch?v=' + i.id, name: i.title }
53 })
54
55 console.log('Will download and upload %d videos.\n', videos.length)
56
57 for (const video of videos) {
58 await processVideo(video)
59 }
60
61 console.log('I\'m finished!')
62 process.exit(0)
63 })
64}
65
e7872038 66function processVideo (video: { name: string, url: string }) {
a7fea183 67 return new Promise(async res => {
e7872038
C
68 const result = await searchVideo(program['url'], video.name)
69
70 console.log('############################################################\n')
71
a7fea183 72 if (result.body.total !== 0) {
e7872038 73 console.log('Video "%s" already exists, don\'t reupload it.\n', video.name)
a7fea183
C
74 return res()
75 }
76
e7872038 77 const path = join(__dirname, new Date().getTime() + '.mp4')
a7fea183 78
e7872038 79 console.log('Downloading video "%s"...', video.name)
a7fea183 80
e7872038
C
81 youtubeDL.exec(video.url, [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]', '-o', path ], {}, async (err, output) => {
82 if (err) return console.error(err)
a7fea183 83
e7872038 84 console.log(output.join('\n'))
a7fea183 85
e7872038
C
86 youtubeDL.getInfo(video.url, async (err, videoInfo) => {
87 if (err) return console.error(err)
a7fea183 88
e7872038 89 await uploadVideoOnPeerTube(normalizeObject(videoInfo), path)
a7fea183 90
e7872038
C
91 return res()
92 })
a7fea183
C
93 })
94 })
95}
96
a7fea183
C
97async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string) {
98 const category = await getCategory(videoInfo.categories)
99 const licence = getLicence(videoInfo.license)
100 const language = 13
101
1d791a26
C
102 let thumbnailfile
103 if (videoInfo.thumbnail) {
104 thumbnailfile = join(__dirname, 'thumbnail.jpg')
105
106 await doRequestAndSaveToFile({
107 method: 'GET',
108 uri: videoInfo.thumbnail
109 }, thumbnailfile)
110 }
111
a7fea183
C
112 const videoAttributes = {
113 name: videoInfo.title,
114 category,
115 licence,
116 language,
117 nsfw: false,
118 commentsEnabled: true,
119 description: videoInfo.description,
120 tags: videoInfo.tags.slice(0, 5),
121 privacy: VideoPrivacy.PUBLIC,
1d791a26
C
122 fixture: videoPath,
123 thumbnailfile
a7fea183
C
124 }
125
126 console.log('\nUploading on PeerTube video "%s".', videoAttributes.name)
127 await uploadVideo(program['url'], accessToken, videoAttributes)
1d791a26 128
a7fea183 129 await unlinkPromise(videoPath)
1d791a26
C
130 if (thumbnailfile) {
131 await unlinkPromise(thumbnailfile)
132 }
133
a7fea183
C
134 console.log('Uploaded video "%s"!\n', videoAttributes.name)
135}
136
137async function getCategory (categories: string[]) {
138 const categoryString = categories[0]
139
140 if (categoryString === 'News & Politics') return 11
141
142 const res = await getVideoCategories(program['url'])
143 const categoriesServer = res.body
144
145 for (const key of Object.keys(categoriesServer)) {
146 const categoryServer = categoriesServer[key]
147 if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10)
148 }
149
150 return undefined
151}
152
153function getLicence (licence: string) {
154 if (licence.indexOf('Creative Commons Attribution licence') !== -1) return 1
155
156 return undefined
157}
e7872038
C
158
159function normalizeObject (obj: any) {
160 const newObj: any = {}
161
162 for (const key of Object.keys(obj)) {
163 // Deprecated key
164 if (key === 'resolution') continue
165
166 const value = obj[key]
167
168 if (typeof value === 'string') {
169 newObj[key] = value.normalize()
170 } else {
171 newObj[key] = value
172 }
173 }
174
175 return newObj
176}