Skip to content

Commit 753e0e4

Browse files
authored
Merge pull request #68 from axi92/feature/moment2luxon
replaced moment with luxon, add timezone support
2 parents ed8acc1 + dc054aa commit 753e0e4

File tree

4 files changed

+57
-36
lines changed

4 files changed

+57
-36
lines changed

index.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
const scrapeIt = require("scrape-it");
2-
const moment = require('moment');
3-
const momentTZ = require('moment-timezone');
42
const events = require('events');
53
const schedule = require('node-schedule');
64
const debug = require('debug')('steam-workshop-scraper');
5+
const { DateTime, Settings } = require("luxon");
76

87
class SteamWorkshopScraper {
9-
constructor() {
8+
constructor(timeZoneName = undefined) {
109
this.workshopMap = new Map();
1110
this.schedule = undefined;
1211
this.workShopUrlInfo = 'https://steamcommunity.com/sharedfiles/filedetails/?id=';
@@ -59,6 +58,12 @@ class SteamWorkshopScraper {
5958
this.schedule = schedule.scheduleJob('28 * * * * *', function () {
6059
this.TriggerUpdate();
6160
}.bind(this));
61+
if(timeZoneName != undefined){
62+
Settings.defaultZone = timeZoneName;
63+
if(Settings.defaultZone.valid != true){
64+
throw new Error('Timezone invalid for constructor SteamWorkshopScraper: ' + timeZoneName)
65+
}
66+
}
6267
}
6368

6469
async TriggerUpdate(){
@@ -153,30 +158,25 @@ class SteamWorkshopScraper {
153158

154159
ParseSteamTime(string) {
155160
debug('ParseStreamTime:', string);
156-
if (string.match('[0-9]{4}')) {
157-
let parsed = moment(string, 'DD MMM, YYYY @ h:mma').format('YYYY-MM-DD HH:mm');
158-
let a = momentTZ.tz(parsed, 'America/Los_Angeles');
159-
if (a.isValid()) {
160-
var time = moment(a).local();
161-
} else {
162-
console.error('not valid date2');
163-
}
161+
var steamDefaultTimezone = 'America/Los_Angeles';
162+
var time;
163+
let time1 = DateTime.fromFormat(string, "d MMM, yyyy @ h:ma", { locale: 'en', zone: steamDefaultTimezone });
164+
let time2 = DateTime.fromFormat(string, "d MMM @ h:ma", { locale: 'en', zone: steamDefaultTimezone });
165+
let time3 = DateTime.fromFormat(string, "MMM d, yyyy @ h:ma", { locale: 'en', zone: steamDefaultTimezone }); // github workflow format
166+
let time4 = DateTime.fromFormat(string, "MMM d @ h:ma", { locale: 'en', zone: steamDefaultTimezone }); // github workflow format for current year
167+
if(time1.isValid){
168+
time = time1;
169+
} else if(time2.isValid){
170+
time = time2;
171+
} else if(time3.isValid){
172+
time = time3;
173+
} else if(time4.isValid){
174+
time = time4;
164175
} else {
165-
let parsed = moment(string, 'DD MMM @ h:mma').format('YYYY-MM-DD HH:mm');
166-
var a = momentTZ.tz(parsed, 'America/Los_Angeles');
167-
try {
168-
if (a.isValid()) {
169-
var time = moment(a).local();
170-
} else {
171-
console.error('not valid date2');
172-
}
173-
} catch (error) {
174-
debug('var a is:');
175-
debug(a);
176-
console.error('try catch error:', error);
177-
}
176+
throw new Error('No time format was found in parser for: ' + string);
178177
}
179-
return time.toISOString(true);
178+
time = time.setZone(Settings.defaultZone);
179+
return time.toString(true);
180180
}
181181
}
182182

package-lock.json

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"dependencies": {
1919
"async": "^3.2.0",
2020
"debug": "^4.3.2",
21+
"luxon": "^3.3.0",
2122
"moment": "^2.29.1",
2223
"moment-timezone": "^0.5.42",
2324
"node-schedule": "^2.1.1",

test/test.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const SteamWorkshopScraper = require('../index.js');
2-
const sws = new SteamWorkshopScraper();
2+
const sws = new SteamWorkshopScraper('Universal');
33
var assert = require('assert');
4-
4+
const { DateTime } = require("luxon");
55

66
describe('SteamWorkshopScraper', function () {
77
it('should show 18 entries', async function () {
@@ -54,15 +54,34 @@ describe('SteamWorkshopScraper', function () {
5454

5555
it('GetChangeLog check text and timePosted', async function () {
5656
await sws.GetChangeLog(478528785).then(function (data) { // very old mod
57-
// console.log('data', data.data[0].text);
5857
assert.equal(data.data[0].text, 'Version 1.0');
59-
assert.equal(data.data[0].timePosted, '2015-07-09T23:59:00.000+02:00');
58+
assert.equal(data.data[0].timePosted, '2015-07-09T21:59:00.000+00:00');
6059
});
6160
});
6261

6362
it('GetInfo never updated mod', async function () {
6463
await sws.GetInfo(518030553).then(function (data) {
65-
// console.log(data);
64+
assert.equal(data.title, 'TXM: Turret Expansion Mod');
65+
assert.equal(data.size, '0.036 MB');
66+
assert.equal(data.timePublished, '2015-09-14T01:52:00.000+00:00');
67+
assert.equal(data.timeUpdated, '2015-09-14T01:52:00.000+00:00');
68+
assert.equal(data.image, 'https://steamuserimages-a.akamaihd.net/ugc/421440386976795132/B34EDDA953337D1CD05DBE82BAAA397B0520AB50/?imw=268&imh=268&ima=fit&impolicy=Letterbox&imcolor=%23000000&letterbox=true');
69+
});
70+
});
71+
72+
it('GetInfo active updated mod', async function () {
73+
await sws.GetInfo(731604991).then(function (data) {
74+
assert.equal(data.title, 'Structures Plus (S+)');
75+
assert.equal(data.size, '58.174 MB');
76+
assert.equal(data.timePublished, '2016-07-26T02:40:00.000+00:00');
77+
assert.equal(data.timeUpdated.includes(DateTime.now().year), true);
78+
assert.equal(data.image, 'https://steamuserimages-a.akamaihd.net/ugc/1020574589494908839/7C3E05B2D3568E166D1E9B0A7597782934F2A153/?imw=268&imh=268&ima=fit&impolicy=Letterbox&imcolor=%23000000&letterbox=true');
79+
});
80+
});
81+
82+
it('Timezone', async function () {
83+
const swsTZ = new SteamWorkshopScraper('Europe/Vienna');
84+
await swsTZ.GetInfo(518030553).then(function (data) {
6685
assert.equal(data.title, 'TXM: Turret Expansion Mod');
6786
assert.equal(data.size, '0.036 MB');
6887
assert.equal(data.timePublished, '2015-09-14T03:52:00.000+02:00');

0 commit comments

Comments
 (0)