|
1 | 1 | const http = require('https');
|
2 | 2 | const fs = require('fs');
|
3 | 3 |
|
4 |
| -// Add Pack (downloading pack from mediafire) |
5 |
| -exports.addPack = function(name){ |
6 | 4 |
|
7 |
| - let patt = new RegExp(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/) |
| 5 | +// Download File From url |
| 6 | +const download = function (url, name) { |
8 | 7 |
|
9 |
| - if(patt.test(name)){ |
| 8 | + console.log("\x1b[36m", "Downloading " + name + ".zip ...", "\x1b[0m"); |
10 | 9 |
|
11 |
| - download(name, "external") |
| 10 | + let file = fs.createWriteStream(name + '.zip'); |
| 11 | + let request = http.get(url, function (response) { |
12 | 12 |
|
13 |
| - } else http.get("https://download1350.mediafire.com/q7aa9e774jug/oyrbdi6fbpqo1bl/mcscri%27%2B%27pt+packages.json", (res) => { |
| 13 | + response.pipe(file); |
| 14 | + response.on('end', () => { |
| 15 | + console.log("\x1b[32m", "Download of " + name + ".zip finished!", "\x1b[0m") |
| 16 | + }); |
14 | 17 |
|
15 |
| - res.setEncoding('utf8'); |
16 |
| - res.on('data', function (chunk) { |
| 18 | + }).on('error', function (e) { |
17 | 19 |
|
18 |
| - let packs = JSON.parse(chunk) |
19 |
| - let obj = packs.find(obj => obj.name === name.toLowerCase()) |
| 20 | + console.log("\x1b[31m", "Got error: " + e.message, "\x1b[0m"); |
20 | 21 |
|
21 |
| - if(obj && obj.url){ |
22 |
| - console.log(obj.url.slice(0, 17)) |
23 |
| - if(obj.url.slice(0, 17) == "https://github.com" || obj.url.slice(0, 16) == "http://github.com") downloadFromRedirectLink(obj.url, obj.name) |
| 22 | + }); |
| 23 | +}; |
| 24 | +// Add Pack (downloading pack from mediafire) |
| 25 | +exports.addPack = function (name) { |
24 | 26 |
|
25 |
| - else download(obj.url, obj.name) |
| 27 | + let patt = new RegExp(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&/=]*)/); |
26 | 28 |
|
27 |
| - } else { |
| 29 | + if (patt.test(name)) { |
28 | 30 |
|
29 |
| - let packStr = "" |
30 |
| - packs.forEach((e, i) => { |
| 31 | + download(name, "external") |
31 | 32 |
|
32 |
| - packStr += e.name |
33 |
| - if(i != packs.length - 1) packStr += ", " |
| 33 | + } else http.get("https://download1350.mediafire.com/q7aa9e774jug/oyrbdi6fbpqo1bl/mcscri%27%2B%27pt+packages.json", (res) => { |
34 | 34 |
|
35 |
| - }) |
| 35 | + res.setEncoding('utf8'); |
| 36 | + res.on('data', function (chunk) { |
36 | 37 |
|
37 |
| - console.log("\x1b[31m","Your pack was not found! Please use one of these: ","\x1b[0m") |
38 |
| - console.log("\x1b[36m",packStr,"\x1b[0m") |
| 38 | + let packs = JSON.parse(chunk); |
| 39 | + let obj = packs.find(obj => obj.name === name.toLowerCase()); |
39 | 40 |
|
40 |
| - } |
41 |
| - }); |
| 41 | + if (obj && obj.url) { |
| 42 | + console.log(obj.url.slice(0, 17)); |
| 43 | + if (obj.url.slice(0, 17) === "https://github.com" || obj.url.slice(0, 16) === "http://github.com") downloadFromRedirectLink(obj.url, obj.name); |
42 | 44 |
|
43 |
| - }).on('error', function(e) { |
| 45 | + else download(obj.url, obj.name) |
44 | 46 |
|
45 |
| - console.log("\x1b[31m","Got error: " + e.message,"\x1b[0m"); |
| 47 | + } else { |
46 | 48 |
|
47 |
| - }); |
48 |
| -} |
| 49 | + let packStr = ""; |
| 50 | + packs.forEach((e, i) => { |
49 | 51 |
|
50 |
| -// Download File From url |
51 |
| -var download = function (url, name){ |
| 52 | + packStr += e.name; |
| 53 | + if (i !== packs.length - 1) packStr += ", " |
52 | 54 |
|
53 |
| - console.log("\x1b[36m","Downloading "+name+".zip ...","\x1b[0m") |
| 55 | + }); |
54 | 56 |
|
55 |
| - let file = fs.createWriteStream(name + '.zip'); |
56 |
| - let request = http.get(url, function(response) { |
| 57 | + console.log("\x1b[31m", "Your pack was not found! Please use one of these: ", "\x1b[0m"); |
| 58 | + console.log("\x1b[36m", packStr, "\x1b[0m") |
57 | 59 |
|
58 |
| - response.pipe(file) |
59 |
| - response.on('end', () => { |
60 |
| - console.log("\x1b[32m","Download of "+name+".zip finished!","\x1b[0m") |
61 |
| - }); |
| 60 | + } |
| 61 | + }); |
62 | 62 |
|
63 |
| - }).on('error', function(e) { |
| 63 | + }).on('error', function (e) { |
64 | 64 |
|
65 |
| - console.log("\x1b[31m","Got error: " + e.message,"\x1b[0m"); |
| 65 | + console.log("\x1b[31m", "Got error: " + e.message, "\x1b[0m"); |
66 | 66 |
|
67 |
| - }); |
68 |
| -} |
| 67 | + }); |
| 68 | +}; |
69 | 69 |
|
70 | 70 | // Download File From Redirected-Url (in this programm mostly github.com)
|
71 | 71 | function downloadFromRedirectLink(url, file) {
|
72 | 72 |
|
73 |
| - http.get(url, function(res) { |
| 73 | + http.get(url, function (res) { |
74 | 74 |
|
75 |
| - if(res.statusCode != 302){ // No redirect |
| 75 | + if (res.statusCode !== 302) { // No redirect |
76 | 76 |
|
77 | 77 | console.log("error: no redirect found");
|
78 | 78 | return;
|
79 | 79 |
|
80 | 80 | }
|
81 | 81 |
|
82 |
| - download(res.headers.location,file); |
| 82 | + download(res.headers.location, file); |
83 | 83 |
|
84 |
| - }).on('error', function(e) { |
| 84 | + }).on('error', function (e) { |
85 | 85 |
|
86 |
| - console.log("\x1b[31m", "Got error: " + e.message,"\x1b[0m"); |
| 86 | + console.log("\x1b[31m", "Got error: " + e.message, "\x1b[0m"); |
87 | 87 |
|
88 | 88 | });
|
89 | 89 | }
|
0 commit comments