Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firebase support #12

Merged
merged 8 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import fs from 'fs'
import path from 'path'
import { spawnSync } from 'child_process'

import fourD from './lib/fourD.js'
import toto from './lib/toto.js'
import sweep from './lib/sweep.js'
import getLottery from './lib/getLottery.js';
import getLotteryTest from './lib/getLotteryTest.js';


const isProduction = process.env.NODE_ENV === 'production'
Expand All @@ -17,31 +16,24 @@ if (!fs.existsSync('temp')) {

const main = async () => {

var oldList = {}

const filename = path.join('temp', 'sglottery.json')
if (fs.existsSync(filename)) {
oldList = JSON.parse(fs.readFileSync(filename, 'utf8'));
fs.unlinkSync(filename)
}

const browser = await puppeteer.launch({
headless: isProduction,
args: isProduction ? ['--no-sandbox'] : [],
})

var results = [ fourD(browser), toto(browser), sweep(browser) ]

var sgLottery = await Promise.all(results)
.then((values) => {
return {
FourD: values[0],
Toto: values[1],
Sweep: values[2]
}
})
.catch((error) => console.error(error))
var sgLottery = isProduction ? await getLottery(browser, oldList) : await getLotteryTest(browser, oldList);

fs.writeFileSync(filename, JSON.stringify(sgLottery, null, isProduction ? 0 : 2))
fs.writeFileSync(filename, JSON.stringify(sgLottery, null, isProduction ? 0 : 2));

await browser.close()
await browser.close();

if (process.env.GITHUB_TOKEN) {
spawnSync(
Expand All @@ -59,7 +51,6 @@ const main = async () => {
)
}


return null
}

Expand Down
83 changes: 83 additions & 0 deletions lib/firebase_push.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* @param import Topic Key
*
* Send push notifications via firebase
**/

import {default as admin} from 'firebase-admin';
import util from 'util';

const topics_dict = {
"4D": "fourD",
"Toto": "toto",
"Sweep": "sweep",
}
class Firebase {

constructor(){
this.app = admin.initializeApp({
credential: admin.credential.cert({
"project_id": process.env.FIREBASE_PROJECT_ID,
"private_key": process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n'),
"client_email": process.env.FIREBASE_CLIENT_EMAIL,
}),
databaseURL: process.env.FIREBASE_DATABASE_URL
});
}

pushTopic(id) {
let topics = topics_dict[id];

if (process.env.NODE_ENV !== 'production') {
topics += "-test";
}

const message = {
notification: {
title: id + " Results",
body: "See the latest results",
},
topic: topics
};

this.app
.messaging()
.send(message)
.then(response => {
console.log(util.format('[%s]: %s', id, response));
})
}

pushTestTopic(id) {

if (process.env.NODE_ENV !== 'production') {
let topics = topics_dict[id] + "-test";

const message = {
notification: {
title: id + " Results",
body: "See the latest results",
},
topic: topics
};

this.app
.messaging()
.send(message)
.then(response => {
console.log(util.format('[%s]: %s', id, response));
})

}
}

exit(){
this.app.delete()
.then(() => console.log("[Firebase]: Terminated sucessfully"))
.catch((error) => console.log("[Firebase Error]:", error));
}
}

const FirebaseSingleton = new Firebase();
Object.freeze(FirebaseSingleton);
export default FirebaseSingleton;
4 changes: 3 additions & 1 deletion lib/fourD.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param returns Promise(list)
*/

export default async function fourD(browser) {
const fourD = async (browser) => {
const page = await browser.newPage()
await page.goto("http://www.singaporepools.com.sg/en/product/Pages/4d_results.aspx")
const results = await page.evaluate( () => {
Expand Down Expand Up @@ -44,3 +44,5 @@ export default async function fourD(browser) {
console.log(`[4D] - scraped ${results.length} items`)
return results
}

export default fourD;
106 changes: 106 additions & 0 deletions lib/getLottery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* @param {import('puppeteer').Browser} browser
* @param import previousLottery
* @param returns Promise(list)
*
* Get fourD, toto, sweep
* If respective methods returns an empty list, repeat again
*
* Compare with previous lottery result,
* If results obtain are different, (aka new results are fetched)
* send push notification via firebase
*/

import fourD from './fourD.js'
import toto from './toto.js'
import sweep from './sweep.js'
import firebase from './firebase_push.js';

const getLottery = async (browser, lotteryList = {}) => {

let lottery = await Promise.all([
getFourD(browser, lotteryList["FourD"]),
getToto (browser, lotteryList["Toto"]),
getSweep(browser, lotteryList["Sweep"])
]).then((values) => {
return {
FourD: values[0],
Toto: values[1],
Sweep: values[2]
}
}).catch((error) => {
console.log(error);
console.log("[ERROR]: Using previous results")
return lotteryList;
}).finally(() => firebase.exit());

return lottery;
}

const getFourD = async (browser, oldFourD = []) => {
let exit = false;
let fourDList = [];

while (!exit) {
fourDList = await fourD(browser);
// Is not an empty list
if (fourDList.length !== 0 ) {
exit = true;
}
}

// Compare old and new
if (JSON.stringify(fourDList) !== JSON.stringify(oldFourD)) {
console.log("[4D]: Fetched new data");
firebase.pushTopic("4D");
} else {
console.log("[4D]: No difference in data");
}
return fourDList;
}

const getToto = async (browser, oldToto = []) => {
let exit = false;
let totoList = [];

while (!exit) {
totoList = await toto(browser);
// Is not an empty list
if (totoList.length !== 0 ) {
exit = true;
}
}

// Compare old and new
if (JSON.stringify(totoList) !== JSON.stringify(oldToto)) {
console.log("[TOTO]: Fetched new data");
firebase.pushTopic("Toto");
} else {
console.log("[TOTO]: No difference in data");
}
return totoList;
}

const getSweep = async (browser, oldSweep = []) => {
let exit = false;
let sweepList = [];

while (!exit) {
sweepList = await sweep(browser);
// Is not an empty list
if (sweepList.length !== 0 ) {
exit = true;
}
}

// Compare old and new
if (JSON.stringify(sweepList) !== JSON.stringify(oldSweep)) {
console.log("[SWEEP]: Fetched new data");
firebase.pushTopic("Sweep");
} else {
console.log("[SWEEP]: No difference in data");
}
return sweepList;
}

export default getLottery;
108 changes: 108 additions & 0 deletions lib/getLotteryTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* @param {import('puppeteer').Browser} browser
* @param import previousLottery
* @param returns Promise(list)
*
* Get fourD, toto, sweep
* If respective methods returns an empty list, repeat again
*
* Compare with previous lottery result,
* If results obtain are different, (aka new results are fetched)
* send push notification via firebase
*
* Only run in dev environment
*/

import fourD from './fourD.js'
import toto from './toto.js'
import sweep from './sweep.js'
import firebase from './firebase_push.js';

const getLotteryTest = async (browser, lotteryList = {}) => {

let lottery = await Promise.all([
getFourD(browser, lotteryList["FourD"]),
getToto (browser, lotteryList["Toto"]),
getSweep(browser, lotteryList["Sweep"])
]).then((values) => {
return {
FourD: values[0],
Toto: values[1],
Sweep: values[2]
}
}).catch((error) => {
console.log(error);
console.log("[ERROR]: Using previous results")
return lotteryList;
}).finally(() => firebase.exit());

return lottery;
}

const getFourD = async (browser, oldFourD = []) => {
let exit = false;
let fourDList = [];

while (!exit) {
fourDList = await fourD(browser);
// Is not an empty list
if (fourDList.length !== 0 ) {
exit = true;
}
}

// Compare old and new
if (JSON.stringify(fourDList) !== JSON.stringify(oldFourD)) {
console.log("[4D]: Fetched new data");
} else {
console.log("[4D]: No difference in data");
}
firebase.pushTestTopic("4D");
return fourDList;
}

const getToto = async (browser, oldToto = []) => {
let exit = false;
let totoList = [];

while (!exit) {
totoList = await toto(browser);
// Is not an empty list
if (totoList.length !== 0 ) {
exit = true;
}
}

// Compare old and new
if (JSON.stringify(totoList) !== JSON.stringify(oldToto)) {
console.log("[TOTO]: Fetched new data");
} else {
console.log("[TOTO]: No difference in data");
}
firebase.pushTestTopic("Toto");
return totoList;
}

const getSweep = async (browser, oldSweep = []) => {
let exit = false;
let sweepList = [];

while (!exit) {
sweepList = await sweep(browser);
// Is not an empty list
if (sweepList.length !== 0 ) {
exit = true;
}
}

// Compare old and new
if (JSON.stringify(sweepList) !== JSON.stringify(oldSweep)) {
console.log("[SWEEP]: Fetched new data");
} else {
console.log("[SWEEP]: No difference in data");
}
firebase.pushTestTopic("Sweep");
return sweepList;
}

export default getLotteryTest;
4 changes: 3 additions & 1 deletion lib/sweep.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param returns Promise(list)
*/

export default async function sweep(browser) {
const sweep = async (browser) => {
const page = await browser.newPage()
await page.goto("http://www.singaporepools.com.sg/en/product/Pages/sweep_results.aspx")
const results = await page.evaluate( () => {
Expand Down Expand Up @@ -69,3 +69,5 @@ export default async function sweep(browser) {
console.log(`[SWEEP] - scraped ${results.length} items`)
return results
}

export default sweep
Loading