-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
204 lines (186 loc) · 5.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const dotenv = require('dotenv')
const { google } = require('googleapis')
const path = require('path')
const fs = require('fs')
const youtubeDl = require('youtube-dl')
const axios = require('axios')
const querystring = require('querystring')
const FormData = require('form-data')
// Configure environment variables
dotenv.config()
const {
YOUTUBE_API_KEY,
PEERTUBE_INSTANCE,
PEERTUBE_USERNAME,
PEERTUBE_PASSWORD,
PEERTUBE_CHANNEL_ID,
YOUTUBE_CHANNEL_ID
} = process.env
const youtube = google.youtube({
version: 'v3',
auth: YOUTUBE_API_KEY
})
const DOWNLOAD_LOCATION = path.resolve(__dirname, 'yt2pt_downloads')
const INSTANCE_API_URL = `${PEERTUBE_INSTANCE}/api/v1`
const APP_STATE = {
channelInfo: {},
videosList: {}
}
async function getChannelInformation (id) {
try {
const response = await youtube.channels.list({
id,
part: 'statistics,snippet,contentDetails'
})
const channelInfo = {
id: response.data.items[0].id,
name: response.data.items[0].snippet.title,
uploadsPlaylist: response.data.items[0].contentDetails.relatedPlaylists.uploads,
totalVideos: response.data.items[0].statistics.videoCount
}
APP_STATE.channelInfo = channelInfo
return channelInfo
} catch (e) {
console.error(e)
}
}
async function getVideosList (channelInfo, pageToken) {
function fetchVideos (playlistId, pageToken) {
return youtube.playlistItems.list({
playlistId,
part: 'id,snippet,contentDetails',
maxResults: 50,
pageToken: pageToken || ''
})
}
try {
let videos = []
let response = await fetchVideos(channelInfo.uploadsPlaylist, pageToken)
while (response.data.nextPageToken) {
const mappedVideosList = response.data.items.map(({ contentDetails: { videoId }, snippet: {
title,
description
} }) => ({
videoId,
title,
description
}))
videos = [...videos, ...mappedVideosList]
response = await fetchVideos(channelInfo.uploadsPlaylist, response.data.nextPageToken)
console.log(`Collected ${videos.length} of ${channelInfo.totalVideos}`)
}
console.log('Collected all videos!')
APP_STATE.videosList = videos
return videos
} catch (e) {
console.error(e)
}
}
function createDownloadFolderIfNotExists () {
return new Promise(async (resolve, reject) => {
try {
await fs.promises.access(DOWNLOAD_LOCATION)
resolve()
} catch (e) {
if (e.code === 'ENOENT') {
// Create folder if doesnt exist
await fs.promises.mkdir(DOWNLOAD_LOCATION)
resolve()
} else {
reject(e)
}
}
})
}
function downloadVideo (videoId) {
const videoUrl = `https://www.youtube.com/watch?v=${videoId}`
const video = youtubeDl(videoUrl)
const videoLocation = `${DOWNLOAD_LOCATION}/${videoId}.mp4`
return new Promise((resolve, reject) => {
let size = 0
video.on('info', info => {
console.log('Download started')
console.log('filename: ' + info._filename)
console.log('size: ' + info.size)
size = info.size
})
video.on('error', reject)
const stream = fs.createWriteStream(videoLocation)
stream.on('close', () => {
console.log('Finished downloading')
resolve({
videoLocation,
size
})
})
video.pipe(stream)
})
}
async function downloadVideos (limit = 0) {
let videosDownloaded = 0
try {
await getChannelInformation(YOUTUBE_CHANNEL_ID)
await getVideosList(APP_STATE.channelInfo)
await createDownloadFolderIfNotExists()
while (videosDownloaded < limit) {
const { videoLocation, size } = await downloadVideo(APP_STATE.videosList[videosDownloaded].videoId)
APP_STATE.videosList[videosDownloaded].videoLocation = videoLocation
APP_STATE.videosList[videosDownloaded].videoSize = size
videosDownloaded += 1
console.log(APP_STATE.videosList[videosDownloaded])
}
} catch (e) {
console.error(e)
}
}
async function yt2pt (limit) {
try {
await downloadVideos(limit)
const { data: {
client_id: clientId,
client_secret: clientSecret
} } = await axios.get(`${INSTANCE_API_URL}/oauth-clients/local`)
const {
data: {
access_token: accessToken,
expires_in: expiresIn
}
} = await axios.post(`${INSTANCE_API_URL}/users/token`, querystring.stringify({
clientId,
clientSecret,
grant_type: 'password',
response_type: 'code',
username: PEERTUBE_USERNAME,
password: PEERTUBE_PASSWORD
}), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
console.log(`Access token expires in ${expiresIn / (1000 * 60)} minutes`)
let videosUploaded = 0
while (videosUploaded < limit) {
const videoToUpload = APP_STATE.videosList[videosUploaded]
console.log(`Uploading video at ${videoToUpload.videoLocation}`)
const formData = new FormData()
formData.append('channelId', PEERTUBE_CHANNEL_ID)
formData.append('name', videoToUpload.title)
formData.append('description', videoToUpload.description)
const videoStream = fs.createReadStream(videoToUpload.videoLocation)
formData.append('videofile', videoStream)
console.log('FORM DATA', formData)
await axios.post(`${INSTANCE_API_URL}/videos/upload`, formData, {
headers: {
...formData.getHeaders(),
'Authorization': `Bearer ${accessToken}`
},
maxContentLength: videoToUpload.videoSize * 1.5
})
videosUploaded += 1
console.log(`uploaded ${videosUploaded} / ${APP_STATE.channelInfo.totalVideos}`)
}
} catch (e) {
console.error(JSON.stringify(e, null, 2))
}
}
yt2pt(1)