-
Notifications
You must be signed in to change notification settings - Fork 2
/
scrapper.js
320 lines (222 loc) · 9.35 KB
/
scrapper.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
const puppeteer = require('puppeteer')
const InstagramGetPosts = require('instagram-get-posts');
const mongoose = require('mongoose')
const Travel = require('./src/models/travel')
const Bulldog = require('./src/models/bulldog')
const Frenchie = require('./src/models/frenchies')
const { COOKIE_ACCOUNT_PASSWORD,USER_AGENT, COOKIE_ACCOUNT_USERNAME } = process.env;
let cookie = ""
let cookies = ""
let app_id = ""
const login = async () => {
const ig_url = 'https://www.instagram.com'
const puppeteer_options = { headless: false , ignoreHTTPSErrors: true}
const browser = await puppeteer.launch(puppeteer_options);
const page = await browser.newPage()
await page.goto("https://www.instagram.com/");
await page.setDefaultTimeout(0);
await page.waitForSelector("input[name='username']");
await page.waitForTimeout(2500);
// Get the inputs on the page
let usernameInput = await page.$("input[name='username']");
let passwordInput = await page.$("input[name='password']");
// Type the username in the username input
await usernameInput.click();
await page.keyboard.type(COOKIE_ACCOUNT_USERNAME, { delay :100 });
// Type the password in the password input
await passwordInput.click();
await page.keyboard.type(COOKIE_ACCOUNT_PASSWORD, { delay :100 });
await page.waitForTimeout(800);
// Click the login button
await page.click('#loginForm button[type="submit"]');
await page.waitForTimeout(1000);
let error_login = await page.evaluate(() => ((document.querySelector('p[data-testid="login-error-message"]'))?.innerText));
if (error_login != undefined) {
console.log('Error', error_login);
await browser.close();
process.exit(500);
}
await page.waitForNavigation();
await page.goto(ig_url + '/' + COOKIE_ACCOUNT_USERNAME);
await page.waitForSelector(`a[href='/${COOKIE_ACCOUNT_USERNAME}/followers/'`);
cookies = await page.cookies();
let csrf_token = cookies.filter((el) => (el.name === 'csrftoken') )[0].value;
let mid = cookies.filter((el ) => (el.name === 'mid') );
let ig_did = cookies.filter((el ) => (el.name === 'ig_did') );
let ig_nrcb = cookies.filter((el ) => (el.name === 'ig_nrcb') );
let ds_user_id = cookies.filter((el ) => (el.name === 'ds_user_id'));
let rur = cookies.filter((el ) => (el.name === 'rur'));
let datr = cookies.filter((el ) => (el.name === 'datr'));
let shbid = cookies.filter((el ) => (el.name === 'shbid'));
let shbts = cookies.filter((el ) => (el.name === 'shbts'));
let sessionid = cookies.filter((el ) => (el.name === 'sessionid'));
cookie = `mid=${mid[0].value}; ig_did=${ig_did[0].value}; ig_nrcb=${ig_nrcb[0].value}; csrftoken=${csrf_token}; ds_user_id=${ds_user_id[0].value}; datr=${datr[0].value}; shbid=${shbid[0].value}; shbts=${shbts[0].value}; sessionid=${sessionid[0].value}; rur=${rur[0].value}`
let script_appId = await page.evaluate(() => {
let scripts = Array.from(document.querySelectorAll('script'));
let text = '';
scripts.forEach(script => {
if(script.innerText.includes('appId')) text = script.innerText;
});
return text;
});
let regx = /("appId":"\d*")/gm;
let matchs = regx.exec(script_appId);
app_id = matchs[0].split(':')[1].replace('"', '').replace('"', '');
}
const travelScrapper = async () => {
try {
const instagramGetPosts = new InstagramGetPosts({
'cookie': cookie,
'user-agent': USER_AGENT,
'x-ig-app-id' :app_id
});
const accounts = [
'travel.heavenss',
'visit.the.america',
'explorearizona',
'hikingbangers',
'maldiverss',
'traveltobeautifulplacess',
'belovedtravelz',
'wonderofmaldives',
'bestbeachspot',
'travelingstraight',
'explorearizona',
'backpackerdestination',
'travel.elegance'
]
accounts.map(async account => {
try {
setTimeout(() => {}, 4000);
let posts = await instagramGetPosts.getPosts({
profile: account, // profile from url instagram (https//www.instagram.com/rimemberchile/?hl=es-la)
maxPosts : 4,
postsType :'all', // type of posts to get, possible values are image, video, all
getBase64 :true // indicates if you want to get the image as base64
})
let newPostArr = posts?.map((post) => {
let linksArr = []
if (post.type === "video") {
linksArr = [post?.videoUrl]
}
let postReturn = {
type: post.type,
likes: post.likes,
caption: post.caption,
comments: post.comments,
links: linksArr
}
return postReturn
})
await Travel.create([...newPostArr])
} catch (error) {
}
})
} catch (error) {
console.log(error);
}
}
const bulldogScrapper = async () => {
try {
const instagramGetPosts = new InstagramGetPosts({
'cookie': cookie,
'user-agent': USER_AGENT,
'x-ig-app-id' :app_id
});
const accounts = [
'bulldogusapage',
'english_bulldog_loversofficial',
'trainingbulldog_world',
'english_bulldoglovers_',
'bulldogtoyal',
'bulldogwon'
]
accounts.map(async account => {
try {
setTimeout(() => {}, 4000);
let posts = await instagramGetPosts.getPosts({
profile: account, // profile from url instagram (https//www.instagram.com/rimemberchile/?hl=es-la)
maxPosts :5,
postsType :'video', // type of posts to get, possible values are image, video, all
getBase64 :true // indicates if you want to get the image as base64
})
let newPostArr = posts?.map((post) => {
let linksArr = []
if (post.type === "video") {
linksArr = [post?.videoUrl]
}
let postReturn = {
type: post.type,
likes: post.likes,
caption: post.caption,
comments: post.comments,
links: linksArr
}
return postReturn
})
await Bulldog.create([...newPostArr])
} catch (error) {
}
})
} catch (error) {
console.log(error);
}
}
const frenchieScrapper = async () => {
try {
const instagramGetPosts = new InstagramGetPosts({
'cookie': cookie,
'user-agent': USER_AGENT,
'x-ig-app-id' :app_id
});
const accounts = [
'frenchbulldoglovers6589',
'frenchieclubs',
]
accounts.map(async account => {
try {
setTimeout(() => {}, 4000);
let posts = await instagramGetPosts.getPosts({
profile: account, // profile from url instagram (https//www.instagram.com/rimemberchile/?hl=es-la)
maxPosts :10,
postsType :'all', // type of posts to get, possible values are image, video, all
getBase64 :true // indicates if you want to get the image as base64
})
let newPostArr = posts?.map((post) => {
let linksArr = []
if (post.type === "video") {
linksArr = [post?.videoUrl]
} else {
if (post?.imageUrl) {
linksArr = [post?.imageUrl]
} else {
linksArr = post?.images
}
}
let postReturn = {
type: post.type,
likes: post.likes,
caption: post.caption,
comments: post.comments,
links: linksArr
}
return postReturn
})
await Frenchie.create([...newPostArr])
} catch (error) {
}
})
} catch (error) {
console.log(error);
}
}
const scrap = async ()=>{
try {
await login();
await travelScrapper();
await frenchieScrapper();
await bulldogScrapper();
} catch (error) {
}
}
module.exports = scrap