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
|
|
|
|
2017-03-13 11:15:54 +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
|
2017-03-13 11:15:54 +01:00
|
|
|
import smtplib
|
2017-03-13 13:49:43 +01:00
|
|
|
import os
|
2017-03-13 20:03:14 +01:00
|
|
|
import sys
|
2017-03-13 11:15:54 +01:00
|
|
|
import configparser
|
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
|
|
|
|
2017-03-13 21:43:58 +01:00
|
|
|
def __init__(self):
|
2017-03-11 14:00:39 +01:00
|
|
|
self.email = input("Please enter your email address:")
|
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
|
|
|
|
2017-03-13 21:43:58 +01:00
|
|
|
def __init__(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
|
|
|
|
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
|
|
|
|
2017-03-13 21:43:58 +01:00
|
|
|
def __init__(self):
|
2017-03-12 16:15:49 +01:00
|
|
|
self.desired_price = input("Please enter the price "
|
2017-03-13 21:43:58 +01:00
|
|
|
"you're looking for:")
|
2017-03-12 16:15:49 +01:00
|
|
|
|
2017-03-13 20:13:14 +01:00
|
|
|
def compare(self, current_price):
|
2017-03-13 21:43:58 +01:00
|
|
|
if not self.desired_price >= current_price:
|
2017-03-12 16:15:49 +01:00
|
|
|
return False
|
|
|
|
|
2017-03-13 21:43:58 +01:00
|
|
|
|
2017-03-13 11:15:54 +01:00
|
|
|
class Configuration():
|
2017-03-13 21:43:58 +01:00
|
|
|
|
|
|
|
def __init__(self):
|
2017-03-13 13:06:21 +01:00
|
|
|
self.check_location()
|
2017-03-13 20:03:14 +01:00
|
|
|
self.apply_settings()
|
2017-03-13 13:06:21 +01:00
|
|
|
|
|
|
|
def check_location(self):
|
|
|
|
# setup the config parser
|
2017-03-13 13:43:14 +01:00
|
|
|
self.config = configparser.ConfigParser()
|
2017-03-13 21:43:58 +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):
|
2017-03-13 20:03:14 +01:00
|
|
|
return self.config.read(config_path)
|
2017-03-13 13:06:21 +01:00
|
|
|
elif os.path.isfile(config_file):
|
2017-03-13 20:03:14 +01:00
|
|
|
return 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
|
2017-03-13 13:43:14 +01:00
|
|
|
self.password = self.config['DEFAULT']['password']
|
2017-03-13 13:30:31 +01:00
|
|
|
# assign the smtp_server variable
|
2017-03-13 13:43:14 +01:00
|
|
|
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
|
2017-03-13 13:43:14 +01:00
|
|
|
self.sender_address = self.config['DEFAULT']['sender_address']
|
2017-03-13 13:06:21 +01:00
|
|
|
|
|
|
|
|
2017-03-13 20:03:14 +01:00
|
|
|
settings = Configuration()
|
2017-03-13 13:06:21 +01:00
|
|
|
email = Email()
|
|
|
|
website = Website()
|
|
|
|
price = Price()
|
2017-03-13 20:13:14 +01:00
|
|
|
current_price = ""
|
|
|
|
|
|
|
|
while not price.compare(current_price):
|
|
|
|
website.get_page()
|
|
|
|
current_price = website.extract_price('//div[@class="product-price"]'
|
2017-03-13 21:43:58 +01:00
|
|
|
'/text()')
|
|
|
|
print('[%s]' % ', '.join(map(str, current_price)))
|
2017-03-13 20:13:14 +01:00
|
|
|
result = price.compare(current_price)
|
|
|
|
time.sleep(60)
|
|
|
|
else:
|
2017-03-13 21:43:58 +01:00
|
|
|
print(website.url)
|