extend the config file
to make the script run more independently I've removed most of the input functions
This commit is contained in:
parent
c09f5d6e05
commit
c2cee1d420
|
@ -2,4 +2,7 @@
|
|||
smtp_server: smtp.switchplus-mail.ch
|
||||
smtp_port: 465
|
||||
sender_address: no-reply@2li.ch
|
||||
recipient_address: andreas@2li.ch
|
||||
password: foo
|
||||
url: https://www.tui.ch/pauschalreisen/suchen/angebote/Caribbean-World-Soma-Bay/79878/c87364cfe300e5c5a80590d92b427a63?useExtendedFilterDefaultValues=true&showTotalPrice=true&childs=5;2&startDate=2017-05-24&endDate=2017-05-31&duration=7&sortOffersField=countryDepartureAirports&roomTypes=FR&maxStopOver=0
|
||||
price: 3000
|
158
price_checker.py
158
price_checker.py
|
@ -16,107 +16,105 @@ import lxml
|
|||
|
||||
class Email(object):
|
||||
|
||||
def __init__(self):
|
||||
self.recipient = input("Please enter your email address:")
|
||||
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 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 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()
|
||||
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()
|
||||
|
||||
|
||||
class Website(object):
|
||||
|
||||
def __init__(self):
|
||||
self.url = input("Please enter the url you want to monitor:")
|
||||
def __init__(self, url):
|
||||
self.url = url
|
||||
|
||||
def get_page(self):
|
||||
#session = dryscrape.Session()
|
||||
#session.visit(self.url)
|
||||
#page = session.body()
|
||||
page = requests.get(self.url)
|
||||
self.soup = BeautifulSoup(page.text, "lxml")
|
||||
def get_page(self):
|
||||
session = dryscrape.Session()
|
||||
session.visit(self.url)
|
||||
page = session.body()
|
||||
self.soup = BeautifulSoup(page, "lxml")
|
||||
|
||||
def extract_price(self):
|
||||
prices = self.soup.find_all("div", class_="product-price")
|
||||
return prices
|
||||
def extract_price(self):
|
||||
prices = self.soup.find_all("div", class_="product-price")
|
||||
return prices
|
||||
|
||||
|
||||
class Price(object):
|
||||
|
||||
def __init__(self):
|
||||
self.desired_price = input("Please enter the price "
|
||||
"you're looking for:")
|
||||
def __init__(self, price):
|
||||
self.desired_price = price
|
||||
|
||||
def compare(self, current_price):
|
||||
if not self.desired_price >= current_price:
|
||||
return False
|
||||
def compare(self, current_price):
|
||||
if not self.desired_price >= current_price:
|
||||
return False
|
||||
|
||||
|
||||
class Configuration():
|
||||
|
||||
def __init__(self):
|
||||
self.check_location()
|
||||
self.apply_settings()
|
||||
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 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']
|
||||
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']
|
||||
|
||||
|
||||
settings = Configuration()
|
||||
#email = Email()
|
||||
website = Website()
|
||||
#price = Price()
|
||||
#current_price = ""
|
||||
#message = "Test"
|
||||
email = Email(settings.recipient_address)
|
||||
website = Website(settings.url)
|
||||
current_price = ""
|
||||
price = Price(settings.price)
|
||||
|
||||
website.get_page()
|
||||
print(website.extract_price())
|
||||
|
||||
#while not price.compare(current_price):
|
||||
# website.get_page()
|
||||
# current_price = website.extract_price('//div[@class="product-price"]'
|
||||
# '/text()')
|
||||
# print('[%s]' % ', '.join(map(str, current_price)))
|
||||
# result = price.compare(current_price)
|
||||
# time.sleep(60)
|
||||
#else:
|
||||
# email.connecting(settings.smtp_server, settings.smtp_port)
|
||||
# email.login(settings.sender_address, settings.password)
|
||||
# email.sending(settings.sender_address, message)
|
||||
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)
|
||||
|
|
Reference in New Issue