-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-downloader.js
More file actions
242 lines (203 loc) · 8.35 KB
/
image-downloader.js
File metadata and controls
242 lines (203 loc) · 8.35 KB
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
const fs = require('fs');
const path = require('path');
const https = require('https');
const http = require('http');
class ImageDownloader {
constructor(downloadDir = './downloaded_images') {
this.downloadDir = downloadDir;
this.ensureDownloadDirectory();
}
ensureDownloadDirectory() {
if (!fs.existsSync(this.downloadDir)) {
fs.mkdirSync(this.downloadDir, { recursive: true });
console.log(`Created download directory: ${this.downloadDir}`);
}
}
getFileExtension(url) {
// Extract file extension from URL
const urlPath = new URL(url).pathname;
const ext = path.extname(urlPath).toLowerCase();
// Common image extensions
if (['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg'].includes(ext)) {
return ext;
}
// Default to .jpg if no extension found
return '.jpg';
}
generateFileName(adId, imageType, url) {
const extension = this.getFileExtension(url);
const timestamp = Date.now();
if (adId) {
return `${adId}_${imageType}_${timestamp}${extension}`;
} else {
return `unknown_${imageType}_${timestamp}${extension}`;
}
}
async downloadImage(url, fileName) {
return new Promise((resolve, reject) => {
const filePath = path.join(this.downloadDir, fileName);
const file = fs.createWriteStream(filePath);
// Choose http or https based on URL
const client = url.startsWith('https:') ? https : http;
console.log(`Downloading: ${fileName}`);
console.log(`From: ${url}`);
const request = client.get(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Accept': 'image/webp,image/apng,image/*,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'Referer': 'https://www.linkedin.com/'
}
}, (response) => {
// Check if request was successful
if (response.statusCode !== 200) {
reject(new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`));
return;
}
// Pipe the response to file
response.pipe(file);
file.on('finish', () => {
file.close();
console.log(`✓ Downloaded: ${fileName}`);
resolve({
fileName,
filePath,
size: fs.statSync(filePath).size,
url
});
});
file.on('error', (err) => {
fs.unlink(filePath, () => {}); // Delete incomplete file
reject(err);
});
});
request.on('error', (err) => {
reject(err);
});
// Set timeout
request.setTimeout(30000, () => {
request.destroy();
reject(new Error('Request timeout'));
});
});
}
async downloadAdImages(adData) {
const results = [];
console.log(`\nDownloading main ad image for Ad ID: ${adData.adId || 'unknown'}`);
console.log('='.repeat(50));
// Download main ad image only
if (adData.imageUrl) {
try {
const fileName = this.generateFileName(adData.adId, 'main_image', adData.imageUrl);
const result = await this.downloadImage(adData.imageUrl, fileName);
results.push({
type: 'main_image',
...result
});
} catch (error) {
console.error(`✗ Failed to download main image: ${error.message}`);
results.push({
type: 'main_image',
error: error.message,
url: adData.imageUrl
});
}
} else {
console.log('No main image URL found');
}
return results;
}
async downloadMultipleAds(adsData) {
const allResults = [];
console.log(`Starting bulk download for ${adsData.length} ads...`);
for (let i = 0; i < adsData.length; i++) {
const adData = adsData[i];
console.log(`\n[${i + 1}/${adsData.length}] Processing ad: ${adData.adId || 'unknown'}`);
try {
const results = await this.downloadAdImages(adData);
allResults.push({
adId: adData.adId,
results
});
// Add delay between downloads to be respectful
if (i < adsData.length - 1) {
console.log('Waiting 2 seconds before next download...');
await new Promise(resolve => setTimeout(resolve, 2000));
}
} catch (error) {
console.error(`Error processing ad ${adData.adId}: ${error.message}`);
allResults.push({
adId: adData.adId,
error: error.message
});
}
}
return allResults;
}
generateDownloadReport(results) {
const report = {
timestamp: new Date().toISOString(),
summary: {
totalAds: results.length,
successfulDownloads: 0,
failedDownloads: 0,
totalFiles: 0,
totalSize: 0
},
details: results
};
results.forEach(adResult => {
if (adResult.results) {
adResult.results.forEach(imageResult => {
if (imageResult.error) {
report.summary.failedDownloads++;
} else {
report.summary.successfulDownloads++;
report.summary.totalFiles++;
report.summary.totalSize += imageResult.size || 0;
}
});
}
});
// Save report to file
const reportPath = path.join(this.downloadDir, `download_report_${Date.now()}.json`);
fs.writeFileSync(reportPath, JSON.stringify(report, null, 2));
console.log('\n' + '='.repeat(60));
console.log('DOWNLOAD REPORT');
console.log('='.repeat(60));
console.log(`Total ads processed: ${report.summary.totalAds}`);
console.log(`Successful downloads: ${report.summary.successfulDownloads}`);
console.log(`Failed downloads: ${report.summary.failedDownloads}`);
console.log(`Total files downloaded: ${report.summary.totalFiles}`);
console.log(`Total size: ${(report.summary.totalSize / 1024 / 1024).toFixed(2)} MB`);
console.log(`Report saved to: ${reportPath}`);
console.log('='.repeat(60));
return report;
}
}
// Test with sample data from level 2 scraper
const sampleAdData = {
adId: '878430633',
headline: 'IT Leaders: Choose ERP with Data, Not Promises',
company: 'Priority Software',
imageUrl: 'https://media.licdn.com/dms/image/v2/D4D10AQEohKf0JouisQ/image-shrink_1280/B4DZk6RWkBJEAQ-/0/1757619252196/12001200_2png?e=2147483647&v=beta&t=jpu1UMiTKNd_rJQVfzwsl_fkfKCTglmZZ8kH1-bbk1E'
};
async function testImageDownloader() {
const downloader = new ImageDownloader();
try {
console.log('Testing Image Downloader with sample ad data...');
const results = await downloader.downloadAdImages(sampleAdData);
const report = downloader.generateDownloadReport([{ adId: sampleAdData.adId, results }]);
console.log('\nImage downloader test completed successfully!');
} catch (error) {
console.error('Image downloader test failed:', error);
process.exit(1);
}
}
// Export the class for use in other scripts
module.exports = ImageDownloader;
// Run test if this script is executed directly
if (require.main === module) {
testImageDownloader();
}