-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
397 lines (325 loc) · 14.9 KB
/
bot.py
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import os
import re
import telebot
import asyncio
import traceback
from time import sleep
from threading import Timer
from bs4 import BeautifulSoup
from flask import Flask, request
from aiohttp import ClientSession
from psycopg2 import connect, InterfaceError
from aiohttp.client_exceptions import InvalidURL
import requests
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36' }
TOKEN = os.getenv("TOKEN")
ADMIN = os.getenv('ADMIN')
dbURL = os.getenv('DATABASE_URL')
ADMIN_NAME = "Koushik Naskar"
server = Flask(__name__)
bot= telebot.TeleBot(TOKEN, parse_mode='HTML')
class DataBase:
def __init__(self):
self.dbFile = dbURL
self.con = connect(self.dbFile)
with self.con:
with self.con.cursor() as cur:
# create database tables and insert admin details
cur.execute( "CREATE TABLE IF NOT EXISTS ITEMS("
"itemID SERIAL PRIMARY KEY,"
"userId INTEGER NOT NULL,"
"link TEXT NOT NULL UNIQUE,"
"name TEXT NOT NULL,"
"addedPrice INTEGER NOT NULL,"
"price INTEGER NOT NULL);"
)
def connectToDb(self):
# flyio postgresql disconnects after 30 minutes of inactivity
if self.con.closed !=0:
sendToAdmin(f'Current value of connection {self.con.closed}')
self.con = connect(self.dbFile)
sleep(1)
else:
try:
with self.con:
with self.con.cursor() as cur:
cur.execute("SELECT count(*) from ITEMS;")
except InterfaceError:
# con.closed is 0 but still unable to connect to database
# sendToAdmin("con.closed fails to check properly")
self.con = connect(self.dbFile)
def addItem(self,user,_link):
try:
self.connectToDb()
# if 'amazon' in _link or 'amzn' in _link:
if 'flipkart' not in _link and "myntra" not in _link:
bot.send_message(user,"Currently, this bot does not support link from this website.")
return
message = bot.send_message(user, "Searching for the product🧐. Please wait.")
links = re.findall("(?P<url>https?://[^\s]+)",_link)
if len(links) ==0:
bot.send_message(user,"No links found.")
return
elif len(links)>1:
bot.send_message(user,"Only one link is supported at a time.")
return
link = links[0]
with self.con:
with self.con.cursor() as cur:
cur.execute("SELECT count(*) from ITEMS where link=%s",(link,))
v = cur.fetchone()
if v[0]==0:
newValues = queryPrice([link])
if newValues:
n,p = newValues[0]
cur.execute(
'INSERT into ITEMS (userId, link, name,addedPrice, price) values (%s,%s,%s,%s,%s)',
(user,link,n,p,p))
bot.send_message(user,
f'The following product(s) are added for tracking.\n\n<i>{n}</i> \nCurrent Price: <b>{p}</b>')
else:
bot.send_message(user,'Unable to find the product.')
else:
bot.send_message(user,'Link is already in database')
except InvalidURL:
bot.send_message(user,'Unable to find the product. Check if the link is ok.')
except:
print(traceback.format_exc())
sendToAdmin("exception occurred in adding new item")
bot.send_message(user,'Unable to find the product.')
finally:
bot.delete_message(message.chat.id, message.message_id)
def update(self,user):
# list the items from the database and also query the site for latest price
try:
self.connectToDb()
message = bot.send_message(user, "Checking prices🧐. Please wait.")
with self.con:
with self.con.cursor() as cur:
cur.execute('SELECT link from ITEMS where userId=%s',(user,))
links = [i for (i,) in cur.fetchall()]
if len(links)==0:
bot.send_message(user,'No items found on list')
return
newValues = queryPrice(links)
if newValues:
for l,(_,p) in zip(links,newValues):
cur.execute("UPDATE ITEMS SET price=%s where link=%s",(p,l))
cur.execute('SELECT name, price, addedPrice,link from ITEMS where userId=%s ORDER BY itemID',(user,))
txt = f"Here is your current product list.\n<b>{'-'*50}</b>\n\n" + self.buildList(cur.fetchall())
bot.send_message(user,txt,disable_web_page_preview=True)
else:
bot.send_message(user,'Unable to get update for some product')
except:
print(traceback.format_exc())
sendToAdmin("exception occurred during update")
finally:
bot.delete_message(message.chat.id, message.message_id)
def scheduleUpdate(self):
toUpdate = []
try:
self.connectToDb()
with self.con:
with self.con.cursor() as cur:
cur.execute("SELECT userid from ITEMS group by userid")
userids = cur.fetchall()
for u in userids:
cur.execute("SELECT link,price from ITEMS where userid=%s",u)
infos = cur.fetchall()
links, oPrice = zip(*infos)
newValues = queryPrice(links)
update = [[p,l] for l,(_,p) in zip(links,newValues) if p ]
# print(update)
noUpdate = [l for l,(_,p) in zip(links,newValues) if not p ]
if len(noUpdate):
sendToAdmin("Cound not update these links\n" +"\n".join(noUpdate))
cur.executemany("UPDATE ITEMS SET price=%s where link=%s",update)
# check if price of any product is dropped
for oP,(_,nP) in zip(oPrice,newValues):
if int(nP)<oP:
# note whom to send the notification
toUpdate.append(u)
break
with self.con:
with self.con.cursor() as cur:
for u in toUpdate:
print(f"Price dropped for items for user {u[0]}")
cur.execute('SELECT name, price, addedPrice, link from ITEMS where userId=%s ORDER BY itemID',u)
txt = f"<b>Price dropped for some items in your list.</b>\n<b>{'-'*50}</b>\n\n" + self.buildList(cur.fetchall())
bot.send_message(u[0],txt,disable_web_page_preview=True)
# sendToAdmin('Scheduled update done.')
except:
print(traceback.format_exc())
sendToAdmin("exception occurred in schedule update")
def buildList(self,info):
# info is a list fo title, price and added price
txt = ""
for i,(title,price,addedPrice,link) in enumerate(info):
txt += f"{i+1}. <a href='{link}'><i>{title}</i></a>\nPrice: <b>{price}</b>"
if price-addedPrice>0:
txt += f" [▲{addedPrice}]"
elif price-addedPrice<0:
txt += f" [▼{addedPrice}]"
txt += "\n\n"
return txt
def untrack(self,user,_prompt):
try:
self.connectToDb()
prompt = _prompt.strip('/untrack').strip()
if len(prompt)==0: # send the list
with self.con:
with self.con.cursor() as cur:
cur.execute('SELECT itemID,name from ITEMS where userId=%s ORDER BY itemID',(user,))
values = cur.fetchall()
if len(values) == 0:
bot.send_message(user,'No items found on list')
return
txt = "Choose the link beside the product to untrack.\n"
txt += f"<b>{'-'*50}</b>\n\n"
for i,(iID,name) in enumerate(values):
txt += f"{i+1}. {name}\n/untrack{iID} \n"
bot.send_message(user,txt)
else: # untrack item
with self.con:
with self.con.cursor() as cur:
cur.execute("DELETE from items where itemID=%s RETURNING name",(prompt,))
name = cur.fetchone()[0]
bot.send_message(user, f"Your product <i>{name}</i> is removed from list.")
except:
print(traceback.format_exc())
sendToAdmin("exception occurred during untrack")
def listAll(self):
try:
self.connectToDb()
with self.con:
with self.con.cursor() as cur:
cur.execute("Select userId, count(*) from items group by userId;")
txt = '\n'.join([f"{i} - {j}" for i,j in cur.fetchall()])
bot.send_message(ADMIN,txt)
except:
print(traceback.format_exc())
sendToAdmin("exception occurred in status")
db = DataBase()
# NOTE:----------------------------------------------------------
# amazon does not allow the web scrapping from the cloud
# will solve this later
async def check_price(session:ClientSession, url:str):
async with session.get(url) as resp:
# print("checking price for ",url)
page = await resp.read()
soup = BeautifulSoup(page, 'html.parser')
if 'flipkart' in url: # if flipkart
title = soup.find("span", {"class": "B_NuCI"})
if title :
title = title.get_text().strip()
else:
return None,None
company = soup.find("span", {"class": "G6XhRU"})
company = company.get_text().strip() if company else ""
price = soup.find("div", {"class": "_30jeq3 _16Jk6d"})
if price:
price = price.get_text()[1:].replace(',','')
else:
return None, None
print('checking price',title,price)
fullTitle = f"{company} {title}"
return fullTitle,price #prints the price
elif "myntra" in url:
title = soup.find("h1", {"class": "pdp-name"})
if title :
title = title.get_text().strip()
else:
return None,None
company = soup.find("h1", {"class": "pdp-title"})
company = company.get_text().strip() if company else ""
price = soup.find("div", {"class": "pdp-price"})
if price:
price = price.get_text()[1:].replace(',','')
else:
return None, None
print('checking price',title,price)
fullTitle = f"{company} {title}"
return fullTitle,price #prints the price
# else:
# return None,None
# elif 'amazon' in url or 'amzn' in url:# for amazon
# title = soup.find("span", {"id": "productTitle"}).get_text()
# price = soup.find("span", {"class": "a-offscreen"}).get_text()[1:].replace(',','')
async def auxqueryPrice(URLs):
async with ClientSession() as session:
tasks = [asyncio.ensure_future(check_price(session, u)) for u in URLs]
return await asyncio.gather(*tasks)
def queryPrice(URLs):
return asyncio.run(auxqueryPrice(URLs))
def check_price_flipkart(url:str):
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
# print(soup)
try:
if 'flipkart' in url: # if flipkart
print('checking for flipkart')
title = soup.find("span", {"class": "B_NuCI"}).get_text()
price = soup.find("div", {"class": "_30jeq3 _16Jk6d"}).get_text()[1:].replace(',','')
elif 'amazon' in url or 'amzn' in url:# for amazon
print('checking for amazon')
title = soup.find("span", {"id": "productTitle"}).get_text()
price = soup.find("span", {"class": "a-offscreen"}).get_text()[1:].replace(',','')
return title,price
else:
print('Unknown website')
except:
print(traceback.format_exc())
sendToAdmin("exception occurred")
def helpMessage(user):
bot.send_message(user,(
'Send a product link and this bot will track the price for you.\n'
'Send /check to check prices for all the products in the list.\n'
'Send /untrack to untrack products.\n'
'Currently only supports link for flipkart.'
))
@bot.message_handler(func=lambda _: True)
def newLink(message):
link:str = message.text
print("Text received",link)
user = message.from_user.id
if link == '/start':
bot.send_message(user,"Welcome to the TelePriceTracker bot.")
helpMessage(user)
elif link == '/help':
helpMessage(user)
elif link == '/check':
db.update(user)
elif link.startswith('/untrack') :
# pass as /untrack 5,6 to untrack them
db.untrack(user,link)
elif link.lower()=='status' and int(user) == int(ADMIN):
db.listAll()
else:
print('link received')
db.addItem(user,link)
@server.route('/' + TOKEN, methods=['POST'])
def getMessage():
json_string = request.get_data().decode('utf-8')
update = telebot.types.Update.de_json(json_string)
bot.process_new_updates([update])
return "!", 200
@server.route("/")
def webhook():
bot.remove_webhook()
bot.set_webhook(url='https://cold-dew-7030.fly.dev/' + TOKEN)
return '''<div style="text-align: center;">
<h1>Tele Price Tracker</h1>
<h3>Send a product link and this bot will track the price for you.</h3>
<h2>Open <br><a href="https://t.me/telepricetrackerbot"> https://t.me/telepricetrackerbot</a> <br> to access the bot.</h2>
</div>''', 200
class RepeatTimer(Timer):
def run(self):
while not self.finished.wait(self.interval):
self.function(*self.args, **self.kwargs)
def sendToAdmin(msg:str):
bot.send_message(ADMIN,msg)
if __name__ == "__main__":
sendToAdmin("Bot started")
t = RepeatTimer(3600, db.scheduleUpdate).start()
from waitress import serve
serve(server, host="0.0.0.0", port=int(os.environ.get('PORT', 5000)))