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

120 lines
3.7 KiB
Python
Raw Permalink 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-14 22:57:35 +01:00
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
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):
2017-03-14 22:57:35 +01:00
msg = MIMEMultipart()
msg['Subject'] = "Found a price match!"
msg['From'] = "Price Checker"
body = message
msg.attach(MIMEText(body, 'plain'))
text = msg.as_string()
try:
2017-03-14 22:57:35 +01:00
self.server.sendmail(sender, self.recipient, text)
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()
2017-03-14 22:57:35 +01:00
session.set_attribute('auto_load_images', False)
session.visit(self.url)
page = session.body()
self.soup = BeautifulSoup(page, "lxml")
2017-03-11 13:15:06 +01:00
def extract_price(self):
2017-03-14 22:57:35 +01:00
prices = [a.get_text() for
a in self.soup.find_all("span", class_="amount ng-binding")]
lowest_price = min(int(s) for s in prices)
return int(lowest_price)
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
2017-03-14 22:57:35 +01:00
self.price = int(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-16 09:20:18 +01:00
dryscrape.start_xvfb()
2017-03-13 20:03:14 +01:00
settings = Configuration()
email = Email(settings.recipient_address)
website = Website(settings.url)
2017-03-16 09:20:18 +01:00
website.get_page()
if website.extract_price() < settings.price:
email.connecting(settings.smtp_server, settings.smtp_port)
email.login(settings.sender_address, settings.password)
email.sending(settings.sender_address, settings.url)
sys.exit(0)
else:
sys.exit(0)