Skip to content

Commit 0ff463c

Browse files
authored
Merge pull request #2 from axi92/feature/events
Feature/events
2 parents 14a2e73 + 9eb375d commit 0ff463c

File tree

5 files changed

+150
-21
lines changed

5 files changed

+150
-21
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1 align="center">Welcome to steam-workshop-scraper 👋</h1>
22
<p>
3-
<a href="https://github.com/axi92/steam-workshop-scraper#readme" target="_blank">
3+
<a href="https://www.npmjs.com/package/steam-workshop-scraper" target="_blank">
44
<img alt="npm" src="https://img.shields.io/npm/v/steam-workshop-scraper">
55
</a>
66
<img alt="npm bundle size" src="https://img.shields.io/bundlephobia/minzip/steam-workshop-scraper">
@@ -48,6 +48,22 @@ sws.GetInfo(670764308).then(function (data) {
4848
});
4949
```
5050

51+
### Or with update event
52+
53+
```javascript
54+
const SteamWorkshopScraper = require('steam-workshop-scraper');
55+
var sws = new SteamWorkshopScraper();
56+
57+
// Add your steam workshop ids what should be monitored
58+
sws.AddToUpdates([1384657523, 670764308, 589205263]);
59+
// Or remove some if you don't need them anymore
60+
sws.RemoveFromUpdates([670764308, 1384657523]);
61+
// Get the event fired if a workshop item gets updated.
62+
sws.Event.on('update', function(data){
63+
console.log('data on update:', data);
64+
});
65+
```
66+
5167
## Run tests
5268

5369
```sh

index.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
const scrapeIt = require("scrape-it");
22
const moment = require('moment');
33
const momentTZ = require('moment-timezone');
4-
5-
4+
const events = require('events');
5+
const async = require("async");
6+
const schedule = require('node-schedule');
67

78
class SteamWorkshopScraper {
89
constructor() {
10+
this.workshopMap = new Map();
11+
this.schedule = undefined;
912
this.workShopUrlInfo = 'https://steamcommunity.com/sharedfiles/filedetails/?id=';
1013
this.workShopUrlChangelog = 'https://steamcommunity.com/sharedfiles/filedetails/changelog/';
1114
this.parseSettingsInfo = {
@@ -40,6 +43,28 @@ class SteamWorkshopScraper {
4043
}
4144
}
4245
}
46+
this.Event = new events.EventEmitter();
47+
var that = this;
48+
this.schedule = schedule.scheduleJob('*/10 * * * * *', async function () {
49+
that.workshopMap.forEach(element => {
50+
that.GetInfo(element.id).then(function (data) {
51+
let preObject = that.workshopMap.get(element.id);
52+
let tempObject = {};
53+
tempObject.id = element.id;
54+
tempObject.title = data.title;
55+
tempObject.size = data.size;
56+
tempObject.timePublished = data.timePublished;
57+
tempObject.timeUpdated = data.timeUpdated;
58+
tempObject.image = data.image;
59+
that.workshopMap.set(element.id, tempObject);
60+
if (preObject.timeUpdated != undefined && preObject.timeUpdated != data.timeUpdated) { // Workshop item was updated
61+
// Emit update
62+
that.Event.emit('update', tempObject);
63+
}
64+
});
65+
});
66+
// console.log(that.workshopMap);
67+
});
4368
}
4469

4570
GetInfo(id) {
@@ -50,6 +75,31 @@ class SteamWorkshopScraper {
5075
return this.Scrape(this.workShopUrlChangelog + id.toString(), this.parseSettingsChangeLog);
5176
}
5277

78+
AddToUpdates(ids) {
79+
if (Array.isArray(ids) === false) {
80+
throw new Error('Provided ids are not an array!');
81+
}
82+
for (var i = 0; i < ids.length; i++) {
83+
let tempObject = {};
84+
tempObject.id = ids[i];
85+
tempObject.title = undefined;
86+
tempObject.size = undefined;
87+
tempObject.timePublished = undefined;
88+
tempObject.timeUpdated = undefined;
89+
tempObject.image = undefined;
90+
this.workshopMap.set(ids[i], tempObject);
91+
}
92+
}
93+
94+
RemoveFromUpdates(ids) {
95+
if (Array.isArray(ids) === false) {
96+
throw new Error('Provided ids are not an array!')
97+
}
98+
for (var i = 0; i < ids.length; i++) {
99+
this.workshopMap.delete(ids[i]);
100+
}
101+
}
102+
53103
Scrape(url, parse) {
54104
return scrapeIt({
55105
url: url

package-lock.json

Lines changed: 56 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "steam-workshop-scraper",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Gets data about Steam workshop mods/assets",
55
"main": "index.js",
66
"scripts": {
@@ -16,8 +16,10 @@
1616
"url": "https://github.com/axi92/steam-workshop-scraper/issues"
1717
},
1818
"dependencies": {
19+
"async": "^3.2.0",
1920
"moment": "^2.27.0",
2021
"moment-timezone": "^0.5.31",
22+
"node-schedule": "^1.3.2",
2123
"scrape-it": "^5.2.4"
2224
},
2325
"engines": {

test.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
const SteamWorkshopScraper = require('./index.js');
2+
const sws = new SteamWorkshopScraper();
3+
4+
sws.Event.on('update', function(data){
5+
console.log('data on update:', data);
6+
});
7+
8+
// sws.AddToUpdates([670764308]);
9+
sws.AddToUpdates([1384657523, 670764308, 589205263]);
10+
// sws.RemoveFromUpdates([670764308, 1384657523]);
11+
console.log(sws.workshopMap); // All workshop items and informations, needs some time to update. Those are empty on AddToUpdates() and get populated after max. 10s
212

3-
var sws = new SteamWorkshopScraper();
413

514
sws.GetChangeLog(670764308).then(function (data) { // up to date mod
615
console.log('data', data.data);
@@ -10,20 +19,17 @@ sws.GetInfo(670764308).then(function (data) {
1019
}); //up to date 18. Aug. um 16:18 Uhr
1120

1221

22+
sws.GetChangeLog(1384657523).then(function (data) { // Old mod 1384657523
23+
console.log('data', data.data[0]);
24+
});
25+
sws.GetInfo(1384657523).then(function (data) {
26+
console.log('data', data);
27+
});
1328

1429

15-
// sws.GetChangeLog(1384657523).then(function (data) { // Old mod 1384657523
16-
// console.log('data', data.data[0]);
17-
// });
18-
// sws.GetInfo(1384657523).then(function (data) {
19-
// console.log('data', data);
20-
// });
21-
22-
23-
24-
// sws.GetChangeLog(589205263).then(function (data) { // Never updated mod 589205263
25-
// console.log('data', data.data[0]);
26-
// });
27-
// sws.GetInfo(589205263).then(function (data) {
28-
// console.log('data', data);
29-
// });
30+
sws.GetChangeLog(589205263).then(function (data) { // Never updated mod 589205263
31+
console.log('data', data.data[0]);
32+
});
33+
sws.GetInfo(589205263).then(function (data) {
34+
console.log('data', data);
35+
});

0 commit comments

Comments
 (0)