-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.ts
662 lines (586 loc) · 20.6 KB
/
main.ts
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
import {
exportToCsv,
ListStorage,
UIContainer,
createCta,
createSpacer,
createTextSpan,
HistoryTracker,
randomString
} from 'browser-scraping-utils';
interface InstaMember {
profileId: string
pictureUrl: string
username: string
fullName: string
isPrivate: boolean
location?: string
source?: string
}
class FBStorage extends ListStorage<InstaMember> {
get headers() {
return [
'Profile Id',
'Username',
'Link',
'Full Name',
'Is Private',
'Location',
'Picture Url',
'Source'
]
}
itemToRow(item: InstaMember): string[]{
const link = `https://www.instagram.com/${item.username}`
let isPrivateClean: string = "";
if(typeof(item.isPrivate)==="boolean"){
isPrivateClean = item.isPrivate ? "true" : "false"
}
return [
item.profileId,
item.username,
link,
item.fullName,
isPrivateClean,
item.location ? item.location : "",
item.pictureUrl,
item.source ? item.source : ""
]
}
}
const memberListStore = new FBStorage({
name: "insta-scrape"
});
const counterId = 'scraper-number-tracker'
const exportName = 'instaExport';
let logsTracker: HistoryTracker;
async function updateConter(){
// Update member tracker counter
const tracker = document.getElementById(counterId)
if(tracker){
const countValue = await memberListStore.getCount();
tracker.textContent = countValue.toString()
}
}
const uiWidget = new UIContainer();
function buildCTABtns(){
// History Tracker
logsTracker = new HistoryTracker({
onDelete: async (groupId: string) => {
// We dont have cancellable adds for now
console.log(`Delete ${groupId}`);
await memberListStore.deleteFromGroupId(groupId);
await updateConter();
},
divContainer: uiWidget.history,
maxLogs: 4
})
// Button Download
const btnDownload = createCta();
btnDownload.appendChild(createTextSpan('Download\u00A0'))
btnDownload.appendChild(createTextSpan('0', {
bold: true,
idAttribute: counterId
}))
btnDownload.appendChild(createTextSpan('\u00A0users'))
btnDownload.addEventListener('click', async function() {
const timestamp = new Date().toISOString()
const data = await memberListStore.toCsvData()
try{
exportToCsv(`${exportName}-${timestamp}.csv`, data)
}catch(err){
console.error('Error while generating export');
// @ts-ignore
console.log(err.stack)
}
});
uiWidget.addCta(btnDownload)
// Spacer
uiWidget.addCta(createSpacer())
// Button Reinit
const btnReinit = createCta();
btnReinit.appendChild(createTextSpan('Reset'))
btnReinit.addEventListener('click', async function() {
await memberListStore.clear();
logsTracker.cleanLogs();
await updateConter();
});
uiWidget.addCta(btnReinit);
// Render
uiWidget.render()
// Initial
window.setTimeout(()=>{
updateConter()
}, 1000)
}
let sourceGlobal: string | null = null;
function processResponseUsers(
dataGraphQL: any,
source?: string
): void{
let data: any[];
if(dataGraphQL?.users){ // Followings/Followers
data = dataGraphQL.users
}else{
// return fast otherwise
return;
}
const membersData = data.map((node)=>{
// User Data
const {
pk,
username,
full_name,
is_private,
profile_pic_url
} = node;
const result: InstaMember = {
profileId: pk,
username: username,
fullName: full_name,
source: source,
isPrivate: is_private,
pictureUrl: profile_pic_url
}
return result
})
const toAdd: [string, InstaMember][] = []
membersData.forEach(memberData=>{
if(memberData){
toAdd.push([memberData.profileId, memberData])
}
})
const groupId = randomString(10)
memberListStore.addElems(toAdd, false, groupId).then((added)=>{
updateConter();
logsTracker.addHistoryLog({
label: source ? `Added ${source}` : 'Added items',
numberItems: added,
groupId: groupId,
cancellable: false
})
})
}
const locationNameCache: {[key: string]: string} = {}
function saveLocationName(locationId: string, locationName: string){
locationNameCache[locationId] = locationName;
}
function sourceString(
sourceType: 'location' | 'tag' | 'followers' | 'following',
value?: string
){
switch(sourceType){
case 'location':
if(value){
if(locationNameCache[value]){
return `post authors (loc: ${locationNameCache[value]})`
}else if(typeof(value)==="string" && value.startsWith('%23')){
return `post authors (loc: ${value.replace('%23', '')})`
}else{
return `post authors (loc: ${value})`
}
}else{
return `post authors`
}
case 'tag':
if(value){
let valueClean = value;
if(typeof(value)==="string" && value.startsWith('%23')){
valueClean = value.replace('%23', '');
}
return `post authors #${valueClean}`
}else{
return `post authors`
}
case 'followers':
return `followers of ${value}`
case 'following':
return `following of ${value}`
}
}
function processResponse(dataGraphQL: any, source?: string): void{
// Only look for GraphQL responses
let data: any[];
let sourceImproved: string | null = null;
if(dataGraphQL?.data){ // Tags
sourceGlobal = dataGraphQL?.data?.name;
data = []
if(dataGraphQL?.data?.recent?.sections){
data.push(...dataGraphQL?.data?.recent?.sections)
}
if(dataGraphQL?.data?.top?.sections){
data.push(...dataGraphQL?.data?.top?.sections)
}
if(dataGraphQL?.data?.xdt_location_get_web_info_tab?.edges){
data.push(...dataGraphQL?.data?.xdt_location_get_web_info_tab?.edges)
}
} else if(dataGraphQL?.media_grid?.sections){ // Place/Tag
data = dataGraphQL?.media_grid?.sections;
} else if(dataGraphQL?.native_location_data){ // Place
if(dataGraphQL?.native_location_data?.location_info?.name){
const locationName = dataGraphQL.native_location_data.location_info.name
saveLocationName(
dataGraphQL?.native_location_data?.location_info?.location_id,
locationName
)
sourceImproved = sourceString(
"location",
dataGraphQL?.native_location_data?.location_info?.location_id
)
sourceGlobal = sourceImproved;
}
data = []
if(dataGraphQL?.native_location_data?.ranked?.sections){
data.push(...dataGraphQL?.native_location_data?.ranked?.sections)
}
if(dataGraphQL?.native_location_data?.recent?.sections){
data.push(...dataGraphQL?.native_location_data?.recent?.sections)
}
} else if(dataGraphQL?.sections){ // Load more in places, use previous source
data = dataGraphQL?.sections;
} else {
// return fast otherwise
return;
}
const toCheck: any[] = [];
data.forEach(sectionNode=>{
const mediaNodes = sectionNode?.layout_content?.medias;
if(mediaNodes && mediaNodes.length>0){
toCheck.push(...mediaNodes)
}
const mediaNodes2 = sectionNode?.layout_content?.fill_items;
if(mediaNodes2 && mediaNodes2.length>0){
toCheck.push(...mediaNodes2)
}
if(sectionNode?.node){
toCheck.push(sectionNode?.node)
}
});
if(toCheck.length===0){
return;
}
let sourceClean = sourceImproved || source || sourceGlobal;
const membersData = toCheck.map((node)=>{
let media = node?.media;
if(!media && node['__typename'] == "XDTMediaDict"){
media = node;
}
if(!media){
return null
}
const owner = media?.owner;
if(!owner){
return null;
}
// User Data
const {
pk,
username,
full_name,
is_private,
profile_pic_url
} = owner;
// Add Location info
let location: string | null = null;
if(media?.location?.name){
location = media?.location?.name;
}
const result: InstaMember = {
profileId: pk,
username: username,
fullName: full_name,
isPrivate: is_private,
pictureUrl: profile_pic_url
}
if(result.isPrivate == null){
if(media?.user){
// Get info from user is not available (for Explore section)
if(typeof(media.user['is_private'])!=="undefined"){
result['isPrivate'] = media.user['is_private']
}
}
}
if(location){
result.location = location
}
if(sourceClean){
result.source = sourceClean;
}
return result
})
const toAdd: [string, InstaMember][] = []
membersData.forEach(memberData=>{
if(memberData){
toAdd.push([memberData.profileId, memberData])
}
})
const groupId = randomString(10)
memberListStore.addElems(toAdd, false, groupId).then((added)=>{
updateConter();
logsTracker.addHistoryLog({
label: sourceClean ? `Added ${sourceClean}` : 'Added items',
numberItems: added,
groupId: groupId,
cancellable: false
})
})
}
function parseResponseExplore(
dataGraphQL: any
){
const items: any[] = dataGraphQL?.sectional_items
if(!items){
return
}
const toCheck: any[] = [];
items.forEach((item: any)=>{
if(item?.layout_content?.fill_items){
toCheck.push(...item?.layout_content?.fill_items)
}
})
if(toCheck.length===0){
return;
}
const membersData = toCheck.map((node)=>{
const media = node?.media;
if(!media){
return null
}
const owner = media?.owner;
// User Data
const {
pk,
username,
full_name,
is_private,
profile_pic_url
} = owner;
const result: InstaMember = {
profileId: pk,
username: username,
fullName: full_name,
isPrivate: is_private,
pictureUrl: profile_pic_url,
source: "Explore"
}
return result;
})
const toAdd: [string, InstaMember][] = []
membersData.forEach(memberData=>{
if(memberData){
toAdd.push([memberData.profileId, memberData])
}
})
const groupId = randomString(10)
memberListStore.addElems(toAdd, false, groupId).then((added)=>{
updateConter();
logsTracker.addHistoryLog({
label: 'Added items from explore',
numberItems: added,
groupId: groupId,
cancellable: false
})
})
}
function parseResponse(
dataRaw: string,
responseType: 'users' | 'section' | 'explore',
source?: string
): void{
let dataGraphQL: Array<any> = [];
try{
dataGraphQL.push(JSON.parse(dataRaw))
}catch(err){
// Sometime Server returns multiline response
const splittedData = dataRaw.split("\n");
// If not a multiline response
if(splittedData.length<=1){
console.error('Fail to parse API response', err);
return;
}
// Multiline response. Parse each response
for(let i=0; i<splittedData.length;i++){
const newDataRaw = splittedData[i];
try{
dataGraphQL.push(JSON.parse(newDataRaw));
}catch(err2){
console.error('Fail to parse API response', err);
}
}
}
for(let j=0; j<dataGraphQL.length; j++){
if(responseType == "section"){
try{
processResponse(dataGraphQL[j], source)
}catch(err){
console.error(err)
}
}else if(responseType == "users"){
try{
processResponseUsers(dataGraphQL[j], source)
}catch(err){
console.error(err)
}
}else if(responseType == "explore"){
try{
parseResponseExplore(dataGraphQL[j])
}catch(err){
console.error(err)
}
}
}
}
const profileUsernamesCache: {[key: string]: string} = {}
async function quickProfileIdLookup(profileId: string): Promise<string | null> {
if(typeof(profileUsernamesCache[profileId])==="string"){
return profileUsernamesCache[profileId]
}
// Try to find in storage
const instaProfile = await memberListStore.getElem(profileId)
if(instaProfile){
// Add in quick storage and return
profileUsernamesCache[profileId] = instaProfile.username;
return instaProfile.username
}
return null
}
function main(): void {
buildCTABtns()
// Watch API calls to find GraphQL responses to parse
const regExTagsMatch = /\/api\/v1\/tags\/web_info\/\?tag_name=(?<tag_name>[\w|_|-]+)/i;
const regExLocationMatch = /\/api\/v1\/locations\/web_info\/?location_id=(?<location_id>[\w|_|-]+)/i;
const regExTagNewMatch = /\/api\/v1\/fbsearch\/web\/top_serp\/\?(?:[\w|_|-|&|=]+)query=(?<tag_name>[\w|_|-|%]+)/i;
// FetchMore
const regExLocationFetchMore = /\/api\/v1\/locations\/(?<location_id>[\w|\d]+)\/sections\//i
// GenericMore
const regExFetchMoreMatch = /\/api\/v1\/[\w|\d|\/]+\/sections\//i
// Explore
const regLocationSlug = /explore\/locations\/(\d+)\/(?<location_slug>[\w|-]+)\/?/i
// Followers/Following
const regExMatchFollowers = /\/api\/v1\/friendships\/(?<profile_id>\d+)\/followers\//i; // Remove g flag to reset index
const regExMatchFollowing = /\/api\/v1\/friendships\/(?<profile_id>\d+)\/following\//i; // Remove g flag to reset index
let send = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function() {
this.addEventListener('readystatechange', function() {
if (this.readyState === 4) {
if(this.responseURL.includes('/api/v1/tags/web_info')){ // Tag
let tagName: undefined| string;
const tagResult = regExTagsMatch.exec(this.responseURL);
if(tagResult){
if(tagResult?.groups?.tag_name){
tagName = tagResult.groups.tag_name;
}
}
parseResponse(this.responseText, 'section', sourceString(
"tag",
tagName
));
} else if(this.responseURL.includes('/api/v1/locations/web_info')){ // Location
let locationId: undefined | string;
const locationRes = regExLocationMatch.exec(this.responseURL);
if(locationRes){
if(locationRes?.groups?.location_id){
locationId = locationRes.groups.location_id
}
}
parseResponse(this.responseText, 'section', sourceString(
"tag",
locationId
));
} else if(this.responseURL.includes('/graphql/query')){ // Explore Location
let locationSlug: undefined | string;
const locationRes = regLocationSlug.exec(window.location.href);
if(locationRes){
if(locationRes?.groups?.location_slug){
locationSlug = locationRes.groups.location_slug;
parseResponse(this.responseText, 'section', sourceString(
"tag",
locationSlug
));
}
}
} else if(this.responseURL.includes('/api/v1/fbsearch/web/top_serp')){ // Tag - New
let tagName: undefined | string;
const locationRes = regExTagNewMatch.exec(this.responseURL);
if(locationRes){
if(locationRes?.groups?.tag_name){
tagName = locationRes.groups.tag_name
}
}
parseResponse(this.responseText, 'section', sourceString(
"tag",
tagName
));
} else if(
this.responseURL.match(regExLocationFetchMore) // Location Fetch More
){
let locationId: undefined | string;
const locationRes = regExLocationFetchMore.exec(this.responseURL);
regExLocationFetchMore.lastIndex = 0;
if(locationRes){
if(locationRes?.groups?.location_id){
locationId = locationRes.groups.location_id
}
}
parseResponse(this.responseText, 'section', sourceString(
"location",
locationId
));
} else if(
this.responseURL.match(regExFetchMoreMatch) ||
this.responseURL.includes('/api/v1/tags/web_info')
){
parseResponse(this.responseText, 'section', 'post authors');
} else if(this.responseURL.includes('/api/v1/discover/web/explore_grid')){ // Explore
parseResponse(this.responseText, 'explore', 'explore')
}else {
const resultFollowers = regExMatchFollowers.exec(this.responseURL);
regExMatchFollowers.lastIndex = 0;
if(resultFollowers){
const profileId = resultFollowers?.groups?.profile_id;
if(profileId){
quickProfileIdLookup(profileId).then((username)=>{
let profileInfo = `${profileId}`;
if(username){
profileInfo = `${profileId} (${username})`
}
parseResponse(
this.responseText,
'users',
sourceString(
"followers",
profileInfo
)
);
});
}
}else{
const resultFollowing = regExMatchFollowing.exec(this.responseURL);
regExMatchFollowing.lastIndex = 0;
if(resultFollowing){
const profileId = resultFollowing?.groups?.profile_id;
if(profileId){
quickProfileIdLookup(profileId).then((username)=>{
let profileInfo = `${profileId}`;
if(username){
profileInfo = `${profileId} (${username})`
}
parseResponse(
this.responseText,
'users',
sourceString(
"following",
profileInfo
)
);
})
}
}
}
}
}
}, false);
send.apply(this, arguments as any);
};
}
main();