Browse Source

Skinny 4G Broadband balance checker

Checks the balance to automatically apply daily Data Binge packs.
Can be configured to run frequently using cron.
Jason Schwarzenberger 11 months ago
parent
commit
11c4351bdc
5 changed files with 123 additions and 6 deletions
  1. BIN
      .gitignore
  2. 37 0
      package-lock.json
  3. 1 0
      package.json
  4. 30 6
      renewal.js
  5. 55 0
      renewal.log

BIN
.gitignore


+ 37 - 0
package-lock.json

@@ -17,6 +17,15 @@
       "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz",
       "integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ=="
     },
+    "axios": {
+      "version": "0.19.0",
+      "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.0.tgz",
+      "integrity": "sha512-1uvKqKQta3KBxIz14F2v06AEHZ/dIoeKfbTRkK1E5oqjDnuEerLmYTgJB5AiQZHJcljpg1TuRzdjDR06qNk0DQ==",
+      "requires": {
+        "follow-redirects": "1.5.10",
+        "is-buffer": "^2.0.2"
+      }
+    },
     "balanced-match": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
@@ -117,6 +126,29 @@
         "pend": "~1.2.0"
       }
     },
+    "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"
+          }
+        },
+        "ms": {
+          "version": "2.0.0",
+          "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
+          "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
+        }
+      }
+    },
     "fs.realpath": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
@@ -168,6 +200,11 @@
       "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
       "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
     },
+    "is-buffer": {
+      "version": "2.0.4",
+      "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.4.tgz",
+      "integrity": "sha512-Kq1rokWXOPXWuaMAqZiJW4XxsmD9zGx9q4aePabbn3qCRGedtH7Cm+zV8WETitMfu1wdh+Rvd6w5egwSngUX2A=="
+    },
     "isarray": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",

+ 1 - 0
package.json

@@ -9,6 +9,7 @@
   "author": "",
   "license": "ISC",
   "dependencies": {
+    "axios": "^0.19.0",
     "dotenv": "^8.2.0",
     "puppeteer": "^2.0.0"
   }

+ 30 - 6
renewal.js

@@ -1,4 +1,5 @@
 require('dotenv').config();
+const axios = require('axios');
 const puppeteer = require('puppeteer');
 
 const urls = {
@@ -64,9 +65,30 @@ const purchase_addon = async page => {
     });
 };
 
+const notify_purchase = async ({balance, quota, ratio}, purchase) => {
+  const token = process.env.SKINNY_BOT;
+  const url = `https://api.telegram.org/bot${token}/sendMessage`;
+  const params = {
+    chat_id: process.env.SKINNY_CHAT_ID,
+    parse_mode: 'markdown',
+    disable_web_page_preview: true,
+    text: `*Skinny 4G Balance*
+= Remaining: ${(ratio * 100).toFixed(2)}%
+= Data: ${balance}GB of ${quota}GB
+= Threshold: ${(process.env.SKINNY_MINIMUM*100).toFixed(0)}%
+${purchase ? '= *Purchased: $5 Unlimited Data Boost*\n' : ''}= https://reg.t0.vc/1j.nz-skinny`.trim()
+  };
+  try {
+    await axios.post(url, params);
+    console.log(`notification sent.`);
+  } catch (e) {
+    console.error(e);
+  }
+};
+
 (async () => {
   const browser = await puppeteer.launch({
-    headless: false
+    headless: true
   });
   const page = await browser.newPage();
 
@@ -74,15 +96,17 @@ const purchase_addon = async page => {
   const { balance, quota, ratio } = await scrape_balance(page);
 
   console.log('balance:', {balance, quota, ratio});
+  console.log('threshold: ', process.env.SKINNY_MINIMUM);
 
   if (ratio < process.env.SKINNY_MINIMUM) {
     console.log('purchasing addon.');
       await purchase_addon(page);
+      await notify_purchase({balance, quota, ratio}, true);
+  } else {
+    await notify_purchase({balance, quota, ratio}, false);
   }
 
-  setTimeout(async () => {
-    console.log('closing chrome.');
-    await page.close();
-    await browser.close();
-  }, 1000);
+  console.log('closing chrome.');
+  await page.close();
+  await browser.close();
 })();

+ 55 - 0
renewal.log

@@ -0,0 +1,55 @@
+Sun 05 Jan 2020 11:04:06 AM UTC
+balance: { balance: 117, quota: 120, ratio: 0.975 }
+threshold:  0.1
+closing chrome.
+
+Sun 05 Jan 2020 12:00:01 PM UTC
+balance: { balance: 117, quota: 120, ratio: 0.975 }
+threshold:  0.1
+closing chrome.
+
+Sun 05 Jan 2020 10:03:20 PM UTC
+balance: { balance: 111, quota: 120, ratio: 0.925 }
+threshold:  0.1
+closing chrome.
+
+Mon 06 Jan 2020 12:00:01 AM UTC
+balance: { balance: 110, quota: 120, ratio: 0.9166666666666666 }
+threshold:  0.1
+closing chrome.
+
+Mon 06 Jan 2020 06:00:01 AM UTC
+balance: { balance: 106, quota: 120, ratio: 0.8833333333333333 }
+threshold:  0.1
+closing chrome.
+
+Mon 06 Jan 2020 08:52:50 AM UTC
+balance: { balance: 103, quota: 120, ratio: 0.8583333333333333 }
+threshold:  0.1
+closing chrome.
+
+Mon 06 Jan 2020 12:00:01 PM UTC
+balance: { balance: 101, quota: 120, ratio: 0.8416666666666667 }
+threshold:  0.1
+closing chrome.
+
+Mon 06 Jan 2020 06:00:01 PM UTC
+balance: { balance: 100, quota: 120, ratio: 0.8333333333333334 }
+threshold:  0.1
+closing chrome.
+
+Tue 07 Jan 2020 12:00:01 AM UTC
+balance: { balance: 95, quota: 120, ratio: 0.7916666666666666 }
+threshold:  0.1
+closing chrome.
+
+Tue 07 Jan 2020 02:34:06 AM UTC
+balance: { balance: 94, quota: 120, ratio: 0.7833333333333333 }
+threshold:  0.1
+closing chrome.
+
+Tue 07 Jan 2020 06:00:01 AM UTC
+balance: { balance: 91, quota: 120, ratio: 0.7583333333333333 }
+threshold:  0.1
+closing chrome.
+