UNPKG

twitch-drops-watcher

Version:

Script to periodically scan for Twitch Drops for a given game.

56 lines (55 loc) 2.6 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const commander_1 = require("commander"); const email_1 = __importDefault(require("./email")); const env_1 = __importDefault(require("./env")); const TwitchAPIClient_1 = __importDefault(require("./TwitchAPIClient")); async function main() { commander_1.program .name('twitch-drops-watcher') .description('CLI tool to check for Twitch live streams with Drops Enabled for a given list of games.') .version(require('../package.json').version) .arguments('<games...>') .option('-d, --drops', 'only show live streams with Twitch Drops enabled.', false) .option('-e, --env <path>', 'path to the .env file to load', './.env'); commander_1.program.parse(); const options = commander_1.program.opts(), gameNames = commander_1.program.args; const { twitch, google } = (0, env_1.default)(options.env); const client = await TwitchAPIClient_1.default.create(twitch.clientId, twitch.clientSecret); const liveStreams = await client.getTwitchGames(gameNames).then((games) => client.getLiveStreams(games.map(({ id }) => id), options.drops)); if (liveStreams.length > 0) { liveStreams.forEach((stream) => { console.log(`${stream.user_name} is playing ${stream.game_name} live at ${TwitchAPIClient_1.default.buildTwitchURL(stream)}!`); }); const html = liveStreams .map((stream) => { const url = TwitchAPIClient_1.default.buildTwitchURL(stream); return `<div>${stream.user_name} is playing ${stream.game_name} live at <a href="${url}">${url}</a>!<div>`; }) .join('\n'); try { await new email_1.default(google).sendEmail({ to: google.gmailAddress, subject: `Twitch Drops Watcher: someone${options.drops ? ' with Drops enabled' : ''} is live!`, html, }); console.log(`Email sent to ${google.gmailAddress}.`); } catch (err) { console.error('An error occurred while sending the email. Check your Google credentials.'); throw err; } } else { console.log([ 'No one is live for the following games:', ...gameNames, "Check the game names, remove the 'Drops Enabled' filter, or come back later.", ].join('\n')); } } main();