-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
119 lines (106 loc) · 3.48 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
var request = require("request").defaults({
jar: true
});
var fs = require('fs');
var cheerio = require('cheerio');
var _ = require('underscore');
var fs = require('fs');
var async = require('async');
var mkdirp = require('mkdirp');
var blobSvc = require('azure-storage').createBlobService();
blobSvc.createContainerIfNotExists('mixergydl', function(error){
if(!error){
console.log('Azure storage created')
}
});
var argv = require('minimist')(process.argv.slice(2));
var VIDEO_TYPES = [
'flv',
'iphone',
'hdflv',
'hdmp4',
'still',
'original',
'preview'
];
var LOGIN_URL = 'https://mixergy.com/wp-login.php?wpe-login=mixergy';
var COOKIES;
mkdirp('videos');
request.post({
url: LOGIN_URL,
form: {
log: process.env.MIXERGY_USERNAME,
pwd: process.env.MIXERGY_PASSWORD,
testcookie: 1,
'wp-submit': 'Log In'
}
}, function(error, response, body) {
request("https://www.kimonolabs.com/api/60slzlww?apikey=dokWpGAvcSxKiPdtLkHmHh7eDuy0sMOk",
function(err, response, body) {
if (!err) {
var docs = JSON.parse(body);
console.log('Detected ' + docs.count + ' interviews.');
var startAt = doc.count - int(argv.start) || 0;
// var oneInterview = [docs.results.interviews[40]];
// _.forEach(oneInterview, function(obj){
var threads = 3;
async.eachLimit(docs.results.interviews.slice(startAt), threads, function(obj, next){
processInterview(obj, next);
}, function(){
console.log('Finished!');
})
}
});
});
function processInterview(obj, cb){
request({
url: obj.Title.href,
method: "GET"
}, function(error, response, body) {
var $ = cheerio.load(body);
var iframe = $('iframe');
var isPremium = $('div#banner-go-premium').length === 0 ? true : false;
if (!isPremium) {
console.log('You are not on premium subscription.');
}
if (!iframe.length) {
console.log('Cannot download this video: ' + obj['Episode Number']);
return false;
}
var vid_url = 'http:' + $('iframe').attr('src');
request({
url: vid_url,
method: 'GET'
}, function(err, res, body) {
var wistia = body.match(/\{"assets(.*)}}/g);
var videos = JSON.parse(String(wistia[0]));
var filename = 'videos/' + String(obj['Episode Number']) + '.flv';
console.log('Video can be downloaded: ' + obj['Episode Number']);
download(filename, videos.assets.flv.url, cb);
});
});
}
function download(localFile, remotePath, callback) {
// var localStream = fs.createWriteStream(localFile);
var writeStream = blobSvc.createWriteStreamToBlockBlob(
'mixergydl',
localFile,
{ contentType: 'video/flv' },
function(error, result, response){
if(error){
console.error(error);
}
});
var out = request({
uri: remotePath
});
out.on('response', function(resp) {
if (resp.statusCode === 200) {
out.pipe(writeStream);
writeStream.on('close', function() {
console.log('Done downloading: ' + localFile);
callback();
});
}
})
};