kabum.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from bs4 import BeautifulSoup
  2. import requests
  3. import time
  4. from bot_buy import Messenger
  5. ### lists.txt format
  6. ### url,expected price,product name
  7. url = ""
  8. m = Messenger()
  9. while (1):
  10. with open('list.txt', 'r') as f:
  11. lines = f.readlines()
  12. for l in lines:
  13. url = l.split(',')[0].rstrip()
  14. price = float(l.split(',')[1].rstrip())
  15. prod = l.split(',')[2].rstrip()
  16. try:
  17. r = requests.get(url)
  18. except:
  19. print('failed to request')
  20. continue
  21. soup = BeautifulSoup(r.text, 'html.parser')
  22. #print(soup.prettify())
  23. price_normal = soup.find('span', class_="preco_desconto")
  24. price_bf = soup.find('span', class_="preco_desconto_avista-cm")
  25. if price_normal:
  26. price_normal = price_normal.find('strong')
  27. spans = [price_normal, price_bf]
  28. #print(type(price_bf))
  29. for s in spans:
  30. if s:
  31. try:
  32. new_price = float(s.get_text().split(' ')[1].replace(".","").replace(',', '.'))
  33. print(f"Produto: {prod} -> Esperado: {price} -> Agora: {new_price}")
  34. if new_price <= price:
  35. m.sendMessage(f"Got it! {prod} -> {new_price}\n{url}")
  36. except:
  37. print(f"failed to parse {new_price}")
  38. time.sleep(10)
  39. time.sleep(120)