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

107 lines
3.3 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
import configparser
2017-03-11 12:28:03 +01:00
2017-03-11 14:00:39 +01:00
class User(object):
def __init__ (self):
self.email = ""
def prompt_for_email(self):
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-12 16:15:49 +01:00
def extract_current_price(self, string):
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():
def check_location():
# setup the config parser
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):
config.read(config_path)
elif os.path.isfile(config_file):
config.read(config_file)
else:
print("Configuration file not found.")
sys.exit(1)
# assign the repository variable depending whether it's a remote or a local
# repository
if 'server' in config['DEFAULT']:
repository = (config['DEFAULT']['user']
+ "@"
+ config['DEFAULT']['server']
+ ":"
+ config['DEFAULT']['repository_path'])
int_vars.server = config['DEFAULT']['server']
else:
repository = config['DEFAULT']['repository_path']
# assign the password variable
password = config['DEFAULT']['password']
# set the environment variables
os.environ['BORG_REPO'] = repository
os.environ['BORG_PASSPHRASE'] = password
2017-03-12 16:15:49 +01:00
christian = User()
tui = Website()
budget = Price()
christian.prompt_for_email()
tui.prompt_for_url()
budget.prompt_for_amount()
tui.get_page()
current_price = tui.extract_current_price('//div[@class="product-price"]'
'/text()')
while not budget.compare_prices(current_price):
tui.get_page()
current_price = tui.extract_current_price('//div[@class="product-price"]'
'/text()')
print ('[%s]' % ', '.join(map(str, current_price)))
result = budget.compare_prices(current_price)
time.sleep(60)
else:
print (tui.url)