Skip to content

Commit

Permalink
Merge pull request #2 from axi92/feature/events
Browse files Browse the repository at this point in the history
Feature/events
  • Loading branch information
axi92 authored Sep 7, 2020
2 parents 14a2e73 + 9eb375d commit 0ff463c
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 21 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1 align="center">Welcome to steam-workshop-scraper 👋</h1>
<p>
<a href="https://github.com/axi92/steam-workshop-scraper#readme" target="_blank">
<a href="https://www.npmjs.com/package/steam-workshop-scraper" target="_blank">
<img alt="npm" src="https://img.shields.io/npm/v/steam-workshop-scraper">
</a>
<img alt="npm bundle size" src="https://img.shields.io/bundlephobia/minzip/steam-workshop-scraper">
Expand Down Expand Up @@ -48,6 +48,22 @@ sws.GetInfo(670764308).then(function (data) {
});
```

### Or with update event

```javascript
const SteamWorkshopScraper = require('steam-workshop-scraper');
var sws = new SteamWorkshopScraper();

// Add your steam workshop ids what should be monitored
sws.AddToUpdates([1384657523, 670764308, 589205263]);
// Or remove some if you don't need them anymore
sws.RemoveFromUpdates([670764308, 1384657523]);
// Get the event fired if a workshop item gets updated.
sws.Event.on('update', function(data){
console.log('data on update:', data);
});
```

## Run tests

```sh
Expand Down
54 changes: 52 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const scrapeIt = require("scrape-it");
const moment = require('moment');
const momentTZ = require('moment-timezone');


const events = require('events');
const async = require("async");
const schedule = require('node-schedule');

class SteamWorkshopScraper {
constructor() {
this.workshopMap = new Map();
this.schedule = undefined;
this.workShopUrlInfo = 'https://steamcommunity.com/sharedfiles/filedetails/?id=';
this.workShopUrlChangelog = 'https://steamcommunity.com/sharedfiles/filedetails/changelog/';
this.parseSettingsInfo = {
Expand Down Expand Up @@ -40,6 +43,28 @@ class SteamWorkshopScraper {
}
}
}
this.Event = new events.EventEmitter();
var that = this;
this.schedule = schedule.scheduleJob('*/10 * * * * *', async function () {
that.workshopMap.forEach(element => {
that.GetInfo(element.id).then(function (data) {
let preObject = that.workshopMap.get(element.id);
let tempObject = {};
tempObject.id = element.id;
tempObject.title = data.title;
tempObject.size = data.size;
tempObject.timePublished = data.timePublished;
tempObject.timeUpdated = data.timeUpdated;
tempObject.image = data.image;
that.workshopMap.set(element.id, tempObject);
if (preObject.timeUpdated != undefined && preObject.timeUpdated != data.timeUpdated) { // Workshop item was updated
// Emit update
that.Event.emit('update', tempObject);
}
});
});
// console.log(that.workshopMap);
});
}

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

AddToUpdates(ids) {
if (Array.isArray(ids) === false) {
throw new Error('Provided ids are not an array!');
}
for (var i = 0; i < ids.length; i++) {
let tempObject = {};
tempObject.id = ids[i];
tempObject.title = undefined;
tempObject.size = undefined;
tempObject.timePublished = undefined;
tempObject.timeUpdated = undefined;
tempObject.image = undefined;
this.workshopMap.set(ids[i], tempObject);
}
}

RemoveFromUpdates(ids) {
if (Array.isArray(ids) === false) {
throw new Error('Provided ids are not an array!')
}
for (var i = 0; i < ids.length; i++) {
this.workshopMap.delete(ids[i]);
}
}

Scrape(url, parse) {
return scrapeIt({
url: url
Expand Down
57 changes: 56 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "steam-workshop-scraper",
"version": "0.0.2",
"version": "0.0.3",
"description": "Gets data about Steam workshop mods/assets",
"main": "index.js",
"scripts": {
Expand All @@ -16,8 +16,10 @@
"url": "https://github.com/axi92/steam-workshop-scraper/issues"
},
"dependencies": {
"async": "^3.2.0",
"moment": "^2.27.0",
"moment-timezone": "^0.5.31",
"node-schedule": "^1.3.2",
"scrape-it": "^5.2.4"
},
"engines": {
Expand Down
38 changes: 22 additions & 16 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
const SteamWorkshopScraper = require('./index.js');
const sws = new SteamWorkshopScraper();

sws.Event.on('update', function(data){
console.log('data on update:', data);
});

// sws.AddToUpdates([670764308]);
sws.AddToUpdates([1384657523, 670764308, 589205263]);
// sws.RemoveFromUpdates([670764308, 1384657523]);
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

var sws = new SteamWorkshopScraper();

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


sws.GetChangeLog(1384657523).then(function (data) { // Old mod 1384657523
console.log('data', data.data[0]);
});
sws.GetInfo(1384657523).then(function (data) {
console.log('data', data);
});


// sws.GetChangeLog(1384657523).then(function (data) { // Old mod 1384657523
// console.log('data', data.data[0]);
// });
// sws.GetInfo(1384657523).then(function (data) {
// console.log('data', data);
// });



// sws.GetChangeLog(589205263).then(function (data) { // Never updated mod 589205263
// console.log('data', data.data[0]);
// });
// sws.GetInfo(589205263).then(function (data) {
// console.log('data', data);
// });
sws.GetChangeLog(589205263).then(function (data) { // Never updated mod 589205263
console.log('data', data.data[0]);
});
sws.GetInfo(589205263).then(function (data) {
console.log('data', data);
});

0 comments on commit 0ff463c

Please sign in to comment.