vlr-stats-scraper 1.0.0 → 1.1.0
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/README.md +88 -74
- package/index.mjs +3 -0
- package/main.mjs +48 -2
- package/package.json +2 -2
- package/scripts/db.mjs +30 -0
- package/scripts/player-info-scraper.mjs +10 -4
- package/scripts/player-link-scraper.mjs +14 -8
package/README.md
CHANGED
|
@@ -1,74 +1,88 @@
|
|
|
1
|
-
# vlr-stats-scraper
|
|
2
|
-
|
|
3
|
-
Scrapes player stats from [vlr.gg](https://www.vlr.gg) and stores them in a local SQLite
|
|
4
|
-
database, grouped by tournament or stage, so you can pull a backlog of players
|
|
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
|
-
|
|
1
|
+
# vlr-stats-scraper
|
|
2
|
+
|
|
3
|
+
Scrapes player stats from [vlr.gg](https://www.vlr.gg) and stores them in a local SQLite
|
|
4
|
+
database, grouped by tournament or stage, so you can pull a backlog of players.
|
|
5
|
+
|
|
6
|
+
## Setup
|
|
7
|
+
|
|
8
|
+
Requires a [Hyperbrowser](https://hyperbrowser.ai) API key for scraping.
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install vlr-stats-scraper
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Create a `.env` file in the directory you run it from:
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
HYPERBROWSER_API_KEY=your_key_here
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## CLI usage
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npx valstats <link> <year> <event-name>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npx valstats https://www.vlr.gg/event/2977/vct-2026-americas-stage-2/group-stage 2026 "Stage 2"
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
- `link` — the vlr.gg event/group-stage page to scrape.
|
|
31
|
+
- `year` — the year the event took place.
|
|
32
|
+
- `event-name` — a label for this tournament/stage (used to group players together later).
|
|
33
|
+
|
|
34
|
+
Each player on that event page gets scraped and saved to `players.db` in your current
|
|
35
|
+
directory, linked to that event name.
|
|
36
|
+
|
|
37
|
+
Running it again with an event name and year that are already saved replaces that event's
|
|
38
|
+
players with the freshly scraped ones.
|
|
39
|
+
|
|
40
|
+
### Clearing data
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
npx valstats --clear-event "Stage 2" # remove one event and its players
|
|
44
|
+
npx valstats --clear # wipe the whole database (asks for confirmation)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Players that also belong to another event are kept when a single event is cleared.
|
|
48
|
+
|
|
49
|
+
## Library usage
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
import {
|
|
53
|
+
scrapePlayer,
|
|
54
|
+
scrapeWebsite,
|
|
55
|
+
savePlayer,
|
|
56
|
+
saveEvent,
|
|
57
|
+
addPlayerToEvent,
|
|
58
|
+
getPlayersByEvent,
|
|
59
|
+
getRandomPlayerFromEvent,
|
|
60
|
+
} from 'vlr-stats-scraper';
|
|
61
|
+
|
|
62
|
+
const links = await scrapeWebsite('https://www.vlr.gg/event/.../group-stage');
|
|
63
|
+
const player = await scrapePlayer(links[0], 'AMER');
|
|
64
|
+
savePlayer(player);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Exports
|
|
68
|
+
|
|
69
|
+
- `scrapePlayer(url, region)` — scrapes one player's profile page.
|
|
70
|
+
- `scrapeWebsite(url)` — scrapes an event page for player profile links.
|
|
71
|
+
- `savePlayer(player)` / `getPlayer(name)` / `getAllPlayers()` / `getRandomPlayer()`
|
|
72
|
+
- `saveEvent({ name, url, region, year, stage })` — creates/updates a tournament or stage,
|
|
73
|
+
returns its id.
|
|
74
|
+
- `addPlayerToEvent(eventId, playerName)`
|
|
75
|
+
- `getEvents()` / `getEventByName(name)`
|
|
76
|
+
- `getPlayersByEvent(eventName)` / `getRandomPlayerFromEvent(eventName)`
|
|
77
|
+
- `emptyEvent(eventName)` — removes an event's players but keeps the event.
|
|
78
|
+
- `clearEvent(eventName)` — removes an event and its players.
|
|
79
|
+
- `clearDatabase()` — deletes every event and player.
|
|
80
|
+
|
|
81
|
+
## Data storage
|
|
82
|
+
|
|
83
|
+
Player and event data is stored in a `players.db` SQLite file created in whichever
|
|
84
|
+
directory you run the tool from.
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
ISC
|
package/index.mjs
CHANGED
package/main.mjs
CHANGED
|
@@ -2,12 +2,51 @@
|
|
|
2
2
|
import promptSync from 'prompt-sync';
|
|
3
3
|
import { scrapeWebsite } from './scripts/player-link-scraper.mjs';
|
|
4
4
|
import scrapePlayer from './scripts/player-info-scraper.mjs';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
savePlayer,
|
|
7
|
+
saveEvent,
|
|
8
|
+
addPlayerToEvent,
|
|
9
|
+
getEventByName,
|
|
10
|
+
emptyEvent,
|
|
11
|
+
clearEvent,
|
|
12
|
+
clearDatabase,
|
|
13
|
+
} from './scripts/db.mjs';
|
|
6
14
|
|
|
7
|
-
const
|
|
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;
|
|
8
45
|
|
|
9
46
|
if (!link || !year || !eventName) {
|
|
10
47
|
console.error('Usage: valstats <link> <year> <event-name>');
|
|
48
|
+
console.error(' valstats --clear-event <event-name>');
|
|
49
|
+
console.error(' valstats --clear');
|
|
11
50
|
process.exit(1);
|
|
12
51
|
}
|
|
13
52
|
|
|
@@ -19,6 +58,13 @@ if (isStage) {
|
|
|
19
58
|
region = prompt('Enter the region: ');
|
|
20
59
|
}
|
|
21
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
|
+
|
|
22
68
|
const eventId = saveEvent({
|
|
23
69
|
name: eventName,
|
|
24
70
|
url: link,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vlr-stats-scraper",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
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": {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"main": "./index.mjs",
|
|
17
17
|
"exports": "./index.mjs",
|
|
18
18
|
"bin": {
|
|
19
|
-
"valstats": "
|
|
19
|
+
"valstats": "main.mjs"
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
22
22
|
"index.mjs",
|
package/scripts/db.mjs
CHANGED
|
@@ -161,6 +161,33 @@ function getRandomPlayerFromEvent(eventName) {
|
|
|
161
161
|
`).get(eventName));
|
|
162
162
|
}
|
|
163
163
|
|
|
164
|
+
// Deletes players that no longer belong to any event.
|
|
165
|
+
function deleteOrphanedPlayers() {
|
|
166
|
+
db.prepare('DELETE FROM players WHERE name NOT IN (SELECT player_name FROM event_players)').run();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Removes every player from an event but keeps the event itself, so it can be re-scraped.
|
|
170
|
+
// Players that are still in other events are kept.
|
|
171
|
+
const emptyEvent = db.transaction((eventName) => {
|
|
172
|
+
const event = getEventByName(eventName);
|
|
173
|
+
if (!event) return false;
|
|
174
|
+
db.prepare('DELETE FROM event_players WHERE event_id = ?').run(event.id);
|
|
175
|
+
deleteOrphanedPlayers();
|
|
176
|
+
return true;
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// Removes an event and its players. Players that are still in other events are kept.
|
|
180
|
+
const clearEvent = db.transaction((eventName) => {
|
|
181
|
+
if (!emptyEvent(eventName)) return false;
|
|
182
|
+
db.prepare('DELETE FROM events WHERE name = ?').run(eventName);
|
|
183
|
+
return true;
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
// Wipes every event and player from the database.
|
|
187
|
+
const clearDatabase = db.transaction(() => {
|
|
188
|
+
db.exec('DELETE FROM event_players; DELETE FROM events; DELETE FROM players;');
|
|
189
|
+
});
|
|
190
|
+
|
|
164
191
|
export {
|
|
165
192
|
savePlayer,
|
|
166
193
|
getPlayer,
|
|
@@ -172,4 +199,7 @@ export {
|
|
|
172
199
|
getEventByName,
|
|
173
200
|
getPlayersByEvent,
|
|
174
201
|
getRandomPlayerFromEvent,
|
|
202
|
+
emptyEvent,
|
|
203
|
+
clearEvent,
|
|
204
|
+
clearDatabase,
|
|
175
205
|
};
|
|
@@ -4,14 +4,20 @@ import { config } from "dotenv";
|
|
|
4
4
|
|
|
5
5
|
config();
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
let client;
|
|
8
|
+
function getClient() {
|
|
9
|
+
if (!client) {
|
|
10
|
+
client = new Hyperbrowser({
|
|
11
|
+
apiKey: process.env.HYPERBROWSER_API_KEY,
|
|
12
|
+
})
|
|
13
|
+
}
|
|
14
|
+
return client;
|
|
15
|
+
}
|
|
10
16
|
|
|
11
17
|
async function scrapePlayer(url, region) {
|
|
12
18
|
console.log('scrapePlayer ran')
|
|
13
19
|
try {
|
|
14
|
-
const scrapeData = await
|
|
20
|
+
const scrapeData = await getClient().scrape.startAndWait({
|
|
15
21
|
url: url,
|
|
16
22
|
scrapeOptions: {
|
|
17
23
|
formats: ["html"]
|
|
@@ -3,13 +3,19 @@ import { config } from "dotenv";
|
|
|
3
3
|
|
|
4
4
|
config();
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
let client;
|
|
7
|
+
function getClient() {
|
|
8
|
+
if (!client) {
|
|
9
|
+
client = new Hyperbrowser({
|
|
10
|
+
apiKey: process.env.HYPERBROWSER_API_KEY,
|
|
11
|
+
})
|
|
12
|
+
}
|
|
13
|
+
return client;
|
|
14
|
+
}
|
|
9
15
|
|
|
10
16
|
async function scrapeWebsite(url) {
|
|
11
17
|
try {
|
|
12
|
-
const scrapeData = await
|
|
18
|
+
const scrapeData = await getClient().scrape.startAndWait({
|
|
13
19
|
url: url,
|
|
14
20
|
scrapeOptions: {
|
|
15
21
|
formats: ["html"],
|
|
@@ -19,14 +25,14 @@ async function scrapeWebsite(url) {
|
|
|
19
25
|
|
|
20
26
|
const players = scrapeData.data.html.split('\n').filter(line => line.includes('event-team-players-item'))
|
|
21
27
|
const playerLinks = players.map(player => {
|
|
22
|
-
const match = player.match(/href="(https:\/\/www\.vlr\.gg\/player\/[^"]*)"/)
|
|
23
|
-
return match ? match[1] : null;
|
|
28
|
+
const match = player.match(/href="(https:\/\/www\.vlr\.gg\/player\/[^"]*)"/)
|
|
29
|
+
return match ? match[1] + "/?timespan=all" : null;
|
|
24
30
|
}).filter(link => link !== null);
|
|
25
31
|
|
|
26
|
-
return playerLinks
|
|
32
|
+
return playerLinks
|
|
27
33
|
}
|
|
28
34
|
catch (error) {
|
|
29
|
-
console.error("Error scraping website:", error)
|
|
35
|
+
console.error("Error scraping website:", error)
|
|
30
36
|
}
|
|
31
37
|
}
|
|
32
38
|
|