-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape-metadata.mjs
316 lines (266 loc) · 10.2 KB
/
scrape-metadata.mjs
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Importing modules using ESM syntax
import axios from 'axios';
import { JSDOM } from 'jsdom';
import { promises as fs } from 'fs'; // Node.js built-in fs promises API
import path from 'path';
import imageKit from 'imagekit';
import { fileTypeFromBuffer } from 'file-type';
import slugify from 'slugify';
import sharp from 'sharp';
import gifResize from '@gumlet/gif-resize'; // Import a library for resizing GIFs
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
const sleep = (milliseconds) => new Promise(resolve => setTimeout(resolve, milliseconds));
const MAX_RETRIES = 3;
const RETRY_DELAY = 2000; // 2 seconds
// Initialize ImageKit
const imagekit = new imageKit({
publicKey: "public_L3fJkv2yTfeiUv4DH5HtKrmlOAk=",
privateKey: "private_i5P3md55uud+u6D6LLqgooJE1hs=",
urlEndpoint: "https://ik.imagekit.io/UltraDAO/"
});
async function fetchMedia(uri) {
try {
let buffer;
let fileName = '';
if (uri.startsWith('ipfs://')) {
const ipfsUri = `https://ipfs.io/ipfs/${uri.slice(7)}`;
const response = await axios.get(ipfsUri, { responseType: 'arraybuffer' });
buffer = Buffer.from(response.data);
const fileType = await fileTypeFromBuffer(buffer);
const ipfsHash = uri.split('/')[2];
// Get image dimensions using sharp
const dimensions = await sharp(buffer).metadata();
const { width, height } = dimensions;
return {
buffer: buffer,
extension: fileType.ext,
fileName: ipfsHash,
dimensions: { width, height }
};
} else if (uri.startsWith('https://') || uri.startsWith('http://')) {
const response = await axios.get(uri, { responseType: 'arraybuffer' });
buffer = Buffer.from(response.data);
const fileType = await fileTypeFromBuffer(buffer);
fileName = path.basename(uri, `.${fileType.ext}`);
// Get image dimensions using sharp
const dimensions = await sharp(buffer).metadata();
const { width, height } = dimensions;
return {
buffer: buffer,
extension: fileType.ext,
fileName: fileName,
dimensions: { width, height }
};
} else {
throw new Error('Unsupported URI scheme');
}
} catch (error) {
console.error('Error fetching media:', error.message);
console.error(uri);
}
return null;
}
async function resizeImage(buffer) {
try {
const fileType = await fileTypeFromBuffer(buffer);
if (fileType.ext === 'gif') {
const resizedGifBuffer = await gifResize({
width: 2000,
height: 2000,
optimizationLevel: 2
})(buffer);
return resizedGifBuffer;
} else {
const resizedBuffer = await sharp(buffer)
.resize({
width: 2000,
height: 2000,
fit: sharp.fit.inside,
withoutEnlargement: true
})
.toBuffer();
return resizedBuffer;
}
} catch (error) {
console.error('Error resizing image:', error.message);
return null;
}
}
async function uploadToImageKit(media, fileBasename) {
const folderName = 'wallace_collection';
const requestBody = {
file: media,
fileName: fileBasename,
folder: folderName,
useUniqueFileName: false
};
console.log("Uploading media to ImageKit: ", requestBody.folder + "/" + requestBody.fileName);
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
try {
const response = await imagekit.upload(requestBody);
if (response && response.url) {
return response.url;
} else {
throw new Error('Invalid response from ImageKit');
}
} catch (error) {
if (attempt < MAX_RETRIES) {
console.log(`Retrying in ${RETRY_DELAY} milliseconds...`);
await sleep(RETRY_DELAY);
} else {
console.error('Max retries reached. Upload failed.');
return null;
}
}
}
}
async function checkExistsOnImageKit(fileName) {
const imageKitUri = `https://ik.imagekit.io/UltraDAO/wallace_collection/${fileName}`;
try {
const response = await axios.head(imageKitUri);
return response.status === 200;
} catch (error) {
if (error.response && error.response.status === 404) {
return false;
} else {
return false;
}
}
}
async function processMedia(ipfsUri, fileName) {
const mediaData = await fetchMedia(ipfsUri);
if (!mediaData) {
return null;
}
const fileBasename = slugify(fileName, {
replacement: '_',
remove: "#",
lower: true,
strict: true,
locale: 'en'
}) + '.' + mediaData.extension;
const exists = await checkExistsOnImageKit(fileBasename);
if (exists) {
console.log(`Media already exists on ImageKit: ${fileBasename}`);
return null;
}
const resizedBuffer = await resizeImage(mediaData.buffer);
const imageKitUri = await uploadToImageKit(resizedBuffer, fileBasename);
return {
uri: imageKitUri,
dimensions: mediaData.dimensions // Include dimensions
};
}
async function fetchMetadata(url) {
try {
const response = await axios.get(url);
return response.data;
} catch (error) {
console.error('Error fetching metadata:', error.message);
return null;
}
}
function normalizeAttributes(attributes) {
return attributes.map(attribute => {
if (attribute.name && !attribute.trait_type) {
attribute.trait_type = attribute.name;
delete attribute.name;
}
return attribute;
});
}
async function normalizeMetadata(metadata) {
const standardMetadata = {
name: '',
description: '',
artist: '',
image: '',
video: '',
display_uri: '',
website: '',
tags: [],
attributes: [],
};
// Map the fields from the scraped metadata to the standard structure
standardMetadata.name = metadata.name || '';
standardMetadata.tokenID = metadata.tokenID || metadata.tokenId || '';
standardMetadata.description = metadata.description || '';
standardMetadata.artist = metadata.artist || '';
standardMetadata.platform = metadata.platform || '';
if (metadata.symbol && metadata.symbol === 'GENTK')
standardMetadata.image = metadata.displayUri || metadata.image || metadata.preview_asset_url || metadata.artifactUri || metadata.thumbnailUri || '';
else
standardMetadata.image = metadata.image || metadata.preview_asset_url || metadata.artifactUri || metadata.displayUri || metadata.thumbnailUri || '';
standardMetadata.live_uri = metadata.live_url || metadata.artifactUri || metadata.generatorUri || metadata.primary_asset_url || '';
standardMetadata.tags = metadata.tags || '';
standardMetadata.website = metadata.website || '';
standardMetadata.attributes = Array.isArray(metadata.features || metadata.attributes || metadata.traits)
? normalizeAttributes(metadata.features || metadata.attributes || metadata.traits)
: [];
if (metadata.image) {
const mediaResult = await processMedia(metadata.image, metadata.name);
if (mediaResult && mediaResult.uri) {
metadata.image = mediaResult.uri;
metadata.dimensions = mediaResult.dimensions; // Include dimensions here
}
}
return standardMetadata;
}
async function scrapeAndStoreMetadata(filePath) {
const html = await fs.readFile(filePath, 'utf8');
const dom = new JSDOM(html);
const document = dom.window.document;
// Find all artist divs
const artistDivs = document.querySelectorAll('.collection-group');
const metadataArray = [];
for (const artistDiv of artistDivs) {
const artistElement = artistDiv.querySelector('.artist-title');
const artistName = artistElement ? artistElement.textContent.trim() : '';
const images = artistDiv.querySelectorAll('img[data-metadata]');
for (const img of images) {
const metadataUrl = img.getAttribute('data-metadata');
let fetchedMetadata = await fetchMetadata(metadataUrl);
if (fetchedMetadata) {
const normalizedMetadata = await normalizeMetadata(fetchedMetadata);
normalizedMetadata.artist = artistName;
// Create or update artist
const artist = await prisma.artist.upsert({
where: { name: normalizedMetadata.artist },
update: {},
create: { name: normalizedMetadata.artist }
});
// Create artwork
const artwork = await prisma.artwork.create({
data: {
title: normalizedMetadata.name,
description: normalizedMetadata.description,
artistId: artist.id,
// Add other artwork details
}
});
metadataArray.push(normalizedMetadata);
}
}
}
const jsonFileName = path.basename(filePath, '.html') + '.json';
const jsonFilePath = path.join('collection', jsonFileName);
if (metadataArray.length === 0) {
console.log(`No metadata found for ${filePath}, skipping file write.`);
} else {
await fs.writeFile(jsonFilePath, JSON.stringify(metadataArray, null, 2));
console.log(`Metadata saved to ${jsonFilePath}`);
}
}
async function processAllFiles() {
const dir = 'collection';
const files = await fs.readdir(dir);
for (const file of files) {
const filePath = path.join(dir, file);
if (path.extname(file) === '.html') {
await scrapeAndStoreMetadata(filePath);
}
}
await prisma.$disconnect();
}
processAllFiles();