-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.cjs
159 lines (121 loc) · 3.98 KB
/
app.cjs
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
/*
Node Dependecies */
const fetch = require("node-fetch");
const fs = require("fs");
const request = require("request");
const readline = require("readline").createInterface({ input: process.stdin, output: process.stdout});
require("dotenv").config();
/*
Constants*/
const folder_name_Regexp = new RegExp("^([a-zA-Z0-9][^*/><?|:&]*)$");
const collection_default_page = 1;
const unsplash_token = process.env.UNSPLASH_TOKEN;
/*
Variables */
let photos_couter=0;
let photos_total=0;
/*
Utitlity function*/
/*
Function to download and wirte Images*/
var downloads = function (uri, filename, callback) {
request.head(uri, function (err, res, body) {
// console.log("content-type:", res.headers["content-type"]);
// console.log("content-length:", res.headers["content-length"]);
request(uri).pipe(fs.createWriteStream(filename)).on("close", callback);
});
};
/*
Function to download and wirte Images*/
function validateFileName(filename) {
if (filename && folder_name_Regexp.test(filename)) {
filename.substr(0, 150);
} else {
filename = Math.random().toString(16).slice(2, 34) + Math.random().toString(16).slice(2, 18) ;
}
return filename
}
/*
Function to get The user input*/
function getUserInput(message,callback) {
readline.question(message,(input) =>{
console.log("Output: ", input);
readline.close();
callback(input);
return
});
}
/*
Function to get The user input*/
async function collection_fetch(collection_id) {
let folder_name = "images/";
let collection_data;
let collection_total_photos;
//if (!collection_id || isNaN(collection_id)) {
// getUserInput("Please Enter a Valid input");
if (!collection_id) {
console.log(">> invalid user input");
return
}
collection_data = await fetch_data(`https://api.unsplash.com/collections/${collection_id}`);
collection_total_photos = parseInt(collection_data.total_photos);
collection_photos_url= new URL(collection_data.links.photos);
folder_name+=collection_data.id+" - "+ validateFileName(collection_data.title);
create_folder(folder_name);
/*
Fetch photos*/
if (collection_total_photos>30) {
let collection_images_pages =10 //parseInt(collection_total_photos / 30);
collection_images_pages = collection_images_pages <= 1 ? 2 : collection_images_pages;
// getUserInput("Hey there",)
for (let i = 1; i <= collection_images_pages; i++) {
photos_array = await fetch_data(collection_photos_url, i);
photos_save(photos_array, folder_name);
}
console.log(collection_images_pages);
}else{
photos_array = await fetch_data(collection_photos_url, 1);
photos_save(photos_array, folder_name);
}
console.log("Total Of Photos to Download "+collection_total_photos);
}
async function fetch_data(uri, page_nr = 1) {
let url = new URL(uri);
url.searchParams.set(
"client_id",
unsplash_token
);
url.searchParams.set("per_page", "30");
url.searchParams.set("page", page_nr);
const post_data = await fetch(url);
const posts = await post_data.json();
return posts;
}
function photos_save(images_array, folder) {
//Loop to all Photos
images_array.forEach((element, index, array) => {
let title = element.alt_description;
title = validateFileName(title);
let blob_URL = "" + element.urls.raw;
//Write Every single Photo
downloads(blob_URL, `${folder}/${title}` + ".jpeg", function () {
console.log("///////////////////////////////////");
console.log(">> Photo Number: "+ (++photos_couter));
console.log(">> Title: "+ title);
console.log(">>> Location: " +element.user.location)
console.log(">>>> Photo By "+element.user.name);
console.log(">>>>>> Downloaded \n");
});
});
}
function create_folder(name) {
fs.mkdir("./" + name, function (err) {
if (err) {
return;
} else {
console.log("New directory successfully created.");
}
});
}
/*Tests*/
getUserInput("\n >>> Hey, what is the number of the collection? ",collection_fetch)