-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.js
296 lines (244 loc) · 10.6 KB
/
scraper.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
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
const puppeteer = require('puppeteer');
const fs = require('fs');
const path = require('path');
// const cron = require('node-cron');
const TimeoutError = puppeteer.errors.TimeoutError;
// load env
require('dotenv').config();
const INSTAGRAM_URL = 'https://www.instagram.com';
const TARGET_USERNAME = 'taaruf.co.id';
const USERNAME = 'taarufin.official';
const PASSWORD = '[email protected]';
const CAPTIONS_FILE = 'storages/scraper_captions';
const POST_IDS_FILE = 'storages/scraper_ids.json';
const COOKIES_FILE = 'storages/scraper_cookies.json';
const USER_DIR = '.puppeteer/scraper_data'
const userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36';
// Fungsi untuk login ke Instagram
async function loginInstagram(page, username, password) {
// await page.goto(`${INSTAGRAM_URL}/accounts/login/`, { waitUntil: 'networkidle2' });
// await page.waitForSelector('input[name="username"]');
// await page.type('input[name="username"]', username);
// await page.type('input[name="password"]', password);
try {
await page.waitForSelector('input[aria-label="Phone number, username or email address"]');
await page.type('input[aria-label="Phone number, username or email address"]', username, { delay: Math.floor(Math.random() * 150) + 100 });
await page.type('input[aria-label="Password"]', password, { delay: Math.floor(Math.random() * 150) + 100 });
await page.click('button[type="submit"]');
// check if login has error message in 'form#loginForm > div + span > div' element textContent
await waitForTimeout();
const errorMessage = await page.evaluate(() => {
const errorElement = document.querySelector('form#loginForm > div + span > div');
return errorElement ? errorElement.textContent : null;
});
if (errorMessage) {
console.error('Login failed:', errorMessage);
return false;
}
} catch (error) {
console.error(error);
// if os linux
if (process.platform === 'linux') {
console.log('check login html', await page.content());
}
if (error instanceof TimeoutError) {
// check apakah kita dibawah ke halaman /challenge
const pathname = await page.evaluate(() => window.location.pathname);
console.log('window.location.pathname:', pathname);
const isChallenged = pathname === '/challenge' || pathname === '/challenge/';
console.log('isChallenged:', isChallenged);
if (isChallenged) {
console.log('Challenge page detected!');
// await waitForTimeout(5000);
// click 'Dismiss' button in div[data-bloks-name="bk.components.Flexbox"][role="button"]
await page.click('div[data-bloks-name="bk.components.Flexbox"][role="button"]');
await waitForTimeout();
return true;
}
} else {
console.error(error)
}
return false
}
return true;
}
// Fungsi untuk mengambil ID dan caption dari 9 postingan terbaru
async function getLatestPosts(page, targetUsername) {
await page.goto(`${INSTAGRAM_URL}/${targetUsername}/`, { waitUntil: 'networkidle2' });
await page.waitForSelector('a img');
const posts = await page.evaluate(() => {
const nodes = Array.from(document.querySelectorAll('a img'));
const fetchTotal = 12; // get X total latest posts
return nodes.slice(1, fetchTotal + 1).map(node => {
const parent = node.closest('a');
const hrefArray = parent.href.split('/');
if (!hrefArray.length) {
return null;
}
const postId = hrefArray[hrefArray.length - 2];
const postType = hrefArray[hrefArray.length - 3];
const caption = node.alt;
return { postId, postType, caption };
}).filter(v => v !== null);
});
return posts;
}
// Fungsi untuk memeriksa apakah pengguna sudah login
async function isUserLoggedIn(page) {
// get cookies
console.log('Checking cookies...');
if (fs.existsSync(COOKIES_FILE)) {
const cookies = fs.readFileSync(COOKIES_FILE, 'utf-8');
const cookiesArr = JSON.parse(cookies);
console.log('Setting cookies...');
await page.setCookie(...cookiesArr);
}
// await page.goto(INSTAGRAM_URL, { waitUntil: 'networkidle2' });
await page.goto(`${INSTAGRAM_URL}/accounts/login/`, { waitUntil: 'networkidle2' });
try {
await page.waitForSelector('svg[aria-label="Settings"]', { timeout: 5000 });
return true;
} catch (error) {
return false;
}
}
async function waitForTimeout(timeout = null) {
const randomTime = timeout || Math.floor(Math.random() * (10000 - 5000 + 1)) + 5000;
console.log('Waiting for', randomTime, 'ms...');
return await new Promise(resolve => setTimeout(resolve, randomTime));
}
// Fungsi utama untuk mengelola proses
async function main() {
const tempDir = path.resolve(USER_DIR);
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir, { recursive: true });
// set permission
fs.chmodSync(tempDir, 0o777);
}
// get puppeter executable path from node_modules
let executablePath = process.env.PUPPETEER_EXECUTABLE_PATH || puppeteer.executablePath();
const appPath = path.resolve('./');
// console.log('App path:', appPath);
if (process.env.PUPPETEER_EXECUTABLE_PATH) {
// if not leading slash
if (!process.env.PUPPETEER_EXECUTABLE_PATH.startsWith('/')) {
// remove leading slash and ending slash
executablePath = path.resolve(appPath, process.env.PUPPETEER_EXECUTABLE_PATH.replace(/\/$/, ''))
} else {
executablePath = process.env.PUPPETEER_EXECUTABLE_PATH;
}
}
console.log('Puppeteer executable path:', executablePath);
console.log('Launching browser...');
const browser = await puppeteer.launch({
headless: process.env.PUPPETEER_HEADLESS === 'true' ? true : false,
// args: ['--no-sandbox', '--disable-setuid-sandbox'],
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--no-first-run',
'--no-zygote',
'--single-process', // <- this one doesn't works in Windows
'--disable-gpu'
],
userDataDir: tempDir,
executablePath,
});
console.log('Browser launched!');
const page = await browser.newPage();
// Set the user agent and disable the webdriver flag
await page.setUserAgent(userAgent);
await page.evaluateOnNewDocument(() => {
// Object.defineProperty(navigator, 'webdriver', {
// get: () => undefined
// });
delete navigator.__proto__.webdriver;
}
);
try {
console.log('Checking if user is logged in...');
const loggedIn = await isUserLoggedIn(page);
if (!loggedIn) {
console.log('Logging in...');
if (await loginInstagram(page, USERNAME, PASSWORD) === false) {
// console.error('Login failed!');
throw new Error('Login failed!');
}
console.log('Go to Instagram home page...');
await page.waitForNavigation({ waitUntil: 'networkidle2' }, { timeout: 60000 });
// Wait for user to login
await page.waitForSelector('svg[aria-label="Settings"]');
console.log('Login success!');
// Save cookies
const cookies = await page.cookies();
console.log('Saving cookies...');
fs.writeFileSync(COOKIES_FILE, JSON.stringify(cookies, null, 2));
}
console.log('Getting latest posts...');
const currentDateStr = new Date().toISOString().split('T')[0]; // YYYY-MM-DD
// Baca ID postingan yang sudah diambil sebelumnya
let savedPostIds = [];
if (fs.existsSync(POST_IDS_FILE)) {
try {
savedPostIds = JSON.parse(fs.readFileSync(POST_IDS_FILE, 'utf-8'));
} catch (error) {
// savedPostIds = [];
savedPostIds = {};
}
}
await waitForTimeout();
const posts = await getLatestPosts(page, TARGET_USERNAME);
// console.log('Posts:', posts);
let newCaptions = [];
let newPostIds = [];
posts.forEach(post => {
// if (!savedPostIds.includes(post.postId)) {
if (!savedPostIds[currentDateStr]) {
savedPostIds[currentDateStr] = [];
}
// check in all dates
let isAlreadySaved = false;
for (const date in savedPostIds) {
if (savedPostIds[date].includes(post.postId)) {
isAlreadySaved = true;
break;
}
}
// if (!savedPostIzds[currentDateStr].includes(post.postId)) {
if (!isAlreadySaved) {
// Skip caption yang tidak ada kata-kata "Nama:" atau "Nama :" atau "Jns kelamin:" atau "Jns kelamin :"
if (!post.caption.match(/Nama\s*:/) && !post.caption.match(/Jns kelamin\s*:/)) {
return;
}
// newCaptions.push(post.caption);
const headerTitle = `=====${TARGET_USERNAME}/${post.postType}/${post.postId}=====`;
newCaptions.push(headerTitle + '\n' + post.caption);
newPostIds.push(post.postId);
}
});
console.log('New post found:', newCaptions.length);
if (newCaptions.length > 0) {
console.log('Saving captions...');
const captionsText = newCaptions.join('\n\n');
// fs.writeFileSync(CAPTIONS_FILE, captionsText, { flag: 'a' });
fs.writeFileSync(CAPTIONS_FILE + `_${currentDateStr}.txt`, captionsText);
// console.log('Captions saved:', newCaptions);
console.log('Captions saved!');
// Simpan ID postingan baru
// savedPostIds = savedPostIds.concat(newPostIds);
savedPostIds[currentDateStr] = savedPostIds[currentDateStr].concat(newPostIds);
fs.writeFileSync(POST_IDS_FILE, JSON.stringify(savedPostIds, null, 2));
}
} catch (error) {
console.error(error);
} finally {
console.log('Closing browser...');
await browser.close();
console.log('Browser closed!');
}
}
// Jadwalkan tugas untuk dijalankan setiap 1 jam
// cron.schedule('0 * * * *', main);
main();