forked from benjaminlin1188/vuemastery-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmirror.js
More file actions
150 lines (134 loc) · 4.75 KB
/
mirror.js
File metadata and controls
150 lines (134 loc) · 4.75 KB
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
const fs = require('fs');
const url = require('url');
const https = require('https');
try {
var data = fs.readFileSync('./data.txt', 'utf8');
data = data.split("\n");
data.forEach(function (v) {
if (v.length > 10) {
let id = v.trim();
id = id.replace('https://player.vimeo.com/video/', '').replace('?', '').replace('autoplay=1', '').replace(/[&?]/gm, '').replace(/app_id=(.*)/gm, '');
// Default APP ID is 122963
startDownloadByID(id, 1080, 122963);
}
})
} catch (e) {
console.log('Error on read file:', e.stack);
}
return;
// Default APP ID is 122963
startDownloadByID(id, quality, 122963)
async function startDownloadByID(vID, quality, appID) {
try {
var pageData = await getVimeoPageByID(vID, quality, appID)
var videoURL = pageData.videoURL;
var subtitleURL = pageData.subtitleURL;
var courseTitle = pageData.title || vID;
if (videoURL !== null) {
console.log(courseTitle + ', Downloading...');
await downloadFile(videoURL, courseTitle.replace(/[^\w\s]/gi, '-') + '.mp4').then(function gotData(data) {
console.log(courseTitle + ', Download Complete.');
}, reason => {
console.log('Error, ' + reason);
});
//Download Video Subtitle from vimeo
if (subtitleURL !== null) {
await downloadFile(subtitleURL, courseTitle.replace(/[^\w\s]/gi, '-') + '.vtt').then(function gotData(data) {
console.log(courseTitle + ', Subtitle Download Complete.');
}, reason => {
console.log('Error, ' + reason);
});
}
} else
console.log('Video not found');
} catch (e) {
console.log('Error On video:' + vID);
console.error(e);
return;
}
}
async function downloadFile(url, name) {
const file = fs.createWriteStream(name);
return new Promise((resolve, reject) => {
let request = https.get(url, function (response) {
response.pipe(file);
file.on('finish', function () {
file.close();
resolve(response);
});
});
/* if there's an error, then reject the Promise
* (can be handled with Promise.prototype.catch) */
request.on('error', reject);
request.end();
});
}
async function getVimeoPageByID(id, quality, appID) {
return new Promise(function (resolve, reject) {
const headers = {
'User-Agent': {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded'
}
};
https.get('https://player.vimeo.com/video/' + id + '?autoplay=1&app_id=' + appID,
{headers: headers}
, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
resolve({
videoURL: findVideoUrl(body, quality),
title: findTitle(body),
subtitleURL: findSubtitleUrl(body)
});
});
res.on('error', (e) => {
reject(e)
});
});
})
}
function findVideoUrl(str, quality) {
const regex = /(?:config = )(?:\{)(.*(\n.*?)*)(?:\"\})/gm;
let res = regex.exec(str);
if (res !== null) {
if (typeof res[0] !== "undefined") {
let config = res[0].replace('config = ', '');
config = JSON.parse(config);
let progressive = config.request.files.progressive, videoURL;
for (let item of progressive) {
videoURL = item.url;
if (quality + 'p' === item.quality)
break;
}
return videoURL;
}
}
return null;
}
function findSubtitleUrl(str) {
const regex = /(?:config = )(?:\{)(.*(\n.*?)*)(?:\"\})/gm;
let res = regex.exec(str);
if (res !== null) {
if (typeof res[0] !== "undefined") {
let config = res[0].replace('config = ', '');
config = JSON.parse(config);
let subtitleURL = 'https://vimeo.com' + config.request.text_tracks[0].url;
return subtitleURL;
}
}
return null;
}
function findTitle(str) {
const VIMEO_NAME = "on Vimeo"
let title = str.match(/<title.*?>(.*)<\/title>/)[1];
if (title) {
return title.split(VIMEO_NAME)[0].trim();
} else {
return null;
}
}