From a20e1e7ac8a1a302e22bedb0de1f45c2185cdc1f Mon Sep 17 00:00:00 2001 From: uzurka Date: Mon, 10 Jul 2023 21:57:25 +0200 Subject: [PATCH] added Docker support with env variables --- Dockerfile | 18 ++++++++++++++ index.js | 73 ++++++++++++++++++++++++++---------------------------- 2 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b8e0bcc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM node:18-alpine + +# Create app directory +WORKDIR /usr/src/app + +# Install app dependencies +# A wildcard is used to ensure both package.json AND package-lock.json are copied +# where available (npm@5+) +COPY package*.json ./ + +RUN npm install +# If you are building your code for production +# RUN npm ci --omit=dev + +# Bundle app source +COPY . . + +CMD [ "node", "." ] \ No newline at end of file diff --git a/index.js b/index.js index 9c2bda5..a36dd5e 100644 --- a/index.js +++ b/index.js @@ -1,59 +1,56 @@ -const settings = require('./settings.json'); const fetch = require('node-fetch'); const cache = {}; const default_headers = { - "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", - "accept-language": "en-US,en;q=0.9", - "upgrade-insecure-requests": "1", - "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36" -} + 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', + 'accept-language': 'en-US,en;q=0.9', + 'upgrade-insecure-requests': '1', + 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36', +}; function isFull(t) { - return (t.includes("This beta is full.") || t.includes("This beta isn't accepting any new testers right now.")); + return (t.includes('This beta is full.') || t.includes('This beta isn\'t accepting any new testers right now.')); } function getName(t) { - return (t.includes("Join the ") ? t.split("<title>Join the ")[1].split(" - TestFlight - Apple")[0] : "App"); + return (t.includes('Join the ') ? t.split('<title>Join the ')[1].split(' - TestFlight - Apple')[0] : 'App'); } -async function do_request(a, h, m = "GET", d = null) { - let r = await fetch(a, { +async function do_request(a, h, m = 'GET', d = null) { + const r = await fetch(a, { headers: Object.assign(h), method: m, - redirect: "manual", - body: m == 'POST' ? JSON.stringify(d) : null + redirect: 'manual', + body: m == 'POST' ? JSON.stringify(d) : null, }); return (await r.text()); } -(async() => { - console.log(`Watching over ${settings.programs.length} programs`); - - setInterval(() => { - console.log("Refreshing now.") - settings.programs.forEach(async p => { - let r = await do_request("https://testflight.apple.com/join/" + p.id, default_headers); - if (!isFull(r)) { - if (!cache[p.id]) { - let message = getName(r) + " is available at https://testflight.apple.com/join/" + p.id; - if (p.url) { - await do_request( - p.url, - Object.assign(default_headers, { "Content-Type": "application/json" }), - "POST", { - "content": message - } - ) - } - console.log(message); +(async () => { + setInterval(async () => { + console.log('Refreshing now.'); + const p = { + id: process.env.TESTFLIGHT_ID, + url: process.env.DISCORD_WEBHOOK_URL, + }; + const r = await do_request('https://testflight.apple.com/join/' + p.id, default_headers); + if (!isFull(r)) { + if (!cache[p.id]) { + const message = getName(r) + ' is available at https://testflight.apple.com/join/' + p.id; + if (p.url) { + await do_request( + p.url, + Object.assign(default_headers, { 'Content-Type': 'application/json' }), + 'POST', { 'content': message }, + ); } - cache[p.id] = true; - } else { - delete cache[p.id]; - console.log(getName(r) + " is still full.") + console.log(message); } - }); - }, 30000) + cache[p.id] = true; + } else { + delete cache[p.id]; + console.log(getName(r) + ' is still full.'); + } + }, 30000); })(); \ No newline at end of file