diff --git a/README.md b/README.md index ffd3a72..afd78aa 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,6 @@ Create an alias for the node command in your `.bash_profile`, `.bashrc`, `.zshrc ```bash alias mensajs='node ~/path/to/mensajs/mensa.js' - -# You can also specify in which language you want to retrieve the menu -# To retrieve the menu in French, add the --fr argument -alias mensajs='node ~/path/to/mensajs/mensa.js --fr' ``` Changes are only available in a new shell session. To make changes immediately @@ -50,10 +46,6 @@ alias mensa='mensajs' # You can use whatever you want as an alias, like for very hungry days: alias givemefood='node ~/path/to/mensajs/mensa.js' - -# You can also specify in which language you want to retrieve the menu -# To retrieve the menu in French, add the --fr argument -alias mensajs='node ~/path/to/mensajs/mensa.js --fr' ``` To make the command available from everywhere in the system, **DO NOT** remove the ~/ prefix at the beginning! @@ -73,12 +65,6 @@ DOSKEY mensajs="node C:\path\to\mensajs\mensa.js" DOSKEY mensa="mensajs" ``` -You can also specify in which language you want to retrieve the menu. -To retrieve the menu in French, add the `--fr` argument: -```cmd -DOSKEY mensajs="node C:\path\to\mensajs\mensa.js --fr" -``` - Run `regedit` and go to `HKEY_CURRENT_USER\Software\Microsoft\Command Processor`, then choose `Add String Value` to add a new entry with the name `AutoRun` and the **full** path of your `.bat/.cmd` file. For example, `%USERPROFILE%\alias.cmd`, replacing the initial segment of the path with `%USERPROFILE%` is useful for syncing among multiple machines. This way, every time cmd is run, the aliases are loaded. The path as well as the name of your `.bat/.cmd` file are completely up to you and within your own responsibility. @@ -89,9 +75,25 @@ After performing all these steps, make sure to restart your computer. The aliase After successfully performing all required steps described above, just open a terminal of your choice, type your specified alias command and press enter to get the glorious mensa menu of today! -![Who's hungry?](assets/terminal.gif) +![Who's hungry?](assets/hungry.gif) + +## Arguments + +### Language +You can specify in which language you want to retrieve the menu. Default is German. +To retrieve the menu in French, add the `--fr` argument: +```bash +node mensa.js --fr' +``` + +### Week +You can retrieve the menu of the whole week. +To retrieve the menu of the week, add the `--week` argument: +```bash +node mensa.js --week' +``` -*Copyright (c) 2019 Guillaume Fricker* +*Copyright (c) 2022 Guillaume Fricker* ## Contributors Hall of Fame @@ -101,3 +103,5 @@ After successfully performing all required steps described above, just open a te **Joshua Drewlow (@drewjosh):** - Fixed fetching data from new website layout +- Show date on menu entry +- Added argument to retrieve week menu diff --git a/assets/hungry.gif b/assets/hungry.gif new file mode 100644 index 0000000..45cec7e Binary files /dev/null and b/assets/hungry.gif differ diff --git a/mensa.js b/mensa.js index 2631fbd..847ead1 100644 --- a/mensa.js +++ b/mensa.js @@ -56,9 +56,13 @@ if (today.getHours() > 13 && today.getDay() != 5) { const uriDE = 'https://www.bfh.ch/ti/de/ueber-das-ti/standort-infrastruktur/'; const uriFR = 'https://www.bfh.ch/ti/fr/le-ti/lieux-infrastructures/'; +// true when user wants to display whole week +let showWeek = false; + // Read the args for multilingual menu const args = process.argv.slice(1); const uri = getMultilingualURI(args); +checkForWeekArgument(args); // Setting flag before displaying the menu let dinnerReady = false; @@ -89,6 +93,12 @@ request(uri, (error, response, html) => { var dateAndTimeTitle = $(element).find('h2').text(); var date = dateAndTimeTitle.split(',')[1].replace(/\s/g, ''); // split into date and remove spaces + var day = { + date: date, + meat: {}, + vegi: {} + }; + // iterate over menu options: 1st is meat, 2nd is vegi $(element).find('.menuplan__menu').each((i, menu) => { var menuTitle = $(menu).find('.menuplan-menu__title').text(); @@ -98,8 +108,11 @@ request(uri, (error, response, html) => { var menuDescriptionHtml = $(menu).find('.menuplan-menu__description').html(); if (menuDescriptionHtml) { var menuDescriptionTemp = menuDescriptionHtml.split('
'); + // iterate over side menu options menuDescriptionTemp.forEach(element => { - menuDescription.push($('
').html(element).text()); // little hack to render ascii in UTF-8 https://stackoverflow.com/a/1912546 + if (element) { + menuDescription.push($('
').html(element).text()); // little hack to render ascii in UTF-8 https://stackoverflow.com/a/1912546 + } }); } @@ -109,12 +122,16 @@ request(uri, (error, response, html) => { menu.push(menuDescription); menu.push(menuPrice); - // only add data when from today - if (checkTodayDate(date)) { - data.push(menu); + if (i == 0) { + day.meat = menu; + } else { + day.vegi = menu; } }); + + data.push(day); }); + // time to see the results printMenu(data); } else if (error) { @@ -163,6 +180,17 @@ function getMultilingualURI(args) { return uri; } +/** + * Checks if user wants to see whole week. + */ +function checkForWeekArgument(args) { + if (args.some((val) => { + return val === '--week'; + })) { + showWeek = true; + } +} + /* * Returns the current date as dd.mm.yyyy * Formatted explicitly to match the content on site @@ -195,22 +223,40 @@ function printMenu(data) { const food = ['šŸ³', 'šŸ', 'šŸ„—', 'šŸ„˜', 'šŸŒ­', 'šŸ”', 'šŸŸ', 'šŸ„™', 'šŸ›']; console.clear(); - if (data[0]) { - console.log('\nšŸ„©'); - console.log('*************************\n', data[0][0]); - data[0][1].forEach(item => { - console.log(" - ", item); - }); - console.log('\nšŸ’µ', data[0][2]) - console.log('*************************'); + if (data.length > 0) { + + data.forEach(dayMenu => { + + if (!showWeek && checkTodayDate(dayMenu.date) || showWeek) { + // show whole week or only today depending on showWeek - console.log('\n\nšŸŒ±'); - console.log('*************************\n', data[1][0]); - data[1][1].forEach(item => { - console.log(" - ", item); + console.log('\nšŸ“…', dayMenu.date); + + if (!dayMenu.meat[0] || !dayMenu.vegi[0]) { + console.log('\n' + nodata[lang]); + console.log(alternatives[Math.floor(Math.random() * alternatives.length)][lang] + '\n'); + return; + } + + console.log('\nšŸ„©:', dayMenu.meat[0]); + dayMenu.meat[1].forEach(item => { + console.log(" - ", item); + }); + console.log('šŸ’µ', dayMenu.meat[2]) + + console.log('\nšŸŒ±:', dayMenu.vegi[0]); + dayMenu.vegi[1].forEach(item => { + console.log(" - ", item); + }); + console.log('šŸ’µ', dayMenu.vegi[2]) + + if (showWeek) { + console.log('\n------------------------------'); + } else { + console.log(); + } + } }); - console.log('\nšŸ’µ', data[1][2]) - console.log('*************************'); } else { console.log('\n' + nodata[lang]); console.log(alternatives[Math.floor(Math.random() * alternatives.length)][lang] + '\n');