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

112 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
#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
import configparser
2017-03-11 12:28:03 +01:00
2017-03-13 13:06:21 +01:00
class Email(object):
2017-03-11 14:00:39 +01:00
def __init__ (self):
self.email = ""
def prompt_for_recipient(self):
2017-03-11 14:00:39 +01:00
self.email = input("Please enter your email address:")
2017-03-12 16:15:49 +01:00
return self.email
2017-03-11 12:28:03 +01:00
2017-03-11 13:15:06 +01:00
class Website(object):
2017-03-11 14:00:39 +01:00
def __init__ (self):
self.url = ""
2017-03-12 16:15:49 +01:00
self.tree = ""
2017-03-11 14:00:39 +01:00
def prompt_for_url(self):
2017-03-12 16:15:49 +01:00
self.url = input("Please enter the url you want to monitor:")
2017-03-11 14:00:39 +01:00
2017-03-12 16:15:49 +01:00
def get_page(self):
page = requests.get(self.url)
self.tree = html.fromstring(page.content)
2017-03-11 13:15:06 +01:00
2017-03-13 13:06:21 +01:00
def extract_price(self, string):
2017-03-12 16:15:49 +01:00
prices = self.tree.xpath(string)
return prices
2017-03-11 13:15:06 +01:00
class Price(object):
2017-03-11 14:00:39 +01:00
def __init__ (self):
self.desired_price = ""
2017-03-12 16:15:49 +01:00
def prompt_for_amount(self):
self.desired_price = input("Please enter the price "
"you're looking for:")
def compare_prices(self, current_price):
if not self.desired_price in current_price:
return False
class Configuration():
2017-03-13 13:06:21 +01:00
def __init__ (self):
self.config = ""
2017-03-13 13:30:31 +01:00
self.password = ""
self.smtp_server = ""
self.sender_address = ""
2017-03-13 13:06:21 +01:00
self.check_location()
2017-03-13 13:56:57 +01:00
self.smtp_port()
2017-03-13 13:06:21 +01:00
def check_location(self):
# setup the config parser
self.config = configparser.ConfigParser()
2017-03-13 13:30:31 +01:00
# check whether the config file exists either in the home folder or
# next to the binary
2017-03-13 13:06:21 +01:00
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):
self.config.read(config_path)
2017-03-13 13:06:21 +01:00
elif os.path.isfile(config_file):
self.config.read(config_file)
2017-03-13 13:06:21 +01:00
else:
print("Configuration file not found.")
sys.exit(1)
2017-03-13 13:30:31 +01:00
def apply_settings(self):
# assign the password variable
self.password = self.config['DEFAULT']['password']
2017-03-13 13:30:31 +01:00
# assign the smtp_server variable
self.smtp_server = self.config['DEFAULT']['smtp_server']
2017-03-13 13:56:57 +01:00
# assign the smtp_port variable
self.smtp_port = self.config['DEFAULT']['smtp_port']
2017-03-13 13:30:31 +01:00
# assign the email_address variable
self.sender_address = self.config['DEFAULT']['sender_address']
2017-03-13 13:06:21 +01:00
2017-03-13 13:49:43 +01:00
config = Configuration()
2017-03-13 13:06:21 +01:00
email = Email()
website = Website()
price = Price()
config.apply_settings()
email.prompt_for_recipient()
2017-03-13 13:06:21 +01:00
website.prompt_for_url()
price.prompt_for_amount()
website.get_page()
current_price = website.extract_price('//div[@class="product-price"]'
2017-03-12 16:15:49 +01:00
'/text()')
while not budget.compare_prices(current_price):
2017-03-13 13:06:21 +01:00
website.get_page()
current_price = website.extract_price('//div[@class="product-price"]'
2017-03-12 16:15:49 +01:00
'/text()')
print ('[%s]' % ', '.join(map(str, current_price)))
2017-03-13 13:06:21 +01:00
result = price.compare_prices(current_price)
2017-03-12 16:15:49 +01:00
time.sleep(60)
else:
print (tui.url)