summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2018-05-01 20:26:07 +0200
committerschneefux <schneefux+commit@schneefux.xyz>2018-05-01 20:38:01 +0200
commit567a0f7a2f248cd25b8a5e0e357f9b10c16a9036 (patch)
tree5c0e0e8790fe8f7be2eecfcb4fe70222b7a847da /src
parent1c0c2e4be26dcd68fdb538edd5845ea702b9fa98 (diff)
downloadsplus-567a0f7a2f248cd25b8a5e0e357f9b10c16a9036.tar.gz
splus-567a0f7a2f248cd25b8a5e0e357f9b10c16a9036.zip
Retrieve schedule for current week, add course config var (closes #3)
Diffstat (limited to 'src')
-rw-r--r--src/config.ts4
-rw-r--r--src/index.ts34
-rw-r--r--src/sources/FileSource.ts5
-rw-r--r--src/sources/HttpSource.ts25
-rw-r--r--src/sources/ISource.ts2
5 files changed, 33 insertions, 37 deletions
diff --git a/src/config.ts b/src/config.ts
index 47bcd02..b6ed360 100644
--- a/src/config.ts
+++ b/src/config.ts
@@ -13,8 +13,8 @@ export interface SplusConfig {
}
export const config: SplusConfig = {
- source: new HttpSource('http://splus.ostfalia.de/semesterplan123.php?id=1362F014835FFFD0F67159E302EC1A3C&identifier=%23SPLUS7A3292'),
- sink: new IcalSink('out.ics'),
+ source: new HttpSource('#SPLUS7A3292'),
+ sink: new IcalSink('docs/informatik1.ics'),
lectureFilter: lecture => {
// Filter some lectures out
diff --git a/src/index.ts b/src/index.ts
index 95d3797..47bc00c 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,39 +1,35 @@
+import * as moment from 'moment';
import {SplusParser} from './core/SplusParser';
import {config} from './config';
import {IEvent} from './core/IEvent';
-function getMonday(date: Date) {
- const day = date.getDay() || 7;
- if (day !== 1) {
- date.setHours(-24 * (day - 1));
- }
-
- return new Date(date.getFullYear(), date.getMonth(), date.getDate());
-}
-
-const baseDate = getMonday(new Date());
+const baseDate = moment().startOf('week');
-config.source.getData().then(async data => {
+config.source.getData(baseDate.weeks()).then(async data => {
const lectures = new SplusParser(data.toString()).getLectures(config.lectureFilter);
for (let i = 0; i < lectures.length; i++) {
const lec = lectures[i];
- let beginDate = new Date(baseDate);
- beginDate.setDate(beginDate.getDate() + lec.day);
- beginDate.setMinutes(beginDate.getMinutes() + lec.begin * 60);
+ let beginDate = baseDate.clone();
+ beginDate.add(moment.duration({
+ days: lec.day + 1, // for moment: 1 = Monday
+ hours: lec.begin,
+ }));
- let endDate = new Date(baseDate);
- endDate.setDate(endDate.getDate() + lec.day);
- endDate.setMinutes(endDate.getMinutes() + lec.end * 60);
+ let endDate = baseDate.clone();
+ endDate.add(moment.duration({
+ days: lec.day + 1,
+ hours: lec.end,
+ }));
const event: IEvent = {
summary: lec.title,
description: lec.lecturer + (lec.info !== '' ? ' - ' : '') + lec.info,
location: lec.room,
- start: beginDate,
- end: endDate
+ start: beginDate.toDate(),
+ end: endDate.toDate(),
};
await config.sink.createEvent(event);
diff --git a/src/sources/FileSource.ts b/src/sources/FileSource.ts
index fd4f6f7..57e51ae 100644
--- a/src/sources/FileSource.ts
+++ b/src/sources/FileSource.ts
@@ -6,11 +6,10 @@ export class FileSource implements ISource {
private _path: string;
private _data: string = null;
- constructor(path: string) {
- this._path = path;
+ constructor(course: string) {
}
- getData(): Promise<string> {
+ getData(weekOfYear: number): Promise<string> {
if (this._data) {
return Promise.resolve(this._data);
}
diff --git a/src/sources/HttpSource.ts b/src/sources/HttpSource.ts
index 45ddd7a..f9391aa 100644
--- a/src/sources/HttpSource.ts
+++ b/src/sources/HttpSource.ts
@@ -1,22 +1,23 @@
-import {get} from 'http';
+import * as request from 'request-promise-native';
import {ISource} from './ISource';
export class HttpSource implements ISource {
- private _uri: string;
+ private _base_uri = 'http://splus.ostfalia.de/semesterplan123.php';
- constructor(path: string) {
- this._uri = path;
+ constructor(private _course: string) {
}
- getData(): Promise<string> {
- return new Promise<string>((resolve, reject) => {
- get(this._uri, (res) => {
- let data = '';
- res.on('error', reject);
- res.on('data', chunk => data += chunk);
- res.on('end', () => resolve(data));
- });
+ getData(weekOfYear: number): PromiseLike<string> {
+ return request({
+ method: 'POST',
+ uri: this._base_uri,
+ qs: {
+ identifier: this._course,
+ },
+ formData: {
+ weeks: weekOfYear.toString(),
+ },
});
}
}
diff --git a/src/sources/ISource.ts b/src/sources/ISource.ts
index 042aaae..3605260 100644
--- a/src/sources/ISource.ts
+++ b/src/sources/ISource.ts
@@ -1,3 +1,3 @@
export interface ISource {
- getData(): Promise<string>;
+ getData(weekOfYear: number): PromiseLike<string>;
}