Skip to content

Commit

Permalink
Merge pull request #13 from drewjosh/master
Browse files Browse the repository at this point in the history
Added week argument and small improvements
  • Loading branch information
frickerg authored Mar 28, 2022
2 parents 5f053d2 + 513ecc6 commit cc75063
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 34 deletions.
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!
Expand All @@ -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.
Expand All @@ -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

Expand All @@ -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
Binary file added assets/hungry.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 64 additions & 18 deletions mensa.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -98,8 +108,11 @@ request(uri, (error, response, html) => {
var menuDescriptionHtml = $(menu).find('.menuplan-menu__description').html();
if (menuDescriptionHtml) {
var menuDescriptionTemp = menuDescriptionHtml.split('<br>');
// iterate over side menu options
menuDescriptionTemp.forEach(element => {
menuDescription.push($('<div/>').html(element).text()); // little hack to render ascii in UTF-8 https://stackoverflow.com/a/1912546
if (element) {
menuDescription.push($('<div/>').html(element).text()); // little hack to render ascii in UTF-8 https://stackoverflow.com/a/1912546
}
});
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit cc75063

Please sign in to comment.