Skip to content

Instantly share code, notes, and snippets.

@c0m4r
Last active November 3, 2024 09:12
Show Gist options
  • Save c0m4r/76c8497c34a093dfc63fbf02537c327d to your computer and use it in GitHub Desktop.
Save c0m4r/76c8497c34a093dfc63fbf02537c327d to your computer and use it in GitHub Desktop.
Email notifications with weekly Epic Store Free Games

Epic Store Free Games email notifications

OS Setup: Linux (any), Postfix as a forwarder

Depends on: nodejs + AuroPick/epic-free-games

It's best to run cron once every 2-3 days, so as not to miss the release time window. The script compares with the last run to avoid sending duplicate emails.

v2: JS only

mkdir -p /root/bin/epic
cd /root/bin/epic
wget https://gist.githubusercontent.com/c0m4r/76c8497c34a093dfc63fbf02537c327d/raw/epic.cjs
wget https://gist.githubusercontent.com/c0m4r/76c8497c34a093dfc63fbf02537c327d/raw/package.json
npm update # or npm install epic-free-games nodemailer fs eslint

Cron

0 0 */3 * * /usr/bin/node /root/bin/epic/epic.cjs > /root/bin/epic/epic.log 2>&1

Linter

wget https://gist.githubusercontent.com/c0m4r/76c8497c34a093dfc63fbf02537c327d/raw/bb9cf89ebfcf49d3997ce5c6702b0a6b0d13c212/eslint.config.js
npx eslint epic.cjs

v1: Bash + JS

npm install epic-free-games nodemailer
mkdir -p /root/bin/epic
cd /root/bin/epic
wget https://gist.githubusercontent.com/c0m4r/76c8497c34a093dfc63fbf02537c327d/raw/epic.js
wget https://gist.githubusercontent.com/c0m4r/76c8497c34a093dfc63fbf02537c327d/raw/epic.sh
chmod o+x epic.sh

Cron

0 0 */3 * * /root/bin/epic/epic.sh > /root/bin/epic/epic.log 2>&1
// Epic Free Games Newsletter
// https://www.npmjs.com/package/epic-free-games
// $ npm install epic-free-games nodemailer fs
const mail_from = "[email protected]";
const mails = [
"[email protected]",
"[email protected]"
];
const fs = require('fs')
const nodemailer = require('nodemailer');
const { EpicFreeGames } = require('epic-free-games');
const epicFreeGames = new EpicFreeGames({ country: 'PL', locale: 'pl', includeAll: true })
let last = "";
fs.readFile(__dirname + '/epic.last', (err, inputD) => {
if (err) {
last = "";
} else {
last = inputD.toString();
}
});
function write_last(content) {
fs.writeFile(__dirname + '/epic.last', content, err => {
if (err) console.log(err);
});
}
// mailer setup
const transporter = nodemailer.createTransport({
host: "localhost",
port: 25,
tls: { rejectUnauthorized: false, },
});
function send_epic_mail(mail_to, mail_msg) {
const mailOptions = {
from: mail_from,
to: mail_to,
subject: 'Epic free games',
html: `<h3>Epic free games</h3><table border="1">${mail_msg}</table>`
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log(`Email to ${mail_to} sent: ${info.response}`);
}
});
}
// looking for free games...
epicFreeGames.getGames().then(res => {
let message = "";
res.currentGames.forEach(function(item) {
console.log(item.title);
message = `${message}<tr>
<td><a href="https://store.epicgames.com/pl/browse?q=${encodeURIComponent(item.title)}">${item.title}</a></td>
<td>${item.description}</td>
</tr>\n`;
});
// sending notifications
if(last != message) {
mails.forEach(mail => {
send_epic_mail(mail, message);
});
write_last(message);
} else {
console.log("no new games since last run");
}
}).catch(err => {
console.log(err);
});
// Epic Free Games
// https://www.npmjs.com/package/epic-free-games
// $ npm install epic-free-games nodemailer
const { EpicFreeGames } = require('epic-free-games');
const epicFreeGames = new EpicFreeGames({ country: 'PL', locale: 'pl', includeAll: true })
epicFreeGames.getGames().then(res => {
var message = "";
res.currentGames.forEach(function(item) {
message = message + "GAME:<tr><td><a href=\"https://store.epicgames.com/pl/browse?q=" + encodeURIComponent(item.title) + "\">" + item.title + "</a></td><td>" + item.description + "</td></tr>\n";
});
console.log(message);
}).catch(err => {
console.log(err);
});
#!/bin/bash
# Epic Free Games Newsletter
set -e
cd /root/bin/epic
EPIC=$(/usr/bin/node epic.js | grep ^GAME | sed 's/^GAME\://g;')
if [ ! -e epic.last ]; then
echo $EPIC > epic.last
else
echo $EPIC > epic.new
set +e
cmp --silent epic.last epic.new && echo "skip, no change since last run" && exit 0
set -e
mv epic.new epic.last
fi
send_epic_mail() {
echo "sending to ${1}"
echo -e "To: ${1}\nFrom: [email protected]\nContent-Type: text/html;\nSubject: Epic free games\n\n<h3>Epic free games</h3><table border=\"1\">${EPIC}</table>" | sendmail -t
}
if [[ "$EPIC" ]]; then
send_epic_mail "[email protected]"
send_epic_mail "[email protected]"
else
echo "error: no output from epic.js"
fi
export default [
{
files: ["**/*.cjs"],
rules: {}
}
];
{
"type": "module",
"dependencies": {
"epic-free-games": "^4.0.2",
"eslint": "^9.12.0",
"fs": "^0.0.1-security",
"nodemailer": "^6.9.15"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment