-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (86 loc) · 2.58 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
const request = require('request');
const Prom = require('bluebird');
const fs = require('fs');
const path = require('path');
const moment = require('moment');
const colors = require('colors');
const BASE = "https://api.vineapp.com/";
const HEADERS = {
referer: 'https://vine.co/',
'user-agent': 'com.vine.iphone/1.0.3 (unknown, iPhone OS 8.1.0, iPhone, Scale/2.000000)'
};
const DEFAULTS = {
maxDownload: Infinity
}
var DL = (function() {
function _getVineData(url, options = {}) {
return new Prom(function(resolve, reject) {
var results = [];
function __doUserRequest(page) {
request({
url: url,
qs: {
page: page,
size: 100
},
headers: {
referer: 'https://vine.co/',
'user-agent': 'com.vine.iphone/1.0.3 (unknown, iPhone OS 6.1.0, iPhone, Scale/2.000000)'
}
}, function(err, res, body) {
var j = JSON.parse(body);
results = [...results, ...j['data']['records']]
console.log(colors.green(`Got timeline page, at ${results.length} items`));
if (results.length > options.maxDownload) {
resolve(results.slice(0, options.maxDownload));
} else {
if (j['data']['nextPage']) {
__doUserRequest(j['data']['nextPage'])
} else {
resolve(results);
}
}
});
}
__doUserRequest(1);
});
}
function _writeFile(url, savePath) {
return new Prom((yes, no) => {
if (!fs.existsSync(savePath)) {
console.log(colors.yellow(`Starting ${url} -> ${savePath}`));
var stream = fs.createWriteStream(savePath);
stream.on('finish', () => {
console.log(colors.green(`\t Success! ${savePath}`));
yes(savePath)
});
request({ url: url, header: HEADERS }).pipe(stream);
}else{
yes(savePath)
}
})
}
function downloadTimeline(userId, downloadDir = __dirname, options = {}) {
options = Object.assign({},DEFAULTS, options)
var vineUrl = BASE + 'timelines/users/' + userId;
return _getVineData(vineUrl, options)
.then((rawVineData) => {
console.log(colors.green(`Got timeline of ${rawVineData.length} posts`));
return Prom.map(rawVineData, (vinePost => {
let { videoUrl, created } = vinePost
created = moment(created).unix()
console.log(created);
let postId = vinePost.postId.toString()
let savePath = path.join(downloadDir, `${created}_${postId}.mp4`)
return _writeFile(videoUrl, savePath)
}), { concurrency: 1 })
.then(downlaodedPaths => {
return { rawVineData: rawVineData, downlaodedPaths: downlaodedPaths }
})
});
}
return {
downloadTimeline: downloadTimeline
}
})();
module.exports = DL;