@@ -40,7 +40,7 @@ const error = {
40
40
de : 'Ups, da esch öppis schief glaufe. ☠️'
41
41
}
42
42
43
- const request = require ( 'request ' ) ;
43
+ const axios = require ( 'axios ' ) ;
44
44
const cheerio = require ( 'cheerio' ) ;
45
45
const today = new Date ( ) ;
46
46
@@ -67,6 +67,9 @@ checkForWeekArgument(args);
67
67
// Setting flag before displaying the menu
68
68
let dinnerReady = false ;
69
69
70
+ // flag to determine if there was an error
71
+ let errorState = false ;
72
+
70
73
// Starting the program
71
74
console . clear ( ) ;
72
75
walk ( 1 ) ;
@@ -75,13 +78,14 @@ walk(1);
75
78
* Connects to the specified URI and reads today's cantina menu in Biel.
76
79
* The validation process only checks against today's day to only display relevant menu entries.
77
80
*/
78
- request ( uri , ( error , response , html ) => {
81
+ axios . get ( uri )
82
+ . then ( ( response ) => {
79
83
// empty data array for results
80
84
var data = [ ] ;
81
85
82
- if ( response && response . statusCode === 200 ) {
86
+ if ( response && response . status === 200 ) {
83
87
// define the loaded DOM as $ for jQuery syntax
84
- var $ = cheerio . load ( html ) ;
88
+ var $ = cheerio . load ( response . data ) ;
85
89
86
90
// get the slide track, normaly Biel/Bienne cantine should be listed first
87
91
var menuDiv = $ ( "div.comp-menuplan ul" ) . first ( ) ;
@@ -135,17 +139,24 @@ request(uri, (error, response, html) => {
135
139
// time to see the results
136
140
printMenu ( data ) ;
137
141
} else if ( error ) {
138
- dinnerReady = true ;
142
+ errorState = true ;
139
143
// unexpected error in the RequestAPI
140
144
console . log ( error [ lang ] ) ;
141
145
console . error ( error ) ;
142
146
} else {
143
- dinnerReady = true ;
147
+ errorState = true ;
144
148
// if this error occurs, the specified URI is invalid and/or outdated
145
149
console . error ( '\nERR: the specified URI is invalid' ) ;
146
150
console . error ( '=> ' + uri + '\n' ) ;
147
151
}
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
+ } )
149
160
150
161
/*
151
162
* Returns the BFH URI according to the specified language flag.
@@ -268,7 +279,7 @@ function printMenu(data) {
268
279
* Loading animation, custom made for slow BFH network ;)
269
280
*/
270
281
function walk ( i ) {
271
- if ( ! dinnerReady ) {
282
+ if ( ! dinnerReady && ! errorState ) {
272
283
const walker = [ '🚶🏼' , '🏃' ] ;
273
284
const text = waiting [ lang ] ;
274
285
process . stdout . write ( ' ' + walker [ i ] + text ) ;
0 commit comments