-
Notifications
You must be signed in to change notification settings - Fork 0
/
puppeteer.js
1196 lines (977 loc) · 44.2 KB
/
puppeteer.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
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import puppeteer from 'puppeteer';
import { writeFile } from 'fs';
import config from './puppeteerConfig.js'
import cliProgress from 'cli-progress';
import bunyan from 'bunyan';
import path from 'path'
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Determine if we are in test mode
const isTest = process.env.MODE === 'test';
let teams = new Map();
// Array to collect logs during script execution
let collectedLogs = [];
// Define the directory for logs
const logsDirectory = path.join(__dirname, 'logs');
// Define the log file path
const logFilePath = isTest
? path.join(logsDirectory, 'testEventLog.json')
: path.join(logsDirectory, `event${config.latestEvent.eventKey}Log.json`);
const bunyanLogger = bunyan.createLogger({
name: `event${config.latestEvent.fullName}`, // Give your logger a name
streams: [
{
type: 'file',
// type: 'rotating-file', // To handle log rotation
path: logFilePath, // Specify the log file path
// period: '1d', // Daily rotation
// count: 3 // Keep 3 back copies
}
],
// Use Bunyan's serializers to ensure proper JSON formatting
serializers: bunyan.stdSerializers,
// Use a custom serializer to format log records as JSON with newline separators
serializers: {
bunyanRecord: function (rec) {
return JSON.stringify(rec) + '\n';
}
}
});
// Function to log with line number
function log(level, message) {
const err = new Error();
// Extract file name and line number
const stackLines = err.stack.split('\n').slice(1, -1);//.slice(2).map(line => line.trim());
const logObject = {
level: level,
message: message,
stack: stackLines,
timestamp: new Date().toISOString()
};
// Push log object to collected logs array
collectedLogs.push(logObject);
// Log to Bunyan
bunyanLogger[level](logObject);
}
function convertJpDate(dateString) {
// Extract the year, month, day, hour, and minute using a regular expression
const datePattern = /(\d{4})年(\d{1,2})月(\d{1,2})日\s(\d{1,2}):(\d{2})/;
const match = dateString.match(datePattern);
if (match) {
const year = match[1];
const month = String(parseInt(match[2], 10)).padStart(2, '0'); // Ensure month is 2 digits
const day = String(parseInt(match[3], 10)).padStart(2, '0'); // Ensure day is 2 digits
const hour = String(parseInt(match[4], 10)).padStart(2, '0'); // Ensure hour is 2 digits
const minute = String(parseInt(match[5], 10)).padStart(2, '0'); // Ensure minute is 2 digits
return `${year}-${month}-${day} ${hour}:${minute}`;
}
return '';
}
const StringConvertJpDate = convertJpDate.toString();
async function headerExists(elementHandle, headerXPath) {
if (!elementHandle || !(await elementHandle.evaluate) || typeof (await elementHandle.evaluate) !== 'function') {
// console.log(`Header element not found or 'evaluate' function missing for XPath: ${headerXPath}`);
return false;
}
// Check if the handle represents an actual element
const element = await elementHandle.asElement();
if (!element) {
// console.log(`Header element not found for XPath: ${headerXPath}`);
return false;
}
// Check if the element has the required classes
const hasRequiredClasses = await element.evaluate(el => {
return el.classList.contains('spost') &&
el.classList.contains('clearfix') &&
el.classList.contains('nomarginbottom');
});
if (!hasRequiredClasses) {
log('error', `Header element does not contain the required classes: ${headerXPath}`);
// console.log(`Missing required Classes on Xpath: ${headerXPath} Classes: ${element.classList}`);
return false;
}
// Check if the element's innerHTML is only
const innerHTML = await element.evaluate(el => el.innerHTML.trim());
if (!innerHTML) {
return false;
}
if (!innerHTML.includes('<div')) {
log('error', `Header element's innerHTML does not contain any <div> tags: ${headerXPath}`);
// console.log(`InnerHTML didn't have <div> on Xpath: ${headerXPath} InnerHTML: ${innerHTML.trim()}`);
return false;
}
// // console.log(`\n\n\nInnerHTML: ${innerHTML.trim()}\nXPath: ${headerXPath} considered a valid header element`);
// console.log('\n');
// console.log(`XPath considered valid: ${headerXPath}`);
return true;
}
async function scrapeLongImpressions(page, headerXPathBase) {
const longImpressions = [];
let headerXPath = headerXPathBase;
let XPathOffset = 0;
let isReply = false;
const headerOffset = 5;
const replyOffset = 2;
let headerElementHandle;
let shouldLoop = true;
let responseButton;
while (shouldLoop) {
// console.log(`\nXpath from top of loop: ${headerXPath}`);
headerElementHandle = await page.evaluateHandle((XPath) => {
const headerElement = document.evaluate(XPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
return headerElement;
}, headerXPath);
if (!(await headerExists(headerElementHandle, headerXPath))) {
log('error', `Header element not found or is undefined for XPath: ${headerXPath}`);
break;
}
// console.dir(headerElementHandle, { depth: null });
// Extract pointsOverall from the <nobr> element
const pointsOverallElementHandle = await headerElementHandle.$('nobr');
const pointsOverall = pointsOverallElementHandle ? await (await pointsOverallElementHandle.getProperty('textContent')).jsonValue() : '';
// Extract other elements similarly
const topHeaderElementHandle = await headerElementHandle.$('div.entry-c div.entry-title');
const topHeaderRawString = topHeaderElementHandle ? await (await topHeaderElementHandle.getProperty('textContent')).jsonValue() : '';
const userNameRegex = /[\s]+([^\n]+?)[\s]*\n\t+/;
const userNameMatch = topHeaderRawString.match(userNameRegex);
const userName = userNameMatch ? userNameMatch[1].trim() : '';
const pointsRegex = /([\w\s]+)\s*:\s*(\d+)\s*Pts\./g;
const pointsRawString = topHeaderRawString.replace(userNameRegex, '');
const pointBreakdownArray = [];
let pointBreakdownMatch;
while ((pointBreakdownMatch = pointsRegex.exec(pointsRawString)) !== null) {
pointBreakdownArray.push({
pointName: pointBreakdownMatch[1].trim(),
pointValue: Number(pointBreakdownMatch[2])
});
}
const countryCodeElementHandle = await headerElementHandle.$('div.spost.clearfix.nomarginbottom div.entry-c div.entry-title img.flag');
const countryCode = countryCodeElementHandle ? await (await countryCodeElementHandle.getProperty('title')).jsonValue() : '';
const countryFlagElementHandle = await headerElementHandle.$('div.spost.clearfix.nomarginbottom div.entry-c div.entry-title img');
const countryFlag = countryFlagElementHandle ? await (await countryFlagElementHandle.getProperty('src')).jsonValue() : '';
const jpDateTimeElementHandle = await headerElementHandle.$('div.spost.clearfix.nomarginbottom ul li');
const jpDateTimeString = jpDateTimeElementHandle ? await (await jpDateTimeElementHandle.getProperty('textContent')).jsonValue() : null;
const jpDateTime = jpDateTimeString ? convertJpDate(jpDateTimeString) : '';
let responseButtonLinkHandle = await page.evaluateHandle((XPath) => {
let responseButtonElement = document.evaluate(`${XPath}/following-sibling::div[3]/p/a`, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (responseButtonElement) {
} else {
responseButtonElement = document.evaluate(`${XPath}/following-sibling::div[5]/p/a`, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
return responseButtonElement ? responseButtonElement.href : null;
}, headerXPath);
responseButton = responseButtonLinkHandle ? await responseButtonLinkHandle.jsonValue() : null;
const commentSectionHandle = await page.evaluateHandle((XPath) => {
const commentSectionElement = document.evaluate(`${XPath}/following-sibling::div[1]/p`, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
return commentSectionElement ? commentSectionElement.innerHTML : null;
}, headerXPath);
longImpressions.push({
pointsOverall: Number(pointsOverall),
userName: userName,
countryCode: countryCode,
countryFlag: countryFlag,
pointBreakdown: pointBreakdownArray,
jpDateTime: new Date(jpDateTime),
commentSection: commentSectionHandle ? await commentSectionHandle.jsonValue() : null,
responseButton: responseButton,
isReply: isReply
});
// first check if there is a reply
let testXPathOffset = XPathOffset + replyOffset;
let testElementHandle = await page.evaluateHandle((XPath) => {
const headerElement = document.evaluate(XPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
return headerElement;
}, `${headerXPathBase}/following-sibling::div[${testXPathOffset}]`);
if (await headerExists(testElementHandle, `${headerXPathBase}/following-sibling::div[${testXPathOffset}]`)) {
// handle case where the current reply genuinely lack a response so the next impression is where a reply would be expected
// isReply = longImpressions[longImpressions.length - 1].responseButton && responseButton ? true : false;
isReply = pointBreakdownArray.length > 0 ? true : false;
} else {
// next check if there is another impression
testXPathOffset = XPathOffset + headerOffset;
testElementHandle = await page.evaluateHandle((XPath) => {
const headerElement = document.evaluate(XPath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
return headerElement;
}, `${headerXPathBase}/following-sibling::div[${testXPathOffset}]`);
if (await headerExists(testElementHandle, `${headerXPathBase}/following-sibling::div[${testXPathOffset}]`)) {
isReply = false;
} else {
shouldLoop = false;
break;
}
}
XPathOffset = testXPathOffset;
headerXPath = `${headerXPathBase}/following-sibling::div[${XPathOffset}]`; // Update headerXPath for next iteration
// console.log(`Xpath from bottom of loop: ${headerXPath}`);
}
return longImpressions;
}
async function saveData() {
// Convert Map to JSON-friendly structure
const teamsObject = {};
teams.forEach((teamDetails, key) => {
const teamDetailsObject = { ...teamDetails }; // Copy main team details
// Handle nested songs Map
if (teamDetails.songs instanceof Map) {
teamDetailsObject.songs = Array.from(teamDetails.songs).reduce((acc, [songKey, songDetails]) => {
acc.push({ songKey, ...songDetails });
return acc;
}, []);
}
teamsObject[key] = teamDetailsObject;
});
const latestEventObject = {
[config.latestEvent.eventKey]: {
fullName: config.latestEvent.fullName,
shortName: config.latestEvent.shortName,
subTitle: config.latestEvent.subTitle,
lastScrapeTime: new Date(),
teams: teamsObject
}
};
// Write teams data to a JSON file
writeFile(`./data/event${config.latestEvent.eventKey}.json`, JSON.stringify(latestEventObject, null, 2), (err) => {
if (err) {
log('error', `Error writing to file: ${err}`);
} else {
log('info', 'Successfully wrote to file');
}
});
// Write collected logs to file as a single JSON array
writeFile(logFilePath, JSON.stringify(collectedLogs, null, 2), (err) => {
if (err) {
// console.log(`Error writing to logFile: ${err}`);
}
});
}
(async () => {
console.time('programExecution');
// Create a new progress bar instance and use shades_classic theme
const multibar = new cliProgress.MultiBar({
clearOnComplete: false,
hideCursor: true,
format: ' {bar} {percentage}% | {filename}',
}, cliProgress.Presets.shades_classic);
const browser = await puppeteer.launch({
headless: "new"
/*
headless: false,
devtools: true
*/
});
const page = await browser.newPage();
page.setDefaultTimeout(config.navigationTimeout);
await page.goto(`https://manbow.nothing.sh/event/event.cgi?action=List_def&event=${config.latestEvent.eventKey}`);
const allTeamElements = await page.$$('.team_information');
// Apply the limit if in test mode
const teamElements = config.numberOfTeamsToLimit
? allTeamElements.slice(0, config.numberOfTeamsToLimit)
: allTeamElements.slice(0, -1); // for whatever reason the last element is just empty?
const eventPageTotal = teamElements.length * config.actions.eventPage
const eventPageBar = multibar.create(eventPageTotal, 0)
eventPageBar.update({ filename: `Event: ${config.latestEvent.fullName}` });
let teamIndex = 1;
for (const teamElement of teamElements) {
const teamInfo = await teamElement.$eval('.fancy-title :is(h2, h3) a', (link) => {
const teamName = link.innerText.trim();
const bannerImageSrc = link.querySelector('img') ? link.querySelector('img').src : '';
const teamPageLink = link.href;
return { teamName, bannerImageSrc, teamPageLink };
});
eventPageBar.increment();
const emblemImageSrc = await teamElement.$eval('.header_emblem', (emblemElement) => {
const dataBg = emblemElement.getAttribute('data-bg');
const withoutPeriod = dataBg.substring(1);
return withoutPeriod.length > 1 ? `https://manbow.nothing.sh/event${withoutPeriod}` : '';
});
eventPageBar.increment();
teamInfo.emblemImageSrc = emblemImageSrc;
eventPageBar.increment();
teamInfo.teamImpression = Number(await teamElement.$eval('#team_imp', (element) => element.innerText.trim()));
eventPageBar.increment();
teamInfo.teamTotal = Number(await teamElement.$eval('#team_total', (element) => element.innerText.trim()));
eventPageBar.increment();
teamInfo.teamMedian = Number(await teamElement.$eval('#team_med', (element) => element.innerText.trim()));
eventPageBar.increment();
// // console.log(`Processed team #${teamIndex}: ${teamInfo.teamName}`);
const songElements = await teamElement.$$('.pricing-box.best-price');
eventPageBar.increment();
// Initialize an array to store song information for the current team
const songs = new Map();
if (!songElements) {
// console.log('No song elements found for this team.');
continue;
}
let songIndex = 1;
// Iterate through songs within the current team
for (const songElement of songElements) {
// songElement.scrollIntoView();
debugger;
// Song Information and Points Information
let songName = '';
try {
songName = await songElement.$eval('a', (a) => a.innerText.trim());
} catch (error) {
const text = await page.$eval('span#notready strong', (strongElement) => {
return strongElement.textContent;
});
if (text === '- NO ENTRY -') {
// // console.log('Skipping non entry for team', teamInfo.teamName);
continue;
}
}
try {
const genreName = await songElement.$eval('h5', (h5) => h5.innerText.trim());
const artistName = await songElement.$eval('.textOverflow:nth-child(3)', (textOverflow) => textOverflow.innerText.trim());
const linkElement = await songElement.$('a');
const songPageLink = linkElement ? await linkElement.getProperty('href').then(href => href.jsonValue()) : null;
const pointsElements = await songElement.$$('xpath/ancestor::div[contains(@class, "col-sm-4")]');
const spans = await pointsElements[0].$$('.bofu_meters span');
const totalPoints = Number(await spans[0].evaluate(span => span.innerText.replace('Total :', '').replace(' Point', '').trim()));
const medianPoints = Number(await spans[1].evaluate(span => span.innerText.replace('Median :', '').replace(' Points', '').trim()));
const songInfo = {
songName,
genreName,
artistName,
songPageLink,
totalPoints,
medianPoints,
};
//BMS labels
const bmsLabels = await songElement.$eval('.bmsinfo small', (labelElement) => {
const labels = Array.from(labelElement.querySelectorAll('strong')).map((label) => label.innerText.trim());
return labels;
});
songInfo.bmsLabels = bmsLabels;
const entryCompositionUpdateElement = await songElement.$eval('.pricing-action span small', (element) => {
const text = element.innerText.trim();
const entryRegex = /No.(\d+)/;
const compositionRegex = /(Original|Copy|Arrange|Remix)/
const updateRegex = /update : (\d{4}\/\d{2}\/\d{2} \d{2}:\d{2})/;
const entryMatch = text.match(entryRegex);
let entryString = null;
if (entryMatch) {
entryString = entryMatch[1];
}
const compositionMatch = text.match(compositionRegex);
let compositionString = null;
if (compositionMatch) {
compositionString = compositionMatch[1];
}
const updateMatch = text.match(updateRegex);
let updateDateString = null;
if (updateMatch) {
updateDateString = updateMatch[1];
}
return { entryString, compositionString, updateDateString };
});
songInfo.entryNumber = Number(entryCompositionUpdateElement.entryString);
songInfo.compositionType = entryCompositionUpdateElement.compositionString;
songInfo.updateDateTime = new Date(entryCompositionUpdateElement.updateDateString);
songInfo.scrapedDateTime = new Date();
// Push the extracted song information to the songs array
songs.set(songInfo.songName, songInfo);
songIndex += 1;
// // console.log(`Processed Song #${songIndex}: ${songInfo.songName}`);
} catch (error) {
// console.log('An Error occured:', error);
// console.log(teamInfo.teamName);
}
}
teamInfo.songs = songs;
eventPageBar.increment();
const teamKey = teamInfo.teamPageLink.match(/team=([\d]+)/)[1].trim();
teams.set(teamKey, teamInfo);
eventPageBar.increment();
teamIndex += 1;
}
// // console.log(teams);
const teamPageTotal = teams.size * config.actions.teamPage;
const teamPageBar = multibar.create(teamPageTotal, 0);
// Assuming 'teams' is your Map object containing team information
let totalSongs = 0;
// Iterate over each entry in the 'teams' map
for (const [teamName, teamInfo] of teams.entries()) {
// Access the songs array from teamInfo and get its length
const songCount = teamInfo.songs.size;
// Add the song count to the total
totalSongs += songCount;
}
// Now 'totalSongs' contains the count of all songs across all teams
// // console.log(`Total songs across all teams: ${totalSongs}`);
const songPageTotal = totalSongs * config.actions.songPage;
const songPageBar = multibar.create(songPageTotal, 0);
// Now, you can access songPageLink within the existing songs map
for (const [teamName, teamInfo] of teams.entries()) {
// Navigate to the teamPageLink
await page.goto(teamInfo.teamPageLink);
teamPageBar.update({ filename: `Teams: ${teamInfo.teamName}` });
teamPageBar.increment();
const sectionElements = await page.$$('div.col_full.center.bottommargin-lg, div.col_half.center, div.col_half.col_last.center, div.col_full.center.bottommargin-lg, div.col_full.center.bottommargin-lg, div.col_half.center.nobottommargin, div.col_half.col_last.center.nobottommargin, div.post-grid.grid-container.post-masonry.clearfix, div.col_full.center.bottommargin-lg, div.col_one_third.bottommargin-lg.center, div.col_one_third.col_last.bottommargin-lg.center, div.col_full.bottommargin-lg, div.col_full.bottommargin-lg, div.col_half.bottommargin-lg, div.col_half.col_last.bottommargin-lg');
teamPageBar.increment();
// ghetto enums cause apparently javascript doesn't have em???
const LEADER = 0;
const TWITTER = 1;
const WEBSITE = 2;
const CONCEPT = 3;
// const BLANK_WORKS = 4;
const WORKS = 5;
const DECLARED = 6;
// const SONGS = 7;
// const BLANK = 8;
const GENRE = 9;
const SHARED = 10;
const REASON = 11;
const MEMBERS = 12;
const COMMENT = 13;
const REGIST = 14;
const UPDATE = 15
const leaderSection = await sectionElements[LEADER].$eval('p:nth-of-type(2)', (element) => {
teamLeader = element.querySelector('big').innerText.trim();
const teamLeaderCountryCode = element.querySelector('img') ? element.querySelector('img').title : '';
const teamLeaderCountryFlag = element.querySelector('img') ? element.querySelector('img').src : '';
const textContent = element.textContent.trim();
const teamLeaderLanguageMatch = textContent.match(/Language : ([^)]+)/);
const teamLeaderLanguage = teamLeaderLanguageMatch ? teamLeaderLanguageMatch[1].trim() : '';
return { teamLeader, teamLeaderCountryCode, teamLeaderCountryFlag, teamLeaderLanguage };
});
teamPageBar.increment();
teamInfo.teamLeader = leaderSection.teamLeader;
teamPageBar.increment();
teamInfo.teamLeaderCountryCode = leaderSection.teamLeaderCountryCode;
teamInfo.teamLeaderCountryFlag = leaderSection.teamLeaderCountryFlag;
teamPageBar.increment();
teamInfo.teamLeaderLanguage = leaderSection.teamLeaderLanguage;
teamPageBar.increment();
let twitterSection = {};
try {
twitterSection = await sectionElements[TWITTER].$eval('p a', (element) => {
const twitterLink = element.href;
return { twitterLink };
});
} catch (error) {
log('error', `Team ${teamName} has no twitter link`);
twitterSection.twitterLink = '';
}
teamPageBar.increment();
teamInfo.twitterLink = twitterSection.twitterLink;
teamPageBar.increment();
let websiteSection = {};
try {
websiteSection = await sectionElements[WEBSITE].$eval('p a', (element) => {
const websiteLink = element.href;
return { websiteLink };
});
} catch (error) {
log('error', `Team ${teamName} has no website link`);
websiteSection.websiteLink = '';
}
teamPageBar.increment();
teamInfo.websiteLink = websiteSection.websiteLink;
teamPageBar.increment();
const conceptSection = await sectionElements[CONCEPT].$$eval('.col-md-3.center.bottommargin-lg', (elements) => {
let concepts = [];
for (const element of elements) {
const conceptImage = element.querySelector('img') ? element.querySelector('img').src : '';
const conceptName = element.querySelector('h3').textContent.trim();
concepts.push({ conceptImage, conceptName });
}
return { concepts };
});
teamPageBar.increment();
teamInfo.concepts = conceptSection.concepts;
teamPageBar.increment();
let worksSection = {};
try {
worksSection = await sectionElements[WORKS].$eval('.counter', (element) => {
const works = Number(element.textContent.trim());
return { works };
});
} catch (error) {
log('error', `Team ${teamName} has no works`);
worksSection.works = 0;
}
teamPageBar.increment();
teamInfo.works = worksSection.works;
teamPageBar.increment();
const declaredWorksSection = await sectionElements[DECLARED].$eval('.counter', (element) => {
const declaredWorks = Number(element.textContent.trim());
return { declaredWorks };
});
teamPageBar.increment();
teamInfo.declaredWorks = declaredWorksSection.declaredWorks;
teamPageBar.increment();
const genreSection = await sectionElements[GENRE].$eval('p', (element) => {
const textContent = element.textContent.trim();
const genreMatch = textContent.match(/オリジナル \/ ([^)]+)/);
const genre = genreMatch ? genreMatch[1].trim() : '';
return { genre };
});
teamPageBar.increment();
teamInfo.genre = genreSection.genre;
teamPageBar.increment();
const sharedSection = await sectionElements[SHARED].$eval('p', (element) => {
const shared = element.textContent.trim();
return { shared };
});
teamPageBar.increment();
teamInfo.shared = sharedSection.shared;
teamPageBar.increment();
const reasonSection = await sectionElements[REASON].$eval('p', (element) => {
const reason = element.textContent.trim();
return { reason };
});
teamPageBar.increment();
teamInfo.reason = reasonSection.reason;
teamPageBar.increment();
const memberSection = await sectionElements[MEMBERS].$$eval('p', (elements) => {
const membersRaw = elements[0].textContent.trim();
const memberCount = Number(elements[1].textContent.trim().match(/[\d]+/));
const membersProcessed = membersRaw.split(/[\n,/]/).map((member) => member.trim());
const isProcessedCorrectly = membersProcessed.length === memberCount;
return { membersRaw, memberCount, membersProcessed, isProcessedCorrectly };
});
teamPageBar.increment();
teamInfo.membersRaw = memberSection.membersRaw;
teamPageBar.increment();
teamInfo.memberCount = memberSection.memberCount;
teamPageBar.increment();
teamInfo.membersProcessed = memberSection.membersProcessed;
teamPageBar.increment();
teamInfo.isProcessedCorrectly = memberSection.isProcessedCorrectly;
teamPageBar.increment();
const commentSection = await sectionElements[COMMENT].$eval('p', (element) => {
const comment = element.innerHTML;
return { comment };
});
teamPageBar.increment();
teamInfo.teamComment = commentSection.comment;
teamPageBar.increment();
const registSection = await sectionElements[REGIST].$eval('strong', (element) => {
const regist = element.textContent.trim();
return { regist };
});
teamPageBar.increment();
teamInfo.teamRegist = new Date(registSection.regist);
teamPageBar.increment();
const updateSection = await sectionElements[UPDATE].$eval('strong', (element) => {
const update = element.textContent.trim();
return { update };
});
teamPageBar.increment();
teamInfo.teamUpdate = new Date(updateSection.update);
teamPageBar.increment();
// song Page Scraping
for (const [songName, songInfo] of teamInfo.songs.entries()) {
const songPageLink = songInfo.songPageLink;
songPageBar.update({ filename: `Songs: ${songName}` });
// Navigate to songPageLink
await page.goto(songPageLink);
songPageBar.increment();
// Use Puppeteer to extract the jacket source
try {
const jacketImageSrc = await page.$eval('.col_one_third.col_last.moreinfo-header.nobottommargin.hidden-xs.hidden-sm img', (imgElement) => {
const withoutPeriod = imgElement.getAttribute('src').substring(1);
return `https://manbow.nothing.sh/event${withoutPeriod}`;
});
songInfo.jacketImageSrc = jacketImageSrc
} catch (error) {
songInfo.jacketImageSrc = '';
}
songPageBar.increment();
try {
// Use Puppeteer to select the div with the specified class and style attribute
const styleElement = await page.$('.section.parallax.nomargin.notopborder');
if (styleElement) {
// Extract the style attribute value
const styleAttribute = await page.evaluate(el => el.getAttribute('style'), styleElement);
if (styleAttribute) {
// Convert the style attribute to a string
const styleString = styleAttribute.toString();
// Use a regular expression to find all URLs within the style attribute
const urlMatches = styleString.match(/url\("([^"]*upload[^"]*)"\)/);
} else {
songInfo.bannerImageSrc = urlMatches;
}
} else {
songInfo.bannerImageSrc = '';
}
} catch (error) {
songInfo.bannerImageSrc = '';
}
songPageBar.increment();
const specialElement = await page.$('span.badge.rounded-pill');
let isSpecial = false;
let specialTitle = '';
if (specialElement) {
isSpecial = true;
specialTitle = await specialElement.$eval('big', (element) => element.textContent.trim());
}
songInfo.isSpecial = isSpecial;
songInfo.specialTitle = specialTitle;
songPageBar.increment();
const bpmLevelBgaElement = await page.$eval('.col_two_third.nobottommargin, .col_full.nobottommargin', (element) => {
bpmMatches = element.textContent.match(/bpm : ([^\/]+)/);
let bpm = '';
let bpmAverage = '';
let bpmLower = '';
let bpmUpper = '';
if (bpmMatches[1].split('~').length > 1) {
bpmLower = bpmMatches[1].split('~')[0].trim();
bpmUpper = bpmMatches[1].split('~')[1].trim();
bpmAverage = String((Number(bpmUpper) + Number(bpmLower)) / 2)
} else {
bpm = bpmMatches[1].trim();
}
levelMatches = element.textContent.match(/Level : ([^\/]+)/);
let levelLower = '';
let levelUpper = '';
if (levelMatches[1].split('~').length > 1) {
levelLower = levelMatches[1].split('~')[0].trim();
levelUpper = levelMatches[1].split('~')[1].trim();
}
bgaStatus = element.textContent.match(/BGA : (.*)/)[1].trim();
return { bpm, bpmLower, bpmUpper, bpmAverage, levelLower, levelUpper, bgaStatus };
});
songInfo.bpm = Number(bpmLevelBgaElement.bpm);
songInfo.bpmLower = Number(bpmLevelBgaElement.bpmLower);
songInfo.bpmUpper = Number(bpmLevelBgaElement.bpmUpper);
songInfo.bpmAverage = Number(bpmLevelBgaElement.bpmAverage);
songInfo.levelLower = Number(bpmLevelBgaElement.levelLower.replace("★x", "").trim());
songInfo.levelUpper = Number(bpmLevelBgaElement.levelUpper.replace("★x", "").trim());
songInfo.bgaStatus = bpmLevelBgaElement.bgaStatus;
songPageBar.increment();
songPageBar.increment();
songPageBar.increment();
// Extract youtube link.
let youtubeLink = ''; // Initialize to a default value
const iframeElement = await page.$('div.fluid-width-video-wrapper iframe');
if (iframeElement) {
youtubeLink = await page.$eval('div.fluid-width-video-wrapper iframe', (iframe) => {
return iframe.getAttribute('src');
});
}
songPageBar.increment();
songInfo.youtubeLink = youtubeLink;
songPageBar.increment();
// Extract only the linkUrls
const linkUrls = await page.$$eval('blockquote p a', (elements) => {
return elements.map((element) => element.getAttribute('href'));
});
songPageBar.increment();
const downloadSize = await page.$eval('blockquote footer', (element) => {
const footerString = element.textContent.trim();
sizeQuantity = footerString.match(/Total : ([\d]+)/)[1];
sizeUnit = footerString.match(/Total : [\d]+ ([a-zA-Z]+)/)[1];
return { sizeQuantity, sizeUnit };
});
songInfo.downloadSizeQuantity = Number(downloadSize.sizeQuantity);
songInfo.downloadSizeUnit = downloadSize.sizeUnit;
songPageBar.increment();
// // console.log('Link URLs:', linkUrls);
const rawHTML = await page.$eval('p[style="font-size:75%"]', (element) => {
const innerHTML = element.innerHTML;
return innerHTML;
});
songInfo.rawHTML = rawHTML;
// Extract all text within the <p> element separated by <br> tags
const paragraphTexts = await page.$eval('p[style="font-size:75%"]', (element) => {
const textWithEntities = element.innerHTML.split('<br>').map((text) => text.trim());
// Define a mapping of character references to their corresponding characters
const characterReferences = {
'<': '<',
'>': '>',
'"': '"',
''': "'",
'&': '&',
// Add more character references here as needed
};
// Replace character references in the text
const textWithoutEntities = textWithEntities.map((text) => {
for (const entity in characterReferences) {
if (text.includes(entity)) {
text = text.replace(new RegExp(entity, 'g'), characterReferences[entity]);
}
}
return text;
});
return textWithoutEntities;
});
songPageBar.increment();
// // console.log('Paragraph Text:', paragraphTexts);
// Initialize the links array
let links = [];
// Handle inline link descriptions
let inlineUrlDescs = [];
for (const paragraphText of paragraphTexts) {
let linkElement = {
linkUrl: '',
linkDesc: '',
};
for (const linkUrl of linkUrls) {
if (paragraphText.includes(linkUrl)) {
// Create a regular expression pattern to match the link pattern
const linkPattern = new RegExp(`<a(.*?)</a>`, 'g');
// Replace the link pattern with an empty string to remove it
linkElement.linkDesc = paragraphText.replace(linkPattern, '');
linkElement.linkUrl = linkUrl;
break;
}
}
if (linkElement.linkUrl == '') {
linkElement.linkDesc = paragraphText;
}
if (linkElement.linkUrl !== '' || linkElement.linkDesc !== '') { // prevent blank linkElements
inlineUrlDescs.push(linkElement)
}
}
songPageBar.increment();
// handle link descriptions above the link
const aboveUrlDescs = [];
try {
for (let i = 0; i < inlineUrlDescs.length; i++) {
// match above descriptions to a link directly below
if (
i == 0 &&
inlineUrlDescs[i].linkUrl === '' &&
inlineUrlDescs[i].linkDesc !== '' &&
inlineUrlDescs[i + 1].linkUrl !== '' &&
inlineUrlDescs[i + 1].linkDesc === ''
) {
const newUrl = inlineUrlDescs[i + 1].linkUrl;
const newDesc = inlineUrlDescs[i].linkDesc;
i++; // Increment i to skip the next element in the original array
aboveUrlDescs.push({ linkUrl: newUrl, linkDesc: newDesc });
} else if (
i < inlineUrlDescs.length - 1 &&
inlineUrlDescs[i].linkUrl === '' &&
inlineUrlDescs[i].linkDesc !== '' &&
inlineUrlDescs[i + 1].linkUrl !== '' &&
inlineUrlDescs[i + 1].linkDesc === '' &&
inlineUrlDescs[i - 1].linkUrl !== '' &&
inlineUrlDescs[i - 1].linkDesc === ''
) {
const newUrl = inlineUrlDescs[i + 1].linkUrl;
const newDesc = inlineUrlDescs[i].linkDesc;
i++; // Increment i to skip the next element in the original array
aboveUrlDescs.push({ linkUrl: newUrl, linkDesc: newDesc });
} else if (inlineUrlDescs[i].linkUrl !== '' || inlineUrlDescs[i].linkDesc !== '') {
aboveUrlDescs.push(inlineUrlDescs[i]); // Keep the current element
}
}
} catch (error) {
// console.log('No Link Found: ', songPageLink)
// technically this could apply whatever text is there as a description with no url, but i don't have the patience for it atm
}
songPageBar.increment();
// handle multiline descs
const multilineUrlDescs = [];
let pendingUrl = '';
let pendingDesc = '';
for (const { linkUrl, linkDesc } of aboveUrlDescs) {
// debugger;
if (linkUrl) {
if (pendingUrl) {
multilineUrlDescs.push({ linkUrl: pendingUrl, linkDesc: pendingDesc });
pendingUrl = '';
pendingDesc = '';
} else if (pendingDesc) {
if (linkDesc) {
multilineUrlDescs.push({ linkUrl: '', linkDesc: pendingDesc });
pendingUrl = '';
pendingDesc = '';
} else {
multilineUrlDescs.push({ linkUrl, linkDesc: pendingDesc });
pendingUrl = '';
pendingDesc = '';
continue;
}
}
if (linkDesc) {
multilineUrlDescs.push({ linkUrl, linkDesc });
}
} else if (linkDesc) {
pendingDesc = pendingDesc ? `${pendingDesc}\n${linkDesc}` : linkDesc;
}
if (linkUrl && !linkDesc) {
multilineUrlDescs.push({ linkUrl, linkDesc });
}
}
songPageBar.increment();
// Handle any pending items
if (pendingUrl) {
multilineUrlDescs.push({ linkUrl: pendingUrl, linkDesc: pendingDesc });
} else if (pendingDesc) {
multilineUrlDescs.push({ linkUrl: '', linkDesc: pendingDesc });
}
songPageBar.increment();
// Replace the original 'links' array with the modified 'newLinks' array
links = multilineUrlDescs;
songPageBar.increment();
songInfo.links = links;
songPageBar.increment();
// // console.log('Link Descriptions:', linkDescs);
const tags = await page.$$eval('div.bmsinfo2 span.label', (labels) => {
return labels.map((label) => label.textContent);
})
songInfo.tags = tags;
songPageBar.increment();