This repository has been archived on 2020-04-03. You can view files and clone it, but cannot push or open issues or pull requests.
price_checker/price_checker.py

121 lines
3.5 KiB
Python
Raw Normal View History

2017-03-11 12:28:03 +01:00
#!/usr/bin/env python3
2017-03-11 13:15:06 +01:00
# get user email -> get url -> get price
2017-03-13 21:43:58 +01:00
# download website -> search for price -> compare price
# if price = website.price -> send mail(user.mail, url)
2017-03-11 12:28:03 +01:00
from bs4 import BeautifulSoup
2017-03-11 13:15:06 +01:00
import requests
2017-03-12 16:15:49 +01:00
import time
import smtplib
2017-03-13 13:49:43 +01:00
import os
2017-03-13 20:03:14 +01:00
import sys
import configparser
2017-03-14 16:16:07 +01:00
import dryscrape
import lxml
2017-03-11 12:28:03 +01:00
2017-03-13 21:43:58 +01:00
2017-03-13 13:06:21 +01:00
class Email(object):
2017-03-11 14:00:39 +01:00
def __init__(self, recipient):
self.recipient = recipient
def connecting(self, smtp_server, smtp_port):
self.server = smtplib.SMTP_SSL(smtp_server, smtp_port)
self.server.ehlo()
def login(self, sender, password):
self.server.login(sender, password)
def sending(self, sender, message):
message = "Subject: " + message
try:
self.server.sendmail(sender, self.recipient, message)
self.server.quit()
print("Successfully sent email")
except SMTPException:
print("Error: unable to send email")
self.server.quit()
2017-03-11 12:28:03 +01:00
2017-03-13 21:43:58 +01:00
2017-03-11 13:15:06 +01:00
class Website(object):
2017-03-11 14:00:39 +01:00
def __init__(self, url):
self.url = url
2017-03-11 14:00:39 +01:00
def get_page(self):
session = dryscrape.Session()
session.visit(self.url)
page = session.body()
self.soup = BeautifulSoup(page, "lxml")
2017-03-11 13:15:06 +01:00
def extract_price(self):
prices = self.soup.find_all("div", class_="product-price")
return prices
2017-03-11 13:15:06 +01:00
2017-03-13 21:43:58 +01:00
2017-03-11 13:15:06 +01:00
class Price(object):
2017-03-11 14:00:39 +01:00
def __init__(self, price):
self.desired_price = price
2017-03-12 16:15:49 +01:00
def compare(self, current_price):
if not self.desired_price >= current_price:
return False
2017-03-12 16:15:49 +01:00
2017-03-13 21:43:58 +01:00
class Configuration():
2017-03-13 21:43:58 +01:00
def __init__(self):
self.check_location()
self.apply_settings()
def check_location(self):
# setup the config parser
self.config = configparser.ConfigParser()
# check whether the config file exists either in the home
# folder or next to the binary
home = os.getenv('HOME')
config_file = "price_checker.cfg"
config_folder = ".config/price_checker/"
config_path = os.path.join(home, config_folder, config_file)
if os.path.isfile(config_path):
return self.config.read(config_path)
elif os.path.isfile(config_file):
return self.config.read(config_file)
else:
print("Configuration file not found.")
sys.exit(1)
def apply_settings(self):
# assign the password variable
self.password = self.config['DEFAULT']['password']
# assign the smtp_server variable
self.smtp_server = self.config['DEFAULT']['smtp_server']
# assign the smtp_port variable
self.smtp_port = self.config['DEFAULT']['smtp_port']
# assign the email_address variable
self.sender_address = self.config['DEFAULT']['sender_address']
# assign the url variable
self.url = self.config['DEFAULT']['url']
# assign the price variable
self.price = self.config['DEFAULT']['price']
# assign the recipient variable
self.recipient_address = self.config['DEFAULT']['recipient_address']
2017-03-13 13:06:21 +01:00
2017-03-13 20:03:14 +01:00
settings = Configuration()
email = Email(settings.recipient_address)
website = Website(settings.url)
current_price = ""
price = Price(settings.price)
while not price.compare(current_price):
website.get_page()
current_price = website.extract_price()
time.sleep(1)
else:
message = "Subject: Found Price match!\n" + settings.url
email.connecting(settings.smtp_server, settings.smtp_port)
email.login(settings.sender_address, settings.password)
email.sending(settings.sender_address, message)