-
Notifications
You must be signed in to change notification settings - Fork 0
/
instaPubV1.js
74 lines (61 loc) · 2.6 KB
/
instaPubV1.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
const puppeteer = require('puppeteer');
const fs = require('fs');
const path = require('path');
async function scrapeInstagramImages(username) {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(`https://www.instagram.com/${username}/`, { waitUntil: 'networkidle2' });
await page.waitForSelector('article img', { timeout: 60000 });
const scrollToEnd = async () => {
let previousHeight;
let currentHeight = await page.evaluate('document.body.scrollHeight');
while (previousHeight !== currentHeight) {
previousHeight = currentHeight;
await page.evaluate('window.scrollTo(0, document.body.scrollHeight)');
await new Promise(resolve => setTimeout(resolve, 2000)); // Espera 2 segundos para carregar mais imagens
currentHeight = await page.evaluate('document.body.scrollHeight');
}
};
await scrollToEnd();
const imageUrls = await page.evaluate(() => {
const imgs = Array.from(document.querySelectorAll('article img'));
return imgs.map(img => img.src);
});
const downloadDir = path.join(__dirname,'downloads',username);
if (!fs.existsSync(downloadDir)) {
fs.mkdirSync(downloadDir);
}
// Baixa as imagens e salva os nomes dos arquivos em uma lista
const downloadedImages = [];
for (const [index, url] of imageUrls.entries()) {
const viewSource = await page.goto(url);
const imageName = `image${index}.jpg`;
const imagePath = path.join(downloadDir, imageName);
fs.writeFileSync(imagePath, await viewSource.buffer());
console.log(`Downloaded image ${index + 1} from ${url}`);
downloadedImages.push(imageName);
}
// Cria o objeto JSON
const jsonData = {
images: downloadedImages,
updatedAt: new Date().toISOString()
};
// Salva o objeto JSON em um arquivo
const jsonPath = path.join(downloadDir, 'instagram-images.json');
fs.writeFileSync(jsonPath, JSON.stringify(jsonData, null, 2));
console.log(`Saved image data to ${jsonPath}`);
await browser.close();
}
// Lê o arquivo config.json
const config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
// Itera sobre os nomes de usuário e executa a função para cada um
(async () => {
for (const username of config.usernames) {
console.log(`Scraping images for ${username}`);
try {
await scrapeInstagramImages(username);
} catch (error) {
console.error(`Error scraping images for ${username}:`, error);
}
}
})();