twitch-drops-watcher
Version:
Script to periodically scan for Twitch Drops for a given game.
38 lines (37 loc) • 1.66 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
class TwitchAPIClient {
client;
constructor(clientId, credentials) {
this.client = axios_1.default.create({
baseURL: 'https://api.twitch.tv/helix',
headers: {
Authorization: `Bearer ${credentials.access_token}`,
'Client-Id': clientId,
},
});
}
static async create(clientId, clientSecret) {
return axios_1.default
.post('https://id.twitch.tv/oauth2/token', `client_id=${clientId}&client_secret=${clientSecret}&grant_type=client_credentials`, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
.then((res) => new TwitchAPIClient(clientId, res.data));
}
async getTwitchGames(gameNames) {
return this.client.get(`/games?name=${gameNames.join('&name=')}`).then((res) => res.data.data);
}
async getLiveStreams(gameIds, dropsEnabled = false) {
const dropsEnabledTagId = 'c2542d6d-cd10-4532-919b-3d19f30a768b';
return this.client
.get(`/streams?game_id=${gameIds.join('&game_id=')}`)
.then((res) => res.data.data)
.then((streams) => dropsEnabled ? streams.filter((stream) => stream.tag_ids.includes(dropsEnabledTagId)) : streams);
}
static buildTwitchURL(stream) {
return `https://www.twitch.tv/${stream.user_login}`;
}
}
exports.default = TwitchAPIClient;