Skip to content

Commit

Permalink
feat: add redis (#1390)
Browse files Browse the repository at this point in the history
  • Loading branch information
BaileyMillerSSI committed Dec 26, 2020
1 parent 90fb430 commit fb82526
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
8 changes: 7 additions & 1 deletion docs/reference/notification.md
Expand Up @@ -142,6 +142,12 @@ Generate token at https://pushover.net/apps/build.
???+ note
`PUSHOVER_EXPIRE` and `PUSHOVER_RETRY` are only used when `PUSHOVER_PRIORITY="2"`

## Redis

| Environment variable | Description |
|:---:|---|
| `REDIS_URL` | Connection string in format (redis://[username]:[password]@[host][:port]/[database-id]) (redis://localhost:6379/1) |

## Slack

| Environment variable | Description |
Expand Down Expand Up @@ -190,4 +196,4 @@ Generate all Twitter keys at: https://developer.twitter.com/
| `TWITCH_CLIENT_SECRET`| Twitch client secret |
| `TWITCH_ACCESS_TOKEN` | Twitch access token |
| `TWITCH_REFRESH_TOKEN` | Twitch refresh token |
| `TWITCH_CHANNEL` | Twitch channel |
| `TWITCH_CHANNEL` | Twitch channel |
42 changes: 42 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -29,6 +29,7 @@
"@jef/pushbullet": "^2.4.3",
"@slack/web-api": "^5.14.0",
"@types/random-useragent": "^0.3.0",
"@types/redis": "^2.8.28",
"chalk": "^4.1.0",
"cheerio": "^1.0.0-rc.3",
"discord.js": "^12.5.1",
Expand All @@ -48,6 +49,7 @@
"puppeteer-extra-plugin-block-resources": "^2.2.7",
"puppeteer-extra-plugin-stealth": "^2.6.5",
"pushover-notifications": "^1.2.2",
"redis": "^3.0.2",
"top-user-agents": "^1.0.19",
"twilio": "^3.54.1",
"twitch": "^4.3.6",
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Expand Up @@ -279,6 +279,9 @@ const notifications = {
token: envOrString(process.env.PUSHOVER_TOKEN),
username: envOrString(process.env.PUSHOVER_USER)
},
redis: {
url: envOrString(process.env.REDIS_URL)
},
slack: {
channel: envOrString(process.env.SLACK_CHANNEL),
token: envOrString(process.env.SLACK_TOKEN)
Expand Down
2 changes: 2 additions & 0 deletions src/notification/notification.ts
Expand Up @@ -14,6 +14,7 @@ import {sendTelegramMessage} from './telegram';
import {sendTweet} from './twitter';
import {sendTwilioMessage} from './twilio';
import {sendTwitchMessage} from './twitch';
import updateRedis from './redis';

export function sendNotification(link: Link, store: Store) {
// Priority
Expand All @@ -33,4 +34,5 @@ export function sendNotification(link: Link, store: Store) {
sendTweet(link, store);
sendTwilioMessage(link, store);
sendTwitchMessage(link, store);
updateRedis(link, store);
}
40 changes: 40 additions & 0 deletions src/notification/redis.ts
@@ -0,0 +1,40 @@
import {Link, Store} from '../store/model';
import {config} from '../config';
import {logger} from '../logger';
import redis from 'redis';

const {url} = config.notifications.redis;

const client = redis.createClient({
url
});

const updateRedis = (link: Link, store: Store) => {
try {
if (url) {
const key = `${store.name}:${link.brand}:${link.model}`
.split(' ')
.join('-');

const value = {
...link,
labels: store.labels,
links: store.links,
name: store.name,
updatedAt: new Date().toUTCString()
};

const redisUpdated = client.set(key, JSON.stringify(value));

if (redisUpdated) {
logger.info('✔ redis updated');
} else {
logger.error(`✖ couldn't update redis for key (${key})`);
}
}
} catch (error: unknown) {
logger.error("✖ couldn't update redis", error);
}
};

export default updateRedis;

0 comments on commit fb82526

Please sign in to comment.