Created
December 24, 2015 19:24
-
-
Save bee-san/0fd2ba97129535feb02a to your computer and use it in GitHub Desktop.
Emails you if a product on Amazon goes on sale or gets cheaper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bs4 as bs # beautiful soup | |
import requests | |
import json # javascript object notation | |
import os | |
import sys | |
#TODO Use a SQL database | |
# auto-key |product name | price | link | |
import logging | |
# imports the loggign module, creates a logging file called "ProgramLog.txt" | |
logging.basicConfig(filename='_ProgramLog.txt', level=logging.DEBUG, | |
format=' %(asctime)s - %(levelname)s- %(message)s') | |
def main(): | |
email_msg = "" | |
logging.debug("started at main") | |
check = input("Do you want to add to the list?") | |
# asks user if they want to add to the list or not | |
if check.upper().startswith("Y"): | |
logging.debug(("user wants to add to dict")) | |
amazon = addToDict() | |
logging.debug(type(amazon)) | |
else: | |
logging.debug("user does not") | |
if os.path.isfile("database.json"): | |
file = open("database.json", "r") | |
logging.debug(file) | |
amazon = json.load(file) | |
logging.debug(type(amazon)) | |
logging.debug(amazon) | |
email_msg = checkPrice(amazon) | |
send_email(email_msg) | |
else: | |
logging.debug("os path does not exist") | |
amazon = addToDict() | |
close() | |
def headlessmode(): | |
logging.debug("program is running in headless mode.") | |
if os.path.isfile("database.json"): | |
amazon = json.loads(json.dumps("database.json")) | |
def send_email(email_msg): | |
print(email_msg) | |
def checkPrice(amazon): | |
logging.debug(type(amazon)) | |
email_msg = "" | |
for key, value in amazon.items(): | |
logging.debug(key) | |
logging.debug(value) | |
logging.debug(type(amazon)) | |
link = key | |
price = value | |
price = removePoundSign(price) | |
logging.debug(link) | |
logging.debug(price) | |
current_price = getPrice(link) | |
current_price = removePoundSign(current_price) | |
if current_price < price: | |
logging.debug("Price types.") | |
logging.debug(type(current_price)) | |
logging.debug(type(price)) | |
percent = ((current_price) * (price) / 100) # may be wrong | |
print("{} is cheaper by {}%".format(link, percent)) | |
email_msg = email_msg + ("{} is cheaper by {}%".format(link, percent)) | |
amazon[link] = current_price #TODO this is broken af | |
else: | |
email_msg = email_msg + ("{}'s price has not changed.".format(link)) | |
return email_msg | |
def removePoundSign(price): | |
# strips all basic puncuation | |
# defines puncuations to strip from the message | |
punctuations = '''£$!()-[]{};:'"\,<>./?@#$%^&*_~''' | |
# creates empty variable to store the final result in | |
no_punct = "" | |
# for every charecter in MESSAGE | |
for char in price: | |
# if charecter is not in puncuations | |
if char not in punctuations: | |
no_punct = no_punct + char | |
# replaces puncuation with nothing | |
no_punct = int(no_punct) | |
return no_punct | |
# returns non-puncuated string | |
def addToDict(): | |
if os.path.isfile("amazon.json"): | |
logging.debug("amazon.json exists") | |
amazon = json.loads(amazon) | |
how_many = input(int("How many amazon links are there?: ")) | |
counter = 0 | |
while counter != how_many: | |
amazon_link = input("please enter your amazon link here") | |
amazon_price = getPrice(amazon_link) | |
amazon.update({(amazon_link) : (amazon_price)}) | |
counter = counter + 1 # cani shroten this lmao | |
else: | |
logging.debug("amazon.son does not exist") | |
amazon = {} | |
how_many = input("how many links? ") | |
how_many = int(how_many) | |
logging.debug("asked user for shit") | |
counter = 0 | |
while counter != how_many: | |
amazon_link = input("please enter your amazon link here ") | |
logging.debug("getting price") | |
amazon_price = getPrice(amazon_link) | |
logging.debug("updating dict") | |
amazon.update({(amazon_link) : (amazon_price)}) | |
counter = counter + 1 | |
logging.debug(type(amazon)) | |
logging.debug(amazon) | |
with open('database.json', 'w') as f: | |
f.write(json.dumps(amazon)) | |
return amazon | |
def getPrice(amazon_link): | |
res = requests.get((amazon_link)) | |
# downloads web page | |
res.raise_for_status() | |
# raises an error if it fails | |
soup = bs.BeautifulSoup(res.text, 'html.parser') | |
# parses the webpage | |
logging.debug("downloaded webpage") | |
elems = (soup.select(""".a-text-normal""")) | |
logging.debug("parsing for price") | |
# looks for price using CSS | |
price = (elems[0].text.strip()) | |
logging.debug(price) | |
# only returns the price | |
return price | |
def close(): | |
sys.exit() | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment