-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (118 loc) · 4.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const express = require('express');
const request = require('teeny-request').teenyRequest;
const parseString = require('xml2js').parseString;
const { encode } = require('single-byte');
const ics = require('ics')
const keychain = require('./keychain.json')
const { writeFile } = require('fs')
const decoder = new TextDecoder('utf-8');
const JSONdb = require('simple-json-db');
const pekelneRes = new JSONdb('pekelne-restauracie.json');
const gram = new JSONdb("gram.json")
const app = express();
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
//res.end("StravaCal")
res.render('index');
});
app.get('/cal/:jidelna',async (req, res) => {
console.log(req.get('user-agent'));
let jidelna;
if (!req.params.hasOwnProperty('jidelna')) {
jidelna = "0000"
} else {
jidelna = req.params.jidelna
}
let tobereturned;
await res.setHeader('Content-Type', 'text/calendar');
await request({uri: `http://www.strava.cz/foxisapi/foxisapi.dll/istravne.istravne.process?xmljidelnickyA&zarizeni=${jidelna}&jazy%20k=SK&httphlavicka=A`}, function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
//body = windows1250.encode(body, { mode: 'replacement' });
let buf = Buffer.from(body, "ascii");
//console.log(decoder.decode(buf))
parseString(decoder.decode(buf), function (err, result) {
res.end(makeCal(result.VFPData.pomjidelnic_xmljidelnic, jidelna))
//console.log(result.VFPData.pomjidelnic_xmljidelnic)
});
});
console.log(jidelna)
console.log("--------")
});
/*function getStrava(jidelna) {
if (jidelna === undefined) {
jidelna = "0000"
}
let tobereturned;
request({uri: `http://www.strava.cz/foxisapi/foxisapi.dll/istravne.istravne.process?xmljidelnickyA&zarizeni=${jidelna}&jazy%20k=SK&httphlavicka=A`}, function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
//body = windows1250.encode(body, { mode: 'replacement' });
let buf = Buffer.from(body, "ascii");
console.log(decoder.decode(buf))
parseString(decoder.decode(buf), function (err, result) {
tobereturned = makeCal(result.VFPData.pomjidelnic_xmljidelnic, jidelna);
});
});
return tobereturned;
}*/
function makeCal(body, jidelna) {
let events = []
body.forEach((obed) => {
let date = String(obed.datum[0])
date = date.split('-')
//console.log(date)
let event = {
calName: `Strava.cz Jídelna ${jidelna} • StravaCal`,
productId: "charliecat/stravaCal",
start: [Number(date[0]), Number(date[1]), Number(date[2]), Number(keychain.time[0]), Number(keychain.time[1])],
duration: { hours: 0, minutes: 30 },
title: `🍽 ${zGramatovanie(obed.druh_popis.join())} • ${obed.nazev.join()}`,
description: `${obed.popis.join()}\n${obed.popis_al.join()}\n--------\nhttps://www.strava.cz/strava/stravnik/jidelnicky?zarizeni=${jidelna}`,
location: pekelneRes.get(`${jidelna}j`),
url: `https://www.strava.cz/strava/stravnik/jidelnicky?zarizeni=${jidelna}`,
status: 'CONFIRMED',
busyStatus: 'BUSY',
organizer: { name: 'StravaCal • stravacal.charliecat.space', email: 'stravacal@charliecat.space'}
}
events.push(event)
})
let { error, value } = ics.createEvents(events)
if (error) {
console.log(error)
return
}
//console.log(value)
return value
}
app.listen(7890, () => {
console.log(`Server started on port 7890`);
});
function zGramatovanie(text) {
gramatika = gram.get('_')
gramatika.forEach(element => {
text = text.replace(element.from, element.to);
})
if (text.includes("�")) {
gram.set(text, "")
}
return text
}
async function getStrava(id) {
if (id === undefined) {
id = "0000"
}
let tobereturned;
await request({uri: `http://www.strava.cz/foxisapi/foxisapi.dll/istravne.istravne.process?xmljidelnickyA&zarizeni=${id}&jazy%20k=SK&httphlavicka=A`}, function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
//body = windows1250.encode(body, { mode: 'replacement' });
let buf = Buffer.from(body, "ascii");
//console.log(decoder.decode(buf))
parseString(decoder.decode(buf), function (err, result) {
//console.dir(result)
result = result.VFPData.pomjidelnic_xmljidelnic
});
});
}
//getStrava("0000").catch(console.error)