Skip to content

Commit 9132557

Browse files
authored
Merge pull request #53 from Sirius-A/feat/eslint
chore: switch to eslint
2 parents be1b9ae + 11d3ba9 commit 9132557

File tree

7 files changed

+650
-397
lines changed

7 files changed

+650
-397
lines changed

.eslintrc.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
env:
2+
browser: true
3+
commonjs: true
4+
es2021: true
5+
extends: google
6+
overrides: []
7+
parserOptions:
8+
ecmaVersion: latest
9+
rules: {
10+
require-jsdoc: 0,
11+
max-len: 0
12+
}

app/SVPageParser.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,32 @@ const request = require('request');
33
const cheerio = require('cheerio');
44
const url = 'http://siemens.sv-restaurant.ch/de/menuplan/';
55

6-
class SVPageParser{
7-
6+
class SVPageParser {
87
parseToday(callback) {
98
request(url, (err, resp, body) => {
10-
let $ = cheerio.load(body);
11-
let offers = $('#menu-plan-tab1').find('.menu-item');
12-
let message = this.formatDayMenu($, offers);
9+
const $ = cheerio.load(body);
10+
const offers = $('#menu-plan-tab1').find('.menu-item');
11+
const message = this.formatDayMenu($, offers);
1312
callback(message);
1413
});
1514
}
1615

1716
parseWeek(callback) {
1817
request(url, (err, resp, body) => {
19-
let $ = cheerio.load(body);
20-
let message = this.formatWeek($);
18+
const $ = cheerio.load(body);
19+
const message = this.formatWeek($);
2120
callback(message);
2221
});
2322
}
2423

2524
formatWeek($) {
26-
let days = $('.day-nav').find('li');
25+
const days = $('.day-nav').find('li');
2726

2827
let text = '';
2928
$(days).each((i, day) => {
30-
let dayName = $(day).find('.day').text();
31-
let dayDate= $(day).find('.date').text();
32-
let offers = $(`#menu-plan-tab${i+1}`).find('.menu-item');
29+
const dayName = $(day).find('.day').text();
30+
const dayDate= $(day).find('.date').text();
31+
const offers = $(`#menu-plan-tab${i+1}`).find('.menu-item');
3332

3433
text += `*${dayName} ${dayDate}* \n`;
3534
text += `========\n`;
@@ -41,19 +40,19 @@ class SVPageParser{
4140

4241
formatDayMenu($, offers) {
4342
let text = '';
44-
$(offers).each(function (i, offer) {
45-
let menuDescription = $(offer).find('.menu-description');
43+
$(offers).each(function(i, offer) {
44+
const menuDescription = $(offer).find('.menu-description');
4645
menuDescription.find('br').replaceWith(' ');
4746

4847
text += '_' + removeSpecialCharacters($(offer).find('.menuline').text()) + '_\n';
4948
text += '*' + removeSpecialCharacters($(offer).find('.menu-title').text().trim()) + '*\n';
5049
text += removeSpecialCharacters(menuDescription.text()) + '\n';
51-
text += '_' + removeSpecialCharacters($(offer).find('.prices-3 .val').text() + ' ' + $(offer).find('.prices-3 .desc').text()) + "_\n";
52-
let provenance = removeSpecialCharacters($(offer).find('.menu-provenance').text());
50+
text += '_' + removeSpecialCharacters($(offer).find('.prices-3 .val').text() + ' ' + $(offer).find('.prices-3 .desc').text()) + '_\n';
51+
const provenance = removeSpecialCharacters($(offer).find('.menu-provenance').text());
5352
if (provenance.length > 1) {
54-
text += '_' + provenance + "_\n\n";
53+
text += '_' + provenance + '_\n\n';
5554
} else {
56-
text += "\n";
55+
text += '\n';
5756
}
5857
});
5958

app/app.js

Lines changed: 90 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
2-
const Tgfancy = require("tgfancy");
3-
const bot_api_token = process.env.BOT_API_TOKEN;
2+
const Tgfancy = require('tgfancy');
3+
const botApiToken = process.env.BOT_API_TOKEN;
44

5-
const tgBot = new Tgfancy(bot_api_token, {
5+
const tgBot = new Tgfancy(botApiToken, {
66
tgfancy: {
77
emojification: true,
88
},
9-
polling: true
9+
polling: true,
1010
});
1111
const CronJob = require('cron').CronJob;
1212

@@ -15,194 +15,195 @@ const parser = new Parser();
1515
const Subscriptions = require('./subscriptions');
1616
const subscriptions = new Subscriptions();
1717

18-
const weekdays = ["(Mon(day)?)","(Tue(sday)?)","(Wed(nesday)?)","Thu(rsday)?","Fri(day)?"];
19-
const weekdayRegex = new RegExp(weekdays.join("(@\w+)?|"),'i');
18+
const weekdays = ['(Mon(day)?)', '(Tue(sday)?)', '(Wed(nesday)?)', 'Thu(rsday)?', 'Fri(day)?'];
19+
const weekdayRegex = new RegExp(weekdays.join('(@\w+)?|'), 'i');
2020

2121
let botUsername = process.env.BOT_USERNAME;
22-
if (botUsername === undefined){
23-
botUsername = "FiveMoodsBot";
22+
if (botUsername === undefined) {
23+
botUsername = 'FiveMoodsBot';
2424
}
2525

2626
/* Daily cronjob to notify subscribers*/
2727
try {
28-
new CronJob('00 10 * * 1-5', function () {
28+
new CronJob('00 10 * * 1-5', function() {
2929
console.log('Cron job started');
3030
notifySubscribers();
31-
}, null, true,'Europe/Zurich');
32-
} catch(ex) {
33-
console.log("cron pattern not valid");
31+
}, null, true, 'Europe/Zurich');
32+
} catch (ex) {
33+
console.log('cron pattern not valid');
3434
}
3535

3636
/* Routes */
37-
tgBot.onText(/\/(get)?(Today)?(@\w+)?$/gmi,getTodayHandler);
38-
tgBot.onText(/\/(get)?Week(@\w+)?$/gmi,getWeekHandler);
39-
tgBot.onText(/\/(get)?Daily(@\w+)?$/gmi,getDailyHandler);
40-
tgBot.onText(/\/(get)?PartTime(@\w+)?/i,getPartTimeHandler);
41-
tgBot.onText(/\/start(@\w+)?/i,startHandler);
42-
tgBot.onText(/\/stop(@\w+)?/i,cancelSubscriptionsHandler);
43-
tgBot.onText(/\/cancel(@\w+)?/i,cancelSubscriptionsHandler);
44-
tgBot.onText(/\/(get)?source(@\w+)?/mgi,getSourceHandler);
45-
tgBot.onText(weekdayRegex,weekdayHandler);
46-
tgBot.onText(/Done.*(@\w+)?/i,cancelWeekdaySelectionHandler);
37+
tgBot.onText(/\/(get)?(Today)?(@\w+)?$/gmi, getTodayHandler);
38+
tgBot.onText(/\/(get)?Week(@\w+)?$/gmi, getWeekHandler);
39+
tgBot.onText(/\/(get)?Daily(@\w+)?$/gmi, getDailyHandler);
40+
tgBot.onText(/\/(get)?PartTime(@\w+)?/i, getPartTimeHandler);
41+
tgBot.onText(/\/start(@\w+)?/i, startHandler);
42+
tgBot.onText(/\/stop(@\w+)?/i, cancelSubscriptionsHandler);
43+
tgBot.onText(/\/cancel(@\w+)?/i, cancelSubscriptionsHandler);
44+
tgBot.onText(/\/(get)?source(@\w+)?/mgi, getSourceHandler);
45+
tgBot.onText(weekdayRegex, weekdayHandler);
46+
tgBot.onText(/Done.*(@\w+)?/i, cancelWeekdaySelectionHandler);
4747
// tgBot.onText(/notify/gi,notifySubscribers); //to test subscriber notifications
4848

4949
/* Handlers */
5050
function getTodayHandler(message) {
51-
let chatId = message.chat.id;
51+
const chatId = message.chat.id;
5252
sendTodaysMenu(chatId);
5353
}
5454

5555
function getWeekHandler(message) {
56-
let chatId = message.chat.id;
56+
const chatId = message.chat.id;
5757
sendWeekMenu(chatId);
5858
}
5959

6060
function getDailyHandler(message) {
61-
let chat = message.chat;
62-
let chatId = message.chat.id;
61+
const chat = message.chat;
62+
const chatId = message.chat.id;
6363

64-
subscriptions.add(chat,function () {
65-
let markdownText = "*Successfully added you to the daily subscriber list* \n" +
66-
"I will send you the menu at 10:00am from now on. \n" +
64+
subscriptions.add(chat, function() {
65+
const markdownText = '*Successfully added you to the daily subscriber list* \n' +
66+
'I will send you the menu at 10:00am from now on. \n' +
6767
`You can send me /stop@${botUsername} to quit that.`;
68-
tgBot.sendMessage(chatId,markdownText,{ parse_mode: 'Markdown'});
68+
tgBot.sendMessage(chatId, markdownText, {parse_mode: 'Markdown'});
6969
});
7070
}
7171

7272
function getPartTimeHandler(message) {
73-
let chatId = message.chat.id;
73+
const chatId = message.chat.id;
7474

7575
// remove current subscriptions before selecting new ones.
7676
subscriptions.remove(chatId);
7777

7878
const weekdaysKeyboard = [
79-
[{text: "Monday"},{text: "Tuesday"},],
80-
[{text: "Wednesday"},{text: "Thursday"}],
81-
[{text: "Friday"}],[{text: "Done!"}]
79+
[{text: 'Monday'}, {text: 'Tuesday'}],
80+
[{text: 'Wednesday'}, {text: 'Thursday'}],
81+
[{text: 'Friday'}], [{text: 'Done!'}],
8282
];
8383

84-
let markdownText = "Please select all weekdays you want to be notified.";
84+
const markdownText = 'Please select all weekdays you want to be notified.';
8585

86-
let options = {
87-
"parse_mode": "Markdown",
88-
"reply_markup": {
89-
"keyboard": weekdaysKeyboard,
90-
"selective": true,
91-
}
86+
const options = {
87+
'parse_mode': 'Markdown',
88+
'reply_markup': {
89+
'keyboard': weekdaysKeyboard,
90+
'selective': true,
91+
},
9292
};
9393
tgBot.sendMessage(chatId, markdownText, options);
9494
}
9595

96-
function weekdayHandler(message,match) {
97-
let chat = message.chat;
98-
let chatId = message.chat.id;
99-
let markdownText = "Okay I will send you updates each " + match[0] ;
100-
let weekdayIndex = + weekdays.regexIndexOf(match[0]) + 1; //+1 since dates/days start on Sunday .
96+
function weekdayHandler(message, match) {
97+
const chat = message.chat;
98+
const chatId = message.chat.id;
99+
const markdownText = 'Okay I will send you updates each ' + match[0];
100+
const weekdayIndex = + weekdays.regexIndexOf(match[0]) + 1; // +1 since dates/days start on Sunday .
101101

102-
subscriptions.addWeekday(chat,weekdayIndex,function () {
102+
subscriptions.addWeekday(chat, weekdayIndex, function() {
103103
tgBot.sendMessage(chatId, markdownText, {parse_mode: 'Markdown'});
104104
weekdays.regexIndexOf(match[0]);
105105
});
106-
107106
}
108107

109108
function cancelWeekdaySelectionHandler(message) {
110-
let chat = message.chat;
111-
let chatId = message.chat.id;
109+
const chat = message.chat;
110+
const chatId = message.chat.id;
112111

113-
subscriptions.getWeekdays(chat,function (err,weekdaysData) {
114-
let markdownText = "";
115-
if(err){
116-
console.log("error getting weekdays from mongodb");
112+
subscriptions.getWeekdays(chat, function(err, weekdaysData) {
113+
let markdownText = '';
114+
if (err) {
115+
console.log('error getting weekdays from mongodb');
117116
return;
118117
}
119118

120-
if(weekdaysData === null){
121-
markdownText = " You did not select any Weekdays :confused: \nI will not send you updates";
122-
}else {
123-
markdownText = ":thumbsup: Alright. Here are the days I will send you the menu:";
124-
for (let weekday of weekdaysData.weekdays) {
125-
let weekdayName = weekdays[weekday - 1].replace(/[()?]/gi, "");
119+
if (weekdaysData === null) {
120+
markdownText = ' You did not select any Weekdays :confused: \nI will not send you updates';
121+
} else {
122+
markdownText = ':thumbsup: Alright. Here are the days I will send you the menu:';
123+
for (const weekday of weekdaysData.weekdays) {
124+
const weekdayName = weekdays[weekday - 1].replace(/[()?]/gi, '');
126125
markdownText += `\n- ${weekdayName}`;
127126
}
128127
}
129128

130-
let options = {
131-
"parse_mode": "HTML",
132-
"reply_markup": {
133-
"remove_keyboard": true,
134-
"selective": true
135-
}
129+
const options = {
130+
'parse_mode': 'HTML',
131+
'reply_markup': {
132+
'remove_keyboard': true,
133+
'selective': true,
134+
},
136135
};
137136
tgBot.sendMessage(chatId, markdownText, options);
138137
});
139138
}
140139
function cancelSubscriptionsHandler(message) {
141-
let chatId = message.chat.id;
140+
const chatId = message.chat.id;
142141

143-
subscriptions.remove(chatId,function () {
144-
let markdownText = "*Successfully removed you from the daily subscriber list* \n" +
145-
"I will no longer send you updates. :wave:";
146-
tgBot.sendMessage(chatId,markdownText,{ parse_mode: 'Markdown'});
142+
subscriptions.remove(chatId, function() {
143+
const markdownText = '*Successfully removed you from the daily subscriber list* \n' +
144+
'I will no longer send you updates. :wave:';
145+
tgBot.sendMessage(chatId, markdownText, {parse_mode: 'Markdown'});
147146
});
148147
}
149148

150149
function startHandler(message) {
151-
let chatId = message.chat.id;
150+
const chatId = message.chat.id;
152151

153-
let markdownText = 'Hello! :smile: \n' +
154-
"I can send you the menu for the SV restaurant in Zug. \n" +
152+
const markdownText = 'Hello! :smile: \n' +
153+
'I can send you the menu for the SV restaurant in Zug. \n' +
155154
`- Try /get@${botUsername} to receive today's menu. \n` +
156155
`- For regular notifications use /daily@${botUsername} or /partTime@${botUsername}`;
157156

158-
tgBot.sendMessage(chatId,markdownText, { parse_mode: 'Markdown'});
157+
tgBot.sendMessage(chatId, markdownText, {parse_mode: 'Markdown'});
159158
}
160159

161-
function getSourceHandler(message){
162-
let chatId = message.chat.id;
160+
function getSourceHandler(message) {
161+
const chatId = message.chat.id;
163162

164-
let markdownText = 'This bot is written by Fabio Zuber utilizing Node.js.\n' +
163+
const markdownText = 'This bot is written by Fabio Zuber utilizing Node.js.\n' +
165164
'The code is open source. Feel free to check it out on [GitHub](https://github.com/Sirius-A/sv-restaurant-zug-bot).';
166-
tgBot.sendMessage(chatId,markdownText,{ parse_mode: 'Markdown'});
165+
tgBot.sendMessage(chatId, markdownText, {parse_mode: 'Markdown'});
167166
}
168167

169168
/* Menu Send Functions */
170169
function sendTodaysMenu(chatId) {
171-
parser.parseToday(function (markdownText) {
170+
parser.parseToday(function(markdownText) {
172171
tgBot.sendMessage(chatId, markdownText, {parse_mode: 'Markdown'});
173172
});
174173
}
175174

176175
function sendWeekMenu(chatId) {
177-
parser.parseWeek(function (markdownText) {
176+
parser.parseWeek(function(markdownText) {
178177
tgBot.sendMessage(chatId, markdownText, {parse_mode: 'Markdown'});
179178
});
180179
}
181180

182181
function notifySubscribers() {
183-
console.log("notify Subscribers");
184-
subscriptions.forAllDailly(function (subscriber) {
182+
console.log('notify Subscribers');
183+
subscriptions.forAllDailly(function(subscriber) {
185184
sendTodaysMenu(subscriber.id);
186185
});
187186

188-
let now = new Date();
189-
subscriptions.forAllParttime(now.getDay(),function (subscriber) {
187+
const now = new Date();
188+
subscriptions.forAllParttime(now.getDay(), function(subscriber) {
190189
sendTodaysMenu(subscriber.id);
191190
})
192-
;}
191+
;
192+
}
193193

194194
/**
195195
* Regular Expression IndexOf for Arrays
196196
* This little addition to the Array prototype will iterate over array
197197
* and return the index of the first element which matches the provided
198198
* regular expression.
199199
* Note: This will not match on objects.
200-
* @param {RegExp} rx The regular expression to test with. E.g. /-ba/gim
200+
* @param {RegExp} RegExp The regular expression to test with. E.g. /-ba/gim
201201
* @return {Number} -1 means not found
202202
*/
203203
if (typeof Array.prototype.regexIndexOf === 'undefined') {
204-
Array.prototype.regexIndexOf = function (RegEx) {
205-
for (let i in this) {
204+
// eslint-disable-next-line no-extend-native
205+
Array.prototype.regexIndexOf = function(RegEx) {
206+
for (const i in this) {
206207
if (RegEx.match(this[i].toString())) {
207208
return i;
208209
}

0 commit comments

Comments
 (0)