vlr-stats-scraper 1.3.0 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/main.mjs +91 -86
- package/package.json +1 -1
- package/scripts/player-info-scraper.mjs +17 -12
package/main.mjs
CHANGED
|
@@ -1,86 +1,91 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import promptSync from 'prompt-sync';
|
|
3
|
-
import { scrapeWebsite } from './scripts/player-link-scraper.mjs';
|
|
4
|
-
import scrapePlayer from './scripts/player-info-scraper.mjs';
|
|
5
|
-
import {
|
|
6
|
-
savePlayer,
|
|
7
|
-
saveEvent,
|
|
8
|
-
addPlayerToEvent,
|
|
9
|
-
getEventByName,
|
|
10
|
-
emptyEvent,
|
|
11
|
-
clearEvent,
|
|
12
|
-
clearDatabase,
|
|
13
|
-
} from './scripts/db.mjs';
|
|
14
|
-
|
|
15
|
-
const args = process.argv.slice(2);
|
|
16
|
-
|
|
17
|
-
if (args[0] === '--clear') {
|
|
18
|
-
const prompt = promptSync();
|
|
19
|
-
const answer = prompt('This deletes every event and player in players.db. Type "yes" to continue: ');
|
|
20
|
-
if (answer?.trim().toLowerCase() === 'yes') {
|
|
21
|
-
clearDatabase();
|
|
22
|
-
console.log('Cleared players.db');
|
|
23
|
-
} else {
|
|
24
|
-
console.log('Cancelled');
|
|
25
|
-
}
|
|
26
|
-
process.exit(0);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (args[0] === '--clear-event') {
|
|
30
|
-
const name = args[1];
|
|
31
|
-
if (!name) {
|
|
32
|
-
console.error('Usage: valstats --clear-event <event-name>');
|
|
33
|
-
process.exit(1);
|
|
34
|
-
}
|
|
35
|
-
if (clearEvent(name)) {
|
|
36
|
-
console.log(`Removed "${name}" and its players from players.db`);
|
|
37
|
-
} else {
|
|
38
|
-
console.error(`No event named "${name}" in players.db`);
|
|
39
|
-
process.exit(1);
|
|
40
|
-
}
|
|
41
|
-
process.exit(0);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const [link, year, eventName] = args;
|
|
45
|
-
|
|
46
|
-
if (!link || !year || !eventName) {
|
|
47
|
-
console.error('Usage: valstats <link> <year> <event-name>');
|
|
48
|
-
console.error(' valstats --clear-event <event-name>');
|
|
49
|
-
console.error(' valstats --clear');
|
|
50
|
-
process.exit(1);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const isStage = eventName.toLowerCase().includes('stage');
|
|
54
|
-
|
|
55
|
-
let region = null;
|
|
56
|
-
if (isStage) {
|
|
57
|
-
const prompt = promptSync();
|
|
58
|
-
region = prompt('Enter the region: ');
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Re-adding an event + year that's already saved replaces its old players.
|
|
62
|
-
const existing = getEventByName(eventName);
|
|
63
|
-
if (existing && existing.year === parseInt(year, 10)) {
|
|
64
|
-
emptyEvent(eventName);
|
|
65
|
-
console.log(`"${eventName}" (${year}) is already in players.db, replacing it`);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const eventId = saveEvent({
|
|
69
|
-
name: eventName,
|
|
70
|
-
url: link,
|
|
71
|
-
region,
|
|
72
|
-
year: parseInt(year, 10),
|
|
73
|
-
stage: null,
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
const links = await scrapeWebsite(link);
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import promptSync from 'prompt-sync';
|
|
3
|
+
import { scrapeWebsite } from './scripts/player-link-scraper.mjs';
|
|
4
|
+
import scrapePlayer from './scripts/player-info-scraper.mjs';
|
|
5
|
+
import {
|
|
6
|
+
savePlayer,
|
|
7
|
+
saveEvent,
|
|
8
|
+
addPlayerToEvent,
|
|
9
|
+
getEventByName,
|
|
10
|
+
emptyEvent,
|
|
11
|
+
clearEvent,
|
|
12
|
+
clearDatabase,
|
|
13
|
+
} from './scripts/db.mjs';
|
|
14
|
+
|
|
15
|
+
const args = process.argv.slice(2);
|
|
16
|
+
|
|
17
|
+
if (args[0] === '--clear') {
|
|
18
|
+
const prompt = promptSync();
|
|
19
|
+
const answer = prompt('This deletes every event and player in players.db. Type "yes" to continue: ');
|
|
20
|
+
if (answer?.trim().toLowerCase() === 'yes') {
|
|
21
|
+
clearDatabase();
|
|
22
|
+
console.log('Cleared players.db');
|
|
23
|
+
} else {
|
|
24
|
+
console.log('Cancelled');
|
|
25
|
+
}
|
|
26
|
+
process.exit(0);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (args[0] === '--clear-event') {
|
|
30
|
+
const name = args[1];
|
|
31
|
+
if (!name) {
|
|
32
|
+
console.error('Usage: valstats --clear-event <event-name>');
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
if (clearEvent(name)) {
|
|
36
|
+
console.log(`Removed "${name}" and its players from players.db`);
|
|
37
|
+
} else {
|
|
38
|
+
console.error(`No event named "${name}" in players.db`);
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
process.exit(0);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const [link, year, eventName] = args;
|
|
45
|
+
|
|
46
|
+
if (!link || !year || !eventName) {
|
|
47
|
+
console.error('Usage: valstats <link> <year> <event-name>');
|
|
48
|
+
console.error(' valstats --clear-event <event-name>');
|
|
49
|
+
console.error(' valstats --clear');
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const isStage = eventName.toLowerCase().includes('stage');
|
|
54
|
+
|
|
55
|
+
let region = null;
|
|
56
|
+
if (isStage) {
|
|
57
|
+
const prompt = promptSync();
|
|
58
|
+
region = prompt('Enter the region: ');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Re-adding an event + year that's already saved replaces its old players.
|
|
62
|
+
const existing = getEventByName(eventName);
|
|
63
|
+
if (existing && existing.year === parseInt(year, 10)) {
|
|
64
|
+
emptyEvent(eventName);
|
|
65
|
+
console.log(`"${eventName}" (${year}) is already in players.db, replacing it`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const eventId = saveEvent({
|
|
69
|
+
name: eventName,
|
|
70
|
+
url: link,
|
|
71
|
+
region,
|
|
72
|
+
year: parseInt(year, 10),
|
|
73
|
+
stage: null,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const links = await scrapeWebsite(link);
|
|
77
|
+
|
|
78
|
+
if (!links) {
|
|
79
|
+
console.error(`Couldn't get the player list from ${link}`);
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
for (const playerLink of links) {
|
|
84
|
+
const player = await scrapePlayer(playerLink, region);
|
|
85
|
+
if (player) {
|
|
86
|
+
savePlayer(player);
|
|
87
|
+
addPlayerToEvent(eventId, player.name);
|
|
88
|
+
console.log(`Saved ${player.name} to players.db under "${eventName}"`);
|
|
89
|
+
console.log(player);
|
|
90
|
+
}
|
|
91
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vlr-stats-scraper",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "Scrapes player stats from vlr.gg and stores them in a local SQLite database, grouped by tournament or stage.",
|
|
5
5
|
"homepage": "https://github.com/OverYander/VLR-Webscraper#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import * as cheerio from 'cheerio'
|
|
1
|
+
import * as cheerio from 'cheerio'
|
|
2
2
|
import { Hyperbrowser } from "@hyperbrowser/sdk"
|
|
3
|
-
import { config } from "dotenv"
|
|
3
|
+
import { config } from "dotenv"
|
|
4
4
|
|
|
5
|
-
config()
|
|
5
|
+
config()
|
|
6
6
|
|
|
7
7
|
let client;
|
|
8
8
|
function getClient() {
|
|
@@ -25,15 +25,15 @@ async function scrapePlayer(url, region) {
|
|
|
25
25
|
})
|
|
26
26
|
|
|
27
27
|
const $ = cheerio.load(scrapeData.data.html);
|
|
28
|
-
|
|
29
28
|
const agentsPlayed = findAgents($)
|
|
29
|
+
|
|
30
30
|
return {
|
|
31
31
|
name: getPlayerName(url),
|
|
32
32
|
team: getCurTeam($),
|
|
33
33
|
past_teams: getPastTeam($),
|
|
34
34
|
winnings: getWinnings($),
|
|
35
35
|
most_played_agents: agentsPlayed.map(data => data.Agent),
|
|
36
|
-
avg_kda: agentsPlayed.reduce((acc, data) => acc + (parseFloat(data['K:D'])) / agentsPlayed.length, 0),
|
|
36
|
+
avg_kda: agentsPlayed.reduce((acc, data) => acc + (parseFloat(data['K:D'])) / agentsPlayed.length, 0).toFixed(2),
|
|
37
37
|
nationality: getNationality($),
|
|
38
38
|
region: region,
|
|
39
39
|
stats: agentsPlayed
|
|
@@ -44,15 +44,16 @@ async function scrapePlayer(url, region) {
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
// testing
|
|
49
48
|
/*
|
|
50
49
|
node scripts/player-info-scraper.mjs
|
|
51
50
|
*/
|
|
52
51
|
|
|
52
|
+
scrapePlayer("https://www.vlr.gg/player/939/mwzera/?timespan=all", "AMER")
|
|
53
|
+
|
|
54
|
+
|
|
53
55
|
function getPlayerName(link) {
|
|
54
|
-
let name =
|
|
55
|
-
console.log(name)
|
|
56
|
+
let name = new URL(link).pathname.split('/').filter(ele => ele !== '').pop()
|
|
56
57
|
return name
|
|
57
58
|
}
|
|
58
59
|
|
|
@@ -66,8 +67,9 @@ function findAgents($) {
|
|
|
66
67
|
const agentStats = header.next().children()
|
|
67
68
|
const agents = agentStats.map((i, el) => {
|
|
68
69
|
el = $(el)
|
|
70
|
+
// console.log('agent found:', el.children().first().find('img').attr('alt'))?.trim()
|
|
69
71
|
return {
|
|
70
|
-
agent: el.children().first().
|
|
72
|
+
agent: el.children().first().find('img').attr('alt')?.trim(),
|
|
71
73
|
stats: el.text().split('\t').map(ele => ele.trim()).filter(ele => ele !== '')
|
|
72
74
|
}
|
|
73
75
|
}).get()
|
|
@@ -91,7 +93,8 @@ function getCurTeam($) {
|
|
|
91
93
|
// console.log('getCurTeam ran')
|
|
92
94
|
const currentTeam = $('h2:contains("Current Teams")').first().next().text().trim()
|
|
93
95
|
const currentTeamName = currentTeam.split('\t').filter(ele => ele.trim() !== '')
|
|
94
|
-
|
|
96
|
+
// Free agents have no current team
|
|
97
|
+
return currentTeamName.length ? currentTeamName[0].trim() : null
|
|
95
98
|
}
|
|
96
99
|
|
|
97
100
|
function getPastTeam($) {
|
|
@@ -120,4 +123,6 @@ function getNationality($) {
|
|
|
120
123
|
// console.log('flags found:', $('.flag').length);
|
|
121
124
|
// console.log(header.parent().text().trim())
|
|
122
125
|
return header.parent().text().trim()
|
|
123
|
-
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export default scrapePlayer
|