Skip to content

Commit 45bddb1

Browse files
author
Joshua Drewlow
committed
replaced request with axios
1 parent 916955f commit 45bddb1

File tree

3 files changed

+248
-817
lines changed

3 files changed

+248
-817
lines changed

mensa.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const error = {
4040
de: 'Ups, da esch öppis schief glaufe. ☠️'
4141
}
4242

43-
const request = require('request');
43+
const axios = require('axios');
4444
const cheerio = require('cheerio');
4545
const today = new Date();
4646

@@ -67,6 +67,9 @@ checkForWeekArgument(args);
6767
// Setting flag before displaying the menu
6868
let dinnerReady = false;
6969

70+
// flag to determine if there was an error
71+
let errorState = false;
72+
7073
// Starting the program
7174
console.clear();
7275
walk(1);
@@ -75,13 +78,14 @@ walk(1);
7578
* Connects to the specified URI and reads today's cantina menu in Biel.
7679
* The validation process only checks against today's day to only display relevant menu entries.
7780
*/
78-
request(uri, (error, response, html) => {
81+
axios.get(uri)
82+
.then((response) => {
7983
// empty data array for results
8084
var data = [];
8185

82-
if (response && response.statusCode === 200) {
86+
if (response && response.status === 200) {
8387
// define the loaded DOM as $ for jQuery syntax
84-
var $ = cheerio.load(html);
88+
var $ = cheerio.load(response.data);
8589

8690
// get the slide track, normaly Biel/Bienne cantine should be listed first
8791
var menuDiv = $("div.comp-menuplan ul").first();
@@ -135,17 +139,24 @@ request(uri, (error, response, html) => {
135139
// time to see the results
136140
printMenu(data);
137141
} else if (error) {
138-
dinnerReady = true;
142+
errorState = true;
139143
// unexpected error in the RequestAPI
140144
console.log(error[lang]);
141145
console.error(error);
142146
} else {
143-
dinnerReady = true;
147+
errorState = true;
144148
// if this error occurs, the specified URI is invalid and/or outdated
145149
console.error('\nERR: the specified URI is invalid');
146150
console.error('=> ' + uri + '\n');
147151
}
148-
});
152+
}).catch((error)=> {
153+
errorState = true;
154+
if (error.code && (error.code =='ETIMEDOUT' || error.code == 'ENOTFOUND')) {
155+
console.log('Ha dä Jonny nöd gfunde... Schalt doch s Internet a du Pflock 🪵');
156+
} else {
157+
console.log('Es hät en Fehler gä:', error.code);
158+
}
159+
})
149160

150161
/*
151162
* Returns the BFH URI according to the specified language flag.
@@ -268,7 +279,7 @@ function printMenu(data) {
268279
* Loading animation, custom made for slow BFH network ;)
269280
*/
270281
function walk(i) {
271-
if (!dinnerReady) {
282+
if (!dinnerReady && !errorState) {
272283
const walker = ['🚶🏼', '🏃'];
273284
const text = waiting[lang];
274285
process.stdout.write(' ' + walker[i] + text);

0 commit comments

Comments
 (0)