-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphUpdate.js
157 lines (144 loc) · 7.85 KB
/
graphUpdate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const functions = require("firebase-functions");
const admin = require('firebase-admin');
const db = admin.firestore();
let moment = require('moment')
const priceAndNoti = require('./PriceAndNoti')
let ptr = require('puppeteer')
exports.graphUpdate = functions.region("asia-southeast1").runWith({memory: '8GB', timeoutSeconds: 300})
.pubsub.schedule('0 20 * * *').timeZone('Etc/GMT+8').onRun(async (context) => {
const usersSnapShot = await db.collection("users").get()
const massRefresh = async (email) => {
const browser = await ptr.launch({
args: ['--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--no-first-run',
'--no-zygote',
'--disable-gpu'],
headless: true,
timeout: 0
})
const id = email
const userSnapShot = await db.collection('users').doc(id).get()
const token = await userSnapShot.data().token
const itemSnapShot = await db.collection("users/" + id + "/items").get()
const promises = itemSnapShot.docs.map(async doc => {
const dataObject = await doc.data()
const url = await dataObject.URL
const item_id = doc.id
const tPrice = dataObject.TargetPrice
const priceArr = dataObject.price
const dateArr = dataObject.dateArr
const itemKey = dataObject.itemKey
const detailTable = dataObject.detailTable
const isNameSetByUser = dataObject.edited
const name = dataObject.name
let data = {}
if (url.includes("shopee")) {
let isLowest = false
const page = await browser.newPage()
await page.setRequestInterception(true);
await page.on('request', request => {
if (request.resourceType() === 'image')
request.abort();
else
request.continue();
});
try {
//network idle to make sure that the website finish loading
await page.goto(url, {waitUntil: "networkidle2"})
} catch (e) {
data['name'] = 'Broken URL is given, did you copied correctly?'
priceArr.push('Broken URL is given, did you copied correctly?')
data['price'] = priceArr
}
const priceSelector = await page.$('._3e_UQT')
const nameSelector = await page.$('.attM6y')
await page.$('.OitLRu')
//if the website don't have css for name and price, meaning website fully loaded but it is item not found or 404
if (nameSelector !== null && priceSelector !== null) {
const retrievePrice = await page.evaluate(() => {
const priceHTML = document.querySelector('._3e_UQT')
const price = priceHTML.innerHTML
return price
})
const getName = await page.evaluate(() => {
const nameHTML = document.querySelector('.attM6y')
const name = nameHTML.textContent
return name
})
const getRating = await page.evaluate(() => {
const ratingHTML = document.querySelector('.OitLRu')
let rating
if (ratingHTML != null) {
rating = ratingHTML.textContent
} else {
rating = 0
}
return rating
})
const getNoOfRatings = await page.evaluate(() => {
const noOfRatingsHTML = document.querySelectorAll('.OitLRu')[1]
let noOfRatings = 0
if (noOfRatingsHTML != null || noOfRatingsHTML !== undefined) {
noOfRatings = noOfRatingsHTML.textContent
} else {
noOfRatings = 0
}
return noOfRatings
})
const floatPrice = parseFloat(retrievePrice.replace("$", ""))
const lowestPrice = parseFloat(detailTable['lowestPrice'].replace("$", ""))
const highestPrice = parseFloat(detailTable['highestPrice'].replace("$", ""))
let currentDate = new Date()
data['name'] = getName
data['lastUpdate'] = moment(currentDate).fromNow()
data['itemKey'] = itemKey
if (lowestPrice > retrievePrice) {
isLowest = true
detailTable['lowestPrice'] = retrievePrice
detailTable['lowRefTime'] = currentDate
detailTable['lowLastUpdate'] = moment(currentDate).fromNow()
}
if (highestPrice < retrievePrice) {
detailTable['highestPrice'] = retrievePrice
detailTable['highRefTime'] = currentDate
detailTable['highLastUpdate'] = moment(currentDate).fromNow()
}
if (priceArr.length === 7) {
priceArr.shift()
dateArr.shift()
}
priceArr.push(retrievePrice)
dateArr.push(currentDate)
detailTable['rating'] = parseFloat(getRating)
detailTable['noOfRatings'] = getNoOfRatings
data['price'] = priceArr
data['dateArr'] = dateArr
data['detailTable'] = detailTable
priceAndNoti.ExpoPushNotification(tPrice, floatPrice, token, false, getName, isLowest)
} else {
data['name'] = 'Broken URL is given, did you copied correctly?'
priceArr[priceArr.length - 1] = 'Broken URL is given, did you copied correctly?'
data['price'] = priceArr
}
await page.close()
} else {
data = await priceAndNoti.amazonEbayPriceAndName(url, tPrice, token, false, priceArr, dateArr, itemKey, detailTable, true)
}
if (isNameSetByUser) {
data['name'] = name
}
await db.collection('users').doc(id).collection('items').doc(item_id).update(data)
return null
}
)
const completedPromises = await Promise.all(promises)
await browser.close()
}
for (let user of usersSnapShot.docs) {
const userID = user.id
await massRefresh(userID)
}
})