-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnft_image_downloader.html
127 lines (111 loc) · 4.28 KB
/
nft_image_downloader.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>NFT Image Downloader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="NFT Image Downloader for MultiversX">
<meta name="keywords" content="NFT, Image Downloader, MultiversX">
<meta name="author" content="NFTools">
<link rel="stylesheet" href="assets/css/main.css">
<style>
button {
background-color: #333;
color: white;
padding: 10px 20px;
border: none;
cursor: pointer;
margin-top: 20px;
}
button:hover {
background-color: #555;
}
.container {
width: 80%;
margin: auto;
padding: 20px;
}
label,
input {
display: block;
width: 100%;
margin-bottom: 10px;
}
input[type="text"] {
padding: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>NFT Image Downloader</h1>
<form id="nftForm">
<label for="erd">Enter ERD:</label>
<input type="text" id="erd" name="erd" required>
<label for="collection">Enter Collection Name:</label>
<input type="text" id="collection" name="collection" required>
<button type="submit">Download Images</button>
</form>
</div>
<script>
async function get_png_from_nft(erd, collection) {
try {
const response = await fetch(`https://api.multiversx.com/accounts/${erd}/nfts?size=123&collections=${collection}`);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const nfts = await response.json();
for (let item of nfts) {
const img_url = item.media[0].url;
const imgResponse = await fetch(img_url);
if (!imgResponse.ok) {
throw new Error(`Image HTTP error! Status: ${imgResponse.status}`);
}
const blob = await imgResponse.blob();
await processImage(blob, img_url);
}
} catch (error) {
console.error('Error fetching NFTs:', error);
}
}
async function processImage(blob, img_url) {
const img = new Image();
img.onload = async () => {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
canvas.toBlob(async (blob) => {
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = determineFileName(img_url);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}, 'image/png');
};
img.src = URL.createObjectURL(blob);
}
function determineFileName(img_url) {
// Logic to determine file name based on img_url length
// This needs to be adapted based on the actual URL structure
if (img_url.length === 88) return img_url.slice(-5);
if (img_url.length === 89) return img_url.slice(-6);
if (img_url.length === 90) return img_url.slice(-7);
if (img_url.length === 91) return img_url.slice(-8);
return 'unknown.png';
}
// Example call
// get_png_from_nft('erd1xuf43l9v4d3lxhfg9ehzh244592gf2an7hmuw9h84gma7j8fdsws60rrqn', 'MRG-1c3ba4');
document.getElementById("nftForm").addEventListener("submit", async function(event) {
event.preventDefault();
const erd = document.getElementById("erd").value;
const collection = document.getElementById("collection").value;
await get_png_from_nft(erd, collection);
});
</script>
<!-- Other scripts and footer -->
</body>
</html>