import csv
import requests
from bs4 import BeautifulSoup
import time
import asyncio
import aiogram
from aiogram import Bot
import gspread
import datetime
# Replace with your own Telegram bot token
bot_token = '6240833652:AAFUVI7UmZfYHK_UR3_tWYeS8hjsqYvEQ-U'
# Replace with your own chat ID
chat_id = 1234702524
sa = gspread.service_account(filename="/Users/jitendersingh/Documents/sheetsapi.json")
sh = sa.open('only74kotc')
sheet = sh.worksheet('pricedrop')
async def send_daily_message(bot: Bot):
# Send the message
message = "I am running---------------------------------------------------"
await bot.send_message(chat_id=chat_id, text=message)
async def dailymsg():
# Create an instance of the Bot
bot = Bot(token=bot_token)
# Call the function to send the daily message
await send_daily_message(bot)
while True:
current_time = datetime.datetime.now().time()
target_time_start = datetime.time(9, 0)
target_time_end = datetime.time(9, 40)
# Checking if the current time is between 9:00 am and 9:40 am
if target_time_start <= current_time <= target_time_end:
asyncio.run(dailymsg())
# Wait until the time window ends (9:40 am) to avoid sending multiple messages
while current_time <= target_time_end:
current_time = datetime.datetime.now().time()
time.sleep(1) # Sleep for 1 second
print("Starting")
for i in range(1, sheet.row_count + 1):
url = sheet.cell(i, 1).value
price2 = sheet.cell(i, 2).value
# Scrape the price from the URL
myheaders = {'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'}
if 'amazon' in url:
page = requests.get(url, headers=myheaders)
soup = BeautifulSoup(page.content, 'html.parser')
maindiv = soup.find('div', id="apex_desktop")
try:
try:
pricediv1 = maindiv.find('div', id="corePriceDisplay_desktop_feature_div")
myprice = pricediv1.find('span', {'class': 'a-price-whole'}).text.replace("₹","")
try:
mrp = pricediv1.find('span', {'class': 'basisPrice'}).find('span', class_="a-offscreen").text.replace("₹","").replace(",","")
except:
mrp = "------"
except Exception as e:
try:
myprice = maindiv.find('td', text='Price:').find_next_sibling('td').find('span', {'aria-hidden': 'true'}).text.replace("₹","")
try:
mrp = maindiv.find('td', text='M.R.P.:').find_next_sibling('td').find('span', {'aria-hidden': 'true'}).text.replace("₹","").replace(",","")
except:
mrp = "------"
except Exception as e:
myprice = maindiv.find('td', text='Combo Price:').find_next_sibling('td').find('span', {'aria-hidden': 'true'}).text.replace("₹","")
try:
mrp = maindiv.find('td', text='Bundle List Price: ').find_next_sibling('td').find('span', {'aria-hidden': 'true'}).text.replace("₹","").replace(",","")
except:
mrp = "------"
try:
cpn = maindiv.find('span', class_="promoPriceBlockMessage").find_all('label')[1].text.replace('Apply ','').replace(' coupon Terms','').replace(' ','')
price = myprice
coupon = cpn
# remove the comma from the price
price = int(price.replace(",", ""))
if "₹" in coupon:
price -= int(coupon.replace("₹", ""))
elif "%" in coupon:
percentage = float(coupon.strip("%")) / 100
price -= price * percentage
else:
print("Invalid coupon")
except:
cpn = "No Coupon"
try:
thecpn = "\n🔥 Coupon: "+cpn+"\nFinal Price 🟰 ✅ "+str(price)+" ✅"
except:
thecpn = "\n----------------"
print("Price:",myprice)
print(cpn,"Coupon, After Discount", price)
except:
availability = soup.find('div', id="availability").span.text
print(availability)
myprice2 = myprice.replace(',', '')
price1 = float(myprice2)
if 'flipkart' in url:
link = url
source = requests.get (link, allow_redirects=False, headers=myheaders)
soup = BeautifulSoup(source.text,'html.parser')
myprice = soup.find('div', class_="_2c7YLP").find('div', class_="_30jeq3").text.replace('₹','').replace(',','').strip()
print(myprice)
myprice2 = myprice.replace(',', '')
price1 = float(myprice2)
print('URL:', url)
print('Price 1:', price1)
print('Price 2:', price2)
if price1 < float(price2):
message = f"{url} {price1} is lower than {price2}"
async def main():
async with aiogram.Bot(token=bot_token) as bot:
await bot.send_message(chat_id=chat_id, text=message)
asyncio.run(main())
print('------------------------------------')
time.sleep(2)
# Set the file pointer back to the beginning of the file
sheet = sh.worksheet('pricedrop')
print("Waiting 10 sec")
time.sleep(10)
0 comments:
Post a Comment