Skip to content

Commit

Permalink
Merge branch 'developer'
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Drewlow committed May 27, 2024
2 parents cc75063 + a4d6232 commit 91c9460
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 819 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ node mensa.js --week'
- Fixed fetching data from new website layout
- Show date on menu entry
- Added argument to retrieve week menu
- replaced request with axios
27 changes: 19 additions & 8 deletions mensa.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const error = {
de: 'Ups, da esch öppis schief glaufe. ☠️'
}

const request = require('request');
const axios = require('axios');
const cheerio = require('cheerio');
const today = new Date();

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

// flag to determine if there was an error
let errorState = false;

// Starting the program
console.clear();
walk(1);
Expand All @@ -75,13 +78,14 @@ walk(1);
* Connects to the specified URI and reads today's cantina menu in Biel.
* The validation process only checks against today's day to only display relevant menu entries.
*/
request(uri, (error, response, html) => {
axios.get(uri)
.then((response) => {
// empty data array for results
var data = [];

if (response && response.statusCode === 200) {
if (response && response.status === 200) {
// define the loaded DOM as $ for jQuery syntax
var $ = cheerio.load(html);
var $ = cheerio.load(response.data);

// get the slide track, normaly Biel/Bienne cantine should be listed first
var menuDiv = $("div.comp-menuplan ul").first();
Expand Down Expand Up @@ -135,17 +139,24 @@ request(uri, (error, response, html) => {
// time to see the results
printMenu(data);
} else if (error) {
dinnerReady = true;
errorState = true;
// unexpected error in the RequestAPI
console.log(error[lang]);
console.error(error);
} else {
dinnerReady = true;
errorState = true;
// if this error occurs, the specified URI is invalid and/or outdated
console.error('\nERR: the specified URI is invalid');
console.error('=> ' + uri + '\n');
}
});
}).catch((error)=> {
errorState = true;
if (error.code && (error.code =='ETIMEDOUT' || error.code == 'ENOTFOUND')) {
console.log('Ha dä Jonny nöd gfunde... Schalt doch s Internet a du Pflock 🪵');
} else {
console.log('Es hät en Fehler gä:', error.code);
}
})

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

0 comments on commit 91c9460

Please sign in to comment.