-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
243 lines (201 loc) · 6.23 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
var request = require('request'),
minimist = require('minimist'),
path = require('path'),
fs = require('fs'),
xboxapi = require('./xboxapi');
var args = minimist(process.argv.slice(2));
var authKey = null;
var directory = path.join(process.cwd(), "xboxclips");
if (args.d !== undefined) {
directory = args.d;
}
if( args.k !== undefined ) {
authKey = args.k;
}
if( authKey === null ) {
console.error("please provide xboxapi key: -k [auth key] (get one at https://xboxapi.com/)");
process.exit(1);
}
console.log("download directory is " + directory);
function createDownloadDir(dir, done) {
fs.stat(dir, function (err, stats) {
if (err) {
/* directory does not exist */
fs.mkdir(dir, function (err) {
if (err) {
console.log("cannot create destination directory");
} else {
done && done(dir);
}
});
} else if (!stats.isDirectory()) {
console.log(dir + " is not a directory");
process.exit(1);
} else {
done && done(dir);
}
});
}
var api = null;
var running_downloads = 0;
var download_objects = {};
var progressItv = null;
function progressBars() {
if( progressItv === null ) {
progressItv = setInterval(showProgressBars,1000);
}
}
function showProgressBars() {
var i;
process.stdout.write('\u001B[2J\u001B[0;0f');
process.stdout.write("============ Downloading ============ \n");
var M = 10;
var dd = Object.keys(download_objects);
var mxl = 0;
dd.forEach(function(k){
var o = download_objects[k].name;
mxl = Math.max(o.length,mxl);
});
dd.forEach(function(k){
var obj = download_objects[k];
var str = obj.name;
while(str.length < mxl) {
str += " ";
}
str += " [";
var D = Math.floor(obj.progress/10);
for( var i = 0 ; i < D ; i++ ) {
str += "=";
}
while( i < 10 ) {
str += " ";
i++;
}
str += "] " + (obj.progress === undefined ? "queued" : obj.progress + "%");
process.stdout.write(str+"\n");
});
if( dd.length == 0 ) {
clearInterval(progressItv);
progressItv = null;
}
}
function downloadClips(dstdir,clips) {
clips.forEach(function (clip) {
if( inStore(clip.gameClipId) ) {
return; /* skip */
}
var dst_name = clip.titleName + " " + clip.clipName,
usename,
i = -1,
topath = null;
do {
i++;
usename = dst_name + ( i > 0 ? "_" + i : "" );
topath = path.join(dstdir, usename + ".mp4" );
} while(fs.existsSync(topath));
var writeStream = fs.createWriteStream(topath);
if( writeStream == null ) {
console.error("cannot create output file");
} else {
var failed = function() {
writeStream.end();
fs.unlinkSync(topath);
};
var download = clip.gameClipUris.some(function(element){
if( element.uriType === "Download" ) {
var downloaded = 0;
var len = element.fileSize;
running_downloads++;
download_objects[clip.gameClipId] = {
name : usename
};
request.get(element.uri)
.on('end',function(){
delete download_objects[clip.gameClipId];
running_downloads--;
writeToStore(clip.gameClipId);
progressBars();
})
.on('data',function(data){
downloaded += data.length;
download_objects[clip.gameClipId].progress = Math.floor(downloaded/len * 100.0);
progressBars();
})
.on('error',function(err){
delete download_objects[clip.gameClipId];
running_downloads--;
progressBars();
failed();
console.error("download error " + err);
}).pipe(writeStream);
return true;
} else{
return false;
}
});
if( ! download ) {
console.error(usename + " does not have downloadable Uris");
failed();
}
}
});
}
function fetchVideos( done) {
if (!api) {
api = new xboxapi(authKey, {
proxy: "http://web-proxy.boi.hp.com:8088"
});
}
var perform_fetch = function (error, uid) {
console.log("fetching clips...");
if (error) {
console.error(error);
} else {
api.getGameClips(uid,function (error, clips) {
if (error) {
console.error(error);
} else {
done && done(clips);
}
});
}
};
if (api.myxuid == null) {
console.log("finding xbox user id...");
api.getXuid(perform_fetch);
} else {
perform_fetch(null, api.myxuid);
}
}
var downloaded = [];
var store_name = "store.json";
function readDownloaded(cb) {
fs.readFile(path.join(directory,store_name),function(err,data){
if( err ) {
cb && cb(null);
} else {
cb && cb(data ? JSON.parse(data) : null );
}
});
}
function writeToStore(id) {
downloaded.push(id);
fs.writeFile(path.join(directory,store_name),JSON.stringify(downloaded),{ flag : "w"},function(err){
if( err ) {
console.error(err);
}
});
}
function inStore(id) {
return downloaded.indexOf(id) >= 0;
}
createDownloadDir(directory, function (dir) {
readDownloaded(function(data){
if( data ) {
downloaded = data;
}
fetchVideos( function (clips) {
downloadClips(dir,clips);
});
});
});