-
Notifications
You must be signed in to change notification settings - Fork 1
/
scraper.py
58 lines (46 loc) · 2.28 KB
/
scraper.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
import requests
from bs4 import BeautifulSoup
import logging
import datetime
from pytz import timezone
from telegram.ext import Updater, CommandHandler
from secret import TOKEN
def send_updates(bot, job):
for product in watchedProducts:
title, price = check_price(product)
bot.send_message(chat_id=product[0], text=f"Price of {title} is now {price} €!")
def start(bot, update):
text = update.message.text
print(text)
emoji = u'\U0001F609'
bot.send_message(chat_id=update.message.chat_id, text=f"Hi, I am the amazon price checker! Type '/register https://smile.amazon.de/gp/product/PRODUCT_LINK' to let me watch a product for you, I'll send you price updates daily {emoji!s}")
def registerProduct(bot, update):
text = update.message.text
print(text)
if (".amazon.de/" not in text):
bot.send_message(chat_id=update.message.chat_id, text="Your message does not contain an amazon.de link!")
else:
parts = text.split()
if (len(parts) != 2):
bot.send_message(chat_id=update.message.chat_id, text="The format of your request was wrong. Please send it like '/register https://smile.amazon.de/gp/product/B07GDR2LYK")
else:
watchedProducts.append([update.message.chat_id,parts[1]])
title, price = check_price(parts[1])
bot.send_message(chat_id=update.message.chat_id, text=f"Checking for {title} with current price {price} € daily")
def check_price(url):
page = requests.get(url, headers=HEADERS)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find(id="productTitle").get_text().strip()
price = soup.find(id="priceblock_ourprice").get_text().strip()
price = price[0:price.find(',')]
return title, price
watchedProducts = []
HEADERS = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}
updater = Updater(token=TOKEN)
dispatcher = updater.dispatcher
jobQueue = updater.job_queue
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
dispatcher.add_handler(CommandHandler('register', registerProduct))
updater.start_polling()
jobQueue.run_daily(send_updates,datetime.time(hour=15, minute=51))