vlr-stats-scraper 1.0.1 → 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 +15 -0
- package/index.mjs +3 -0
- package/main.mjs +48 -2
- package/package.json +1 -1
- package/scripts/db.mjs +30 -0
package/README.md
CHANGED
|
@@ -34,6 +34,18 @@ npx valstats https://www.vlr.gg/event/2977/vct-2026-americas-stage-2/group-stage
|
|
|
34
34
|
Each player on that event page gets scraped and saved to `players.db` in your current
|
|
35
35
|
directory, linked to that event name.
|
|
36
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
|
+
|
|
37
49
|
## Library usage
|
|
38
50
|
|
|
39
51
|
```js
|
|
@@ -62,6 +74,9 @@ savePlayer(player);
|
|
|
62
74
|
- `addPlayerToEvent(eventId, playerName)`
|
|
63
75
|
- `getEvents()` / `getEventByName(name)`
|
|
64
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.
|
|
65
80
|
|
|
66
81
|
## Data storage
|
|
67
82
|
|
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.0
|
|
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": {
|
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
|
};
|