-
Notifications
You must be signed in to change notification settings - Fork 3
/
standalone.js
143 lines (129 loc) · 4.32 KB
/
standalone.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
134
135
136
137
138
139
140
141
142
143
"use strict";
const fs = require('fs-extra');
const path = require('path');
const unzip = require('unzip');
const https = require('https');
const moment = require('moment');
const WSEvent = require('./wsevent.js');
const {
sendEvent,
deleteMessage
} = require('./tlgrm.js');
const testDir = path.join(__dirname, 'events');
const urlEvents = 'https://codeload.github.com/web-standards-ru/calendar/zip/master';
const eventsDir = path.join(testDir, 'calendar-master/events');
const sendersDir = path.join(__dirname, 'senders');
const fileEvents = path.join(testDir, 'master.zip');
if (fs.existsSync(testDir)) {
fs.removeSync(testDir);
}
fs.mkdirSync(testDir);
if (!fs.existsSync(sendersDir)) {
fs.mkdirSync(sendersDir);
}
const download = () => {
return new Promise((resolve, reject) => {
//download events yaml from github
const file = fs.createWriteStream(fileEvents);
https.get(urlEvents, (response) => {
response.pipe(file);
file.on('finish', function () {
file.close(() => {
fs.createReadStream(fileEvents).pipe(unzip.Extract({
path: testDir
}))
.on('close', () => {
resolve();
});
});
});
});
});
};
function* generatorEvents(events) {
let pos = 0;
while (pos < events.length) {
yield events[pos++];
}
}
const formatEvent = (event) => {
moment.locale('ru');
return `[${event.name}](${event.link})\n${event.city}, ${moment(event.start).utc().format('D MMMM YYYY')}`;
};
download()
.then(() => {
const events = fs.readdirSync(eventsDir)
.filter((file) => {
console.log(file);
return /\.ya?ml$/.test(file.toLowerCase());
})
.map((file) => {
const e = WSEvent.fromYaml(fs.readFileSync(path.join(eventsDir, file), 'utf-8'));
e.file = file;
return e;
})
.sort((event1, event2) => {
return event1.start.valueOf() - event2.start.valueOf();
});
send(generatorEvents(events));
})
.catch((err) => {
console.warn(err);
});
const send = (gen) => {
const item = gen.next();
if (item.done) {
return console.log('end', new Date);
}
const tmpFile = path.join(sendersDir, `${item.value.file}.res`);
const msg = formatEvent(item.value);
const _send = () => {
const start = new Date();
sendEvent(item.value, process.env.TOKEN, process.env.CHANNEL, process.env.PROXY, formatEvent)
.then((res) => {
const {
status,
body
} = res;
const data = JSON.parse(body);
if (status != 200 || !data.ok) {
throw new Error(`Error send: ${body}`);
}
const to = (new Date()).valueOf() - start.valueOf();
console.log('Send', item.value.name, to);
fs.writeFileSync(tmpFile, JSON.stringify({
messageId: data.result.message_id,
text: msg,
dt: (new Date()).toString(),
to: to
}));
})
.catch((err) => {
console.warn(err);
})
.finally(() => {
send(gen);
});
};
if (fs.existsSync(tmpFile)) {
const fileData = JSON.parse(fs.readFileSync(tmpFile));
if (fileData.text != msg) {
console.log('Rm', fileData.messageId, fileData.text, msg);
deleteMessage(fileData.messageId, process.env.TOKEN, process.env.CHANNEL, process.env.PROXY)
.then((res) => {
if (res.status != 200 || !JSON.parse(res.body).ok) {
throw new Error(`Error rm: ${res.status} ${res.body}`);
}
})
.catch((err) => {
console.warn('Error rm', fileData.messageId, err);
})
.finally(_send);
} else {
console.log('Yet sending', item.value.name);
return send(gen);
}
} else {
_send();
}
};