-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalbget.js
124 lines (101 loc) · 3.7 KB
/
albget.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
/* Node dependencies */
var request = require('request'); //For calling the url.
var fs = require('file-system'); //For saving images and creating directories.
/* Imgur API client id, plz no steal */
var clientID = "3752a956b4926c9";
/* For checking directory name */
var unallowChar = "><:\"\\/|?*\0"; //List of unnallowed characters.
//Get the argument count.
var args = process.argv.splice(2, 4); //Get rid of the first two elements as those aren't needed.
var customPath = args.length > 1;
var pathArg
if (customPath) {
pathArg = args[1];
}
//Testing for usage.
if (args.length < 1 || args.length > 2) {
failExit("Incorrect usage! See README for usage information.")
}
var urlImages = "https://api.imgur.com/3/album/" + getAlbumHash(args[0]); //Get the url argument for the images themselves.
var optionsImages = {
url: urlImages,
headers: {
'authorization': 'Client-ID ' + clientID
}
}
/**
* @desc Get the hash from an album by splitting the url.
* @param {string} url the url to get the album has from.
* @return {string} the album hash.
*/
function getAlbumHash(url) {
return url.split("/")[url.split("/").length - 1]
}
/**
* @desc Parse the images for important information to get the title and links from them.
* @param {error} err passing an error if the images are invalid.
* @param {string} resp the response from the HTTP get req.
* @param {string} body the original body from the req.
*/
function parseImages(err, resp, body) {
if (err != null) {
failExit(err);
}
var imgTitles = [];
var imgLinks = [];
JSON.parse(body, (key, val) => {
if (key == "title") {
imgTitles.push(val); //Push titles to array.
}
if (key == "link") {
imgLinks.push(val); //Push links to array.
}
});
if (!customPath) {
//The title to the album will always be the first element.
var dirTitle = removeSpecialCharacters(imgTitles[0]); //Remove unsuable directory characters.
fs.mkdir(dirTitle); //Create the directory for the folders.
} else {
var dirTitle = pathArg; //Just set that to the directory.
}
imgLinks = imgLinks.slice(1, imgLinks.length); //Get rid of the album elements.
//Loop through all
for (image = 0; image < imgLinks.length; image++) {
var filename = imgLinks[image].split("/")[3];
download(imgLinks[image], filename, dirTitle); //Send a request to download them.
}
console.log("Album is located at --> '" + dirTitle + "'");
}
/**
* @desc Remove special characters from the directory name to prevent invalid naming.
* @param {string} dir the directory name.
* @return {string} the directory name without invalid characters.
*/
function removeSpecialCharacters(dir) {
var newDir = ""; //The newDir with removed invalid characters.
for (letter = 0; letter < dir.length; letter++) {
if (unallowChar.indexOf(dir.charAt(letter)) === -1) { //If the char is valid.
newDir += dir.charAt(letter); //If the character is valid add it to the newDir.
}
}
return newDir;
}
/**
* @desc Sends an HTTP get request and pipes it to a write stream.
* @param {string} link the link we're sending the req to.
* @param {string} filename the filename we're writing to.
* @param {string} dir the drectory name we're writing to.
*/
function download(link, filename, dir) {
request(link).pipe(fs.createWriteStream(dir + "/" + filename));
}
/**
* @desc Fail the program and display why.
* @param {err/string} why the reason the program failed if a reason is given.
*/
function failExit(why) {
console.log(why);
process.exit(1);
}
/* Send the requests to Imgur API */
request(optionsImages, parseImages);