-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
246 lines (204 loc) · 7.22 KB
/
main.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
237
238
239
240
241
242
243
244
245
246
import config from "./config.js";
import puppeteer from "puppeteer";
import * as fs from "fs";
import * as path from "path";
const baseIpfsUrl = "https://ipfs.io/ipfs";
const {
ipfsMetadataSampleUrl,
collectionName,
firstEditionId,
lastEditionId,
collectionSize,
pageTimeout,
} = config;
let currentEdition = firstEditionId;
let metadataObject;
let errorCount = 0;
let currentEditionWithError = currentEdition;
let missingEditions = [];
async function getCollection() {
const { browser, page } = await initPuppeteer();
try {
while (currentEdition <= lastEditionId) {
page.setDefaultTimeout(pageTimeout);
const metadataFileName = `${currentEdition}.json`;
const metadataPath = generateFilePath(metadataFileName);
const existsJsonFile = fs.existsSync(metadataPath);
const metadataUrl = generateMetadataUrl();
await getMetadataFromIpfs(page, metadataUrl);
if (!existsJsonFile) {
saveMetadataFile(metadataPath);
}
if (metadataObject) {
const imageName = metadataObject.name;
const imageFormat = metadataObject.image.split(".").pop();
const imagePath = generateFilePath(`${imageName}.${imageFormat}`);
const existsImageFile = fs.existsSync(imagePath);
if (!existsImageFile) {
const imageUrl = metadataObject.image;
const ipfsImageUrl = generateIpfsImageUrl(imageUrl);
const imageBuffer = await getImagesFromIpfs(page, ipfsImageUrl);
saveImageFile(imagePath, imageBuffer);
}
}
currentEdition++;
const imagesCount = fs.readdirSync(`${collectionName}/images`).length;
if (imagesCount === collectionSize) {
await browser.close();
return console.log("Collection completely fetched!");
}
if (currentEdition > lastEditionId) {
console.log("Fetching missing files...");
await getMissingFiles(browser, page);
}
}
} catch (error) {
await browser.close();
const hasTooManyResquestsFailed = checkForError();
if (hasTooManyResquestsFailed) {
missingEditions.push(currentEdition);
console.log(
`Edition ${currentEdition} is taking too much time to load at IPFS, the script will get the rest of the collection and get back to missing ones later!`
);
currentEdition++;
}
setTimeout(() => {
getCollection();
}, 100);
}
}
function createFolders() {
if (!fs.existsSync(collectionName)) {
fs.mkdirSync(collectionName);
fs.mkdirSync(`${collectionName}/metadata`);
fs.mkdirSync(`${collectionName}/images`);
}
}
async function initPuppeteer() {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
const ua =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36";
await page.setExtraHTTPHeaders({ "Accept-Language": "en-US,en;q=0.9" });
await page.setUserAgent(ua);
return { browser, page };
}
function generateFilePath(fileName) {
if (fileName.includes(".json")) {
return path.resolve(collectionName, "metadata", fileName);
}
return path.resolve(collectionName, "images", fileName);
}
function generateMetadataUrl(missingEdition) {
const hasFileExtension = ipfsMetadataSampleUrl.includes(".json");
const barIndex = ipfsMetadataSampleUrl.lastIndexOf("/");
const baseMetadataUrl = ipfsMetadataSampleUrl.slice(0, barIndex);
const edition = missingEdition ?? currentEdition;
if (hasFileExtension) {
return `${baseMetadataUrl}/${edition}.json`;
}
return `${baseMetadataUrl}/${edition}`;
}
async function getMetadataFromIpfs(page, metadataUrl) {
await page.goto(metadataUrl);
await page.content();
metadataObject = await page.evaluate(() => {
return JSON.parse(document.querySelector("body").innerText);
});
}
async function getImagesFromIpfs(page, ipfsImageUrl) {
const response = await page.goto(`${ipfsImageUrl}`);
const imageBuffer = await response.buffer();
return imageBuffer;
}
function saveMetadataFile(metadataPath, missingEdition) {
fs.writeFileSync(metadataPath, JSON.stringify(metadataObject, null, 2));
const edition = missingEdition ?? currentEdition;
console.log(
`${collectionName} #${edition} metadata saved to ${metadataPath}`
);
}
function saveImageFile(imagePath, imageBuffer, missingEdition) {
const writeStream = fs.createWriteStream(imagePath);
const edition = missingEdition ?? currentEdition;
writeStream.write(imageBuffer);
console.log(`${collectionName} #${edition} image saved to ${imagePath}`);
}
const generateIpfsImageUrl = (imageUrl) => {
const imageUrlSplited = imageUrl.split("/");
const imageCID = imageUrlSplited[2];
let imageFormat;
if (imageUrlSplited[3]) {
imageFormat = imageUrlSplited[3].split(".")[1];
}
let ipfsImageUrl;
if (imageFormat) {
ipfsImageUrl = `${baseIpfsUrl}/${imageCID}/${currentEdition}.${imageFormat}`;
} else {
ipfsImageUrl = `${baseIpfsUrl}/${imageCID}`;
}
return ipfsImageUrl;
};
function checkForError() {
const maxErrorCount = 50;
if (currentEdition === currentEditionWithError) {
errorCount++;
if (errorCount >= maxErrorCount) {
return true;
}
}
currentEditionWithError = currentEdition;
}
async function getMissingFiles(browser, page) {
let editionIndex = 0;
try {
while (missingEditions.length !== 0) {
const missingEdition = missingEditions[editionIndex];
const metadataFileName = `${missingEdition}.json`;
const metadataPath = generateFilePath(metadataFileName);
const existsJsonFile = fs.existsSync(metadataPath);
const metadataUrl = generateMetadataUrl(missingEdition);
await getMetadataFromIpfs(page, metadataUrl);
if (!existsJsonFile) {
saveMetadataFile(metadataPath, missingEdition);
}
if (metadataObject) {
const imageName = metadataObject.name;
const imageFormat = metadataObject.image.split(".").pop();
const imagePath = generateFilePath(`${imageName}.${imageFormat}`);
const existsImageFile = fs.existsSync(imagePath);
if (!existsImageFile) {
const imageUrl = metadataObject.image;
const ipfsImageUrl = generateIpfsImageUrl(imageUrl);
const imageBuffer = await getImagesFromIpfs(page, ipfsImageUrl);
saveImageFile(imagePath, imageBuffer, missingEdition);
}
}
missingEditions = missingEditions.filter(
(element, index) => index !== editionIndex
);
if (editionIndex > missingEditions.length + 1) {
editionIndex = 0;
}
const imagesCount = fs.readdirSync(`${collectionName}/images`).length + 1;
if (imagesCount === collectionSize) {
await browser.close();
return console.log("Collection completely fetched!");
}
}
} catch (error) {
const hasTooManyResquestsFailed = checkForError();
if (hasTooManyResquestsFailed) {
console.log(
`Edition ${editionIndex} is taking too much time to load at IPFS, the script will get the rest of the collection and get back to missing ones later!`
);
editionIndex++;
}
setTimeout(() => {
getMissingFiles(browser, page);
}, 100);
}
}
createFolders();
console.log("Fetching files...");
getCollection();