diff --git a/app.js b/app.js index bee7856..de48ec6 100644 --- a/app.js +++ b/app.js @@ -5,11 +5,13 @@ const cheerio = require('cheerio'); var fs = require('fs'); const inquirer = require('./input'); const treekill = require('tree-kill'); +const axios = require('axios') var run = true; var firstRun = true; -var cookie = null; -var streamers = null; +var cookie; +var streamers; +var isDropped = false; // ========================================== CONFIG SECTION ================================================================= const configPath = './config.json' const screenshotFolder = './screenshots/'; @@ -57,15 +59,28 @@ const streamPauseQuery = 'button[data-a-target="player-play-pause-button"]'; const streamSettingsQuery = '[data-a-target="player-settings-button"]'; const streamQualitySettingQuery = '[data-a-target="player-settings-menu-item-quality"]'; const streamQualityQuery = 'input[data-a-target="tw-radio"]'; +const listNotificationRaw = '{"operationName":"OnsiteNotifications_ListNotifications","variables":{"limit":3,"cursor":"","language":"en"},"extensions":{"persistedQuery":{"version":1,"sha256Hash":"b317b93ed481bf07c35defbcf01848f09744805ebb640734763298f9a7dfd64f"}}}' // ========================================== CONFIG SECTION ================================================================= - +async function checkDrop(){ + await axios.post('https://gql.twitch.tv/gql', listNotificationRaw, {headers: { Authorization: "OAuth " + cookie[0].value }}).then( + response=>{ + isDropped = JSON.stringify(response.data.data.currentUser.notifications.edges).includes('You just received the **VALORANT**') + } + ) +} async function viewRandomPage(browser, page) { var streamer_last_refresh = dayjs().add(streamerListRefresh, streamerListRefreshUnit); var browser_last_refresh = dayjs().add(browserClean, browserCleanUnit); while (run) { try { + await checkDrop() + if (isDropped){ + console.log('Valorant has been dropped') + await shutDown() + } + if (dayjs(browser_last_refresh).isBefore(dayjs())) { var newSpawn = await cleanup(browser, page); browser = newSpawn.browser; @@ -74,6 +89,8 @@ async function viewRandomPage(browser, page) { browser_last_refresh = dayjs().add(browserClean, browserCleanUnit); } + + if (dayjs(streamer_last_refresh).isBefore(dayjs())) { await getAllStreamer(page); //Call getAllStreamer function and refresh the list streamer_last_refresh = dayjs().add(streamerListRefresh, streamerListRefreshUnit); //https://github.com/D3vl0per/Valorant-watcher/issues/25 @@ -245,14 +262,11 @@ async function getAllStreamer(page) { console.log('📡 Checking active streamers...'); await scroll(page, scrollTimes); const jquery = await queryOnWebsite(page, channelsQuery); - streamers = null; - streamers = new Array(); - + streamers = [] console.log('🧹 Filtering out html codes...'); for (var i = 0; i < jquery.length; i++) { streamers[i] = jquery[i].attribs.href.split("/")[1]; } - return; } @@ -366,4 +380,4 @@ async function main() { main(); process.on("SIGINT", shutDown); -process.on("SIGTERM", shutDown); +process.on("SIGTERM", shutDown); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 871ba97..96e93fb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -51,6 +51,14 @@ "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz", "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==" }, + "axios": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", + "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", + "requires": { + "follow-redirects": "1.5.10" + } + }, "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", @@ -300,6 +308,24 @@ "escape-string-regexp": "^1.0.5" } }, + "follow-redirects": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", + "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", + "requires": { + "debug": "=3.1.0" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + } + } + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", diff --git a/package.json b/package.json index 0a54068..6583f0d 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "url": "https://github.com/D3vl0per/Valorant-watcher" }, "dependencies": { + "axios": "^0.19.2", "cheerio": "^1.0.0-rc.3", "dayjs": "^1.8.24", "dotenv": "^8.2.0",