-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathComparisionMechanism.js
240 lines (228 loc) · 11.9 KB
/
ComparisionMechanism.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
function compare(check, chid, limitingValue) {
var msg = 'Products being tracked : \n\n';
var tx = 0
var ssNew = SpreadsheetApp.openById(sheetID)
var sheet = ssNew.getSheets()[0]
var sheet2 = ssNew.getSheets()[1]
var range = sheet.getDataRange()
var rules = range.getValues()
Logger.log('Number of products : ' + (rules.length - 1))
var fromValue = 1
var toValue = rules.length
if (limitingValue) {
var limitingRange = sheet2.getDataRange()
var limitingRules = limitingRange.getValues()
fromValue = limitingRules[1][0]
toValue = minimum(limitingRules[1][1], toValue)
sheet2.getRange(2, 1).setValue(toValue)
sheet2.getRange(2, 2).setValue(minimum(toValue + batchSize, rules.length))
}
Logger.log(`Will be tracking product ${fromValue} to product ${toValue - 1}`)
for (i = fromValue; i < toValue; i++) {
var uri = rules[i][ssVars[3]]
var chatid = rules[i][ssVars[0]]
var prevPrice = rules[i][ssVars[1]]
var trackId = rules[i][ssVars[2]]
var name = rules[i][ssVars[6]]
var oStock = String(rules[i][ssVars[5]])
if (uri.lastIndexOf('amazon.in') != -1) {
var client = 'Amazon'
var api = fetchAmazonAPI
} else if (uri.lastIndexOf('flipkart.com') != -1) {
var client = 'Flipkart'
var api = fetchAPI
} else {
var client = 'Flipkart'
var api = fetchAPI
}
if (check == 'yes' && chid == chatid) {
var json = api(uri)
} else if (check == 'yes' && chid != chatid) {
var json = null
} else {
var json = api(uri)
}
if (json != null) {
var cp = json.current_price
if (cp != NaN && cp != undefined) {
var stock = json.in_stock
var newURL = json.share_url
if (newURL != undefined) {
if (name == undefined || name == '' || name == 'undefined') {
sheet.getRange(i + 1, ssVars[3] + 1).setValue(String(newURL))
sheet.getRange(i + 1, ssVars[6] + 1).setValue(String(json.name))
Logger.log(`URL Updated to ${newURL}`)
}
}
var keyboardButtons = [
{ "text": "➡️ Show Product", "callback_data": `get_message` },
{ "text": "⚠️ Stop Tracking", "callback_data": `/stop ${trackId}` },
{ "text": `ℹ️ Open in ${client}`, "url": uri }
]
// var toStop = '\n\n⚖️ To view the product details : /show_' + trackId + '\n✋ To stop getting alerts for this product : /stop_' + trackId + '\n'
if (json.in_stock != undefined) {
var dpercent = Math.abs(parseInt(10000 * (cp - prevPrice) / prevPrice) / 100)
Logger.log(i + '. ' + json.name + ' (Rs. ' + inr(cp) + ')')
if (oStock != String(stock)) {
if (stock) {
sendButtonMessage('😀 Stock avaliable for Product <a href="' + uri + '">' + json.name + '</a>\nCurrent Price : Rs. ' + inr(cp) + `\nMarketplace : <code>${client}</code>\n\nGenerated by ` + botName, null, chatid, keyboardButtons)
} else {
sendButtonMessage('😔 Product Out Of Stock <a href="' + uri + '">' + json.name + '</a>' + `\nMarketplace : <code>${client}</code>\n\nGenerated by ` + botName, null, chatid, keyboardButtons)
}
sheet.getRange(i + 1, ssVars[5] + 1).setValue(String(stock))
}
if (cp < prevPrice) {
sendButtonMessage('<a href="' + uri + '">' + json.name +
'</a>\n\n📉 Prices have been reduced by Rs. ' + inr(-cp + prevPrice) +
' ( ' + dpercent + '% )\n\nCurrent Price : Rs. ' + inr(cp) + '\n\nTracking ID: <code>' + trackId +
'</code>' + `\nMarketplace : <code>${client}</code>\n\nGenerated by ` + botName, null, chatid, keyboardButtons
)
sheet.getRange(i + 1, ssVars[1] + 1).setValue(cp)
} else if (cp > prevPrice) {
sendButtonMessage('<a href="' + uri + '">' + json.name +
'</a>\n\n📈 Prices have been increased by Rs. ' + inr(cp - prevPrice) +
' ( ' + dpercent + '% ) \n\nCurrent Price : Rs. ' + inr(cp) + '\n\nTracking ID : <code>' + trackId +
'</code>' + `\nMarketplace : <code>${client}</code>\n\nGenerated by ` + botName, null, chatid, keyboardButtons
)
sheet.getRange(i + 1, ssVars[1] + 1).setValue(cp)
}
if (check == 'yes') {
if (chatid == chid) {
tx++
msg += tx + '. <b><a href="' + uri + '">' + json.name + '</a></b>\n<b>Tracking ID :</b> <code>' + trackId + '</code>' + '\n<b>Current Price :</b> <code>Rs. ' + inr(cp) + '</code>\n<b>In Stock :</b> ' + String(stock) + '\n\n'
}
}
var t = new Date();
sheet.getRange(i + 1, ssVars[4] + 1).setValue(t.toString())
}
}
}
}
if (limitingValue) {
if (toValue == rules.length) {
sheet2.getRange(2, 1).setValue(1)
sheet2.getRange(2, 2).setValue(batchSize + 1)
}
}
if (check == 'yes') {
var after = Math.ceil((rules.length - 1) / batchSize) * trackAfter
msg += `\nCurrently we are tracking your products every ${after} minutes!\nGenerated by ${botName}`
if (tx != 0) {
sendMessage(msg, null, chid)
} else {
sendMessage('You don\'t have any products to track!', null, chid)
}
}
SpreadsheetApp.flush();
}
function pincodeCompare(check, chid, limitingValue) {
var msg = 'Products being tracked : \n\n';
var tx = 0
var ssNew = SpreadsheetApp.openById(sheetID)
var sheet = ssNew.getSheets()[2]
var sheet2 = ssNew.getSheets()[1]
var range = sheet.getDataRange()
var rules = range.getValues()
Logger.log('Number of products : ' + (rules.length - 1))
var fromValue = 1
var toValue = rules.length
if (limitingValue) {
var limitingRange = sheet2.getDataRange()
var limitingRules = limitingRange.getValues()
fromValue = limitingRules[2][0]
toValue = minimum(limitingRules[2][1], toValue)
if (toValue > fromValue + 4) {
toValue = fromValue + 4
}
sheet2.getRange(3, 1).setValue(toValue)
sheet2.getRange(3, 2).setValue(minimum(toValue + pincodeBatchSize, rules.length))
}
Logger.log(`Will be tracking product ${fromValue} to product ${toValue - 1} (Pincode Mode)`)
for (i = fromValue; i < toValue; i++) {
var uri = rules[i][ssVars[3]]
var chatid = rules[i][ssVars[0]]
var prevPrice = rules[i][ssVars[1]]
var trackId = rules[i][ssVars[2]]
var name = rules[i][ssVars[6]]
var oStock = String(rules[i][ssVars[5]])
var pincode = String(rules[i][ssVars[7]])
if (check == 'yes' && chid == chatid) {
var json = fetchAPI(uri)
} else if (check == 'yes' && chid != chatid) {
var json = null
} else {
var json = pincodeAPI(uri, pincode)
}
if (json != null && json.stock_details != undefined) {
var cp = json.current_price
var stock = json.stock_details.pincode_stock
var newURL = json.share_url
if (newURL != undefined) {
if (name == undefined || name == '' || name == 'undefined') {
sheet.getRange(i + 1, ssVars[3] + 1).setValue(String(newURL))
sheet.getRange(i + 1, ssVars[6] + 1).setValue(String(json.name))
Logger.log(`URL Updated to ${newURL}`)
}
}
var keyboardButtons = [
{ "text": "➡️ Show Product", "callback_data": `pincode_message ${pincode}` },
{ "text": "⚠️ Stop Tracking", "callback_data": `/stop ${trackId}` },
{ "text": 'ℹ️ Open in Flipkart', "url": uri }
]
// var toStop = '\n\n⚖️ To view the product details : /show_' + trackId + '\n✋ To stop getting alerts for this product : /stop_' + trackId + '\n'
if (json.stock_details != undefined) {
var dpercent = Math.abs(parseInt(10000 * (cp - prevPrice) / prevPrice) / 100)
Logger.log(i + '. ' + json.name + ' (Rs. ' + inr(cp) + ')')
if (oStock != String(stock)) {
if (stock) {
sendButtonMessage('😀 Stock avaliable for Product <a href="' + uri + '">' + json.name + `</a> (Pincode : <code>${pincode}</code>)\nCurrent Price : <code>Rs. ` + inr(cp) + '</code>\n\nGenerated by ' + botName, null, chatid, keyboardButtons)
} else {
sendButtonMessage('😔 Product Out Of Stock <a href="' + uri + '">' + json.name + `</a> (Pincode : <code>${pincode}</code>)\n\nGenerated by ${botName}`, null, chatid, keyboardButtons)
}
sheet.getRange(i + 1, ssVars[5] + 1).setValue(String(stock))
}
if (cp < prevPrice) {
sendButtonMessage('<a href="' + uri + '">' + json.name +
`</a> (Pincode : <code>${pincode}</code>)\n\n📉 Prices have been reduced by Rs. ` + inr(-cp + prevPrice) +
' ( ' + dpercent + '% ) \n\n<a href="' +
uri + '">Tap to go to product</a>\n\nCurrent Price : Rs. ' + inr(cp) + '\n\nTracking ID: <code>' + trackId +
'</code>' + '\nGenerated by ' + botName, null, chatid, keyboardButtons
)
sheet.getRange(i + 1, ssVars[1] + 1).setValue(cp)
} else if (cp > prevPrice) {
sendButtonMessage('<a href="' + uri + '">' + json.name +
`</a> (Pincode : <code>${pincode}</code>)\n\n📈 Prices have been increased by Rs. ` + inr(cp - prevPrice) +
' ( ' + dpercent + '% ) \n\n<a href="' +
uri + '">Tap to go to product</a>\n\nCurrent Price : Rs. ' + inr(cp) + '\n\nTracking ID : <code>' + trackId +
'</code>' + '\nGenerated by ' + botName, null, chatid, keyboardButtons
)
sheet.getRange(i + 1, ssVars[1] + 1).setValue(cp)
}
if (check == 'yes') {
if (chatid == chid) {
tx++
msg += tx + '. <b><a href="' + uri + '">' + json.name + '</a></b>\n<b>Tracking ID :</b> <code>' + trackId + '</code>' + '\n<b>Current Price :</b> <code>Rs. ' + inr(cp) + '</code>\n<b>In Stock :</b> ' + String(stock) + '\n\n'
}
}
var t = new Date();
sheet.getRange(i + 1, ssVars[4] + 1).setValue(t.toString())
}
}
}
if (limitingValue) {
if (toValue == rules.length) {
sheet2.getRange(3, 1).setValue(1)
sheet2.getRange(3, 2).setValue(batchSize + 1)
}
}
if (check == 'yes') {
var after = Math.ceil((rules.length - 1) / pincodeBatchSize) * pincodeTrackAfter
msg += `\nCurrently we are tracking your products every ${after} minutes in your pincode!\nGenerated by ${botName}`
if (tx != 0) {
sendMessage(msg, null, chid)
} else {
sendMessage('You don\'t have any products to track!', null, chid)
}
}
SpreadsheetApp.flush();
}