diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6b4622c --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +*.pyc +.vscode/ +products.json +__pycache__ +geckodriver.log +.env + +## env +/conda-env \ No newline at end of file diff --git a/app.py b/app.py index 4a906f8..822247f 100644 --- a/app.py +++ b/app.py @@ -1,9 +1,16 @@ from price_scraper import scrape_amazon_search, best_deal from flask import Flask, request, render_template, jsonify -import requests +import boto3 +from botocore.exceptions import NoCredentialsError +from dotenv import load_dotenv +from datetime import datetime +import requests import json import os +# Set .env vars +load_dotenv() + app = Flask(__name__) app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True @@ -18,8 +25,34 @@ def scrape(): search_term = str(request.form['test']) + now = datetime.now() + dt_str = now.strftime("%H:%M:%S %B %d, %Y ") + + ACCESS_KEY = os.getenv("AWSAccessKeyId") + SECRET_KEY = os.getenv("AWSSecretKey") + BUCKET_NAME = "amazon-search-results" + FILE_NAME = dt_str + "searched_products.json" + products = scrape_amazon_search(search_term) + with open('products.json', 'w') as json_file: + json.dump(products, json_file, sort_keys=True, indent=4) + + s3 = boto3.resource( + 's3', + aws_access_key_id = ACCESS_KEY, + aws_secret_access_key = SECRET_KEY, + ) + + + try: + s3.Bucket(BUCKET_NAME).upload_file("products.json", FILE_NAME) + print("Upload Successful") + except FileNotFoundError: + print("The file was not found") + except NoCredentialsError: + print("Credentials not available") + best_deal_item = best_deal(products) #return jsonify(products) diff --git a/price_scraper.py b/price_scraper.py index 39e17fa..89f20ca 100644 --- a/price_scraper.py +++ b/price_scraper.py @@ -94,7 +94,7 @@ def scrape_amazon_search(search_term): # print("exception") should_add = False - product = {"Name": name, "Price": price, "Previous price": prev_price, + product = {"Search term": search_term, "Name": name, "Price": price, "Previous price": prev_price, "Discount": discount, "URL": link, "Prime product": prime} if should_add: products.append(product) diff --git a/requirements.txt b/requirements.txt index 71da5ba..f7ae15c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +boto3==1.9.66 certifi==2020.11.8 chardet==3.0.4 click==7.1.2 @@ -7,8 +8,8 @@ idna==2.10 itsdangerous==1.1.0 Jinja2==2.11.2 MarkupSafe==1.1.1 +python-dotenv==0.15.0 requests==2.25.0 selenium==3.141.0 urllib3==1.26.2 -Werkzeug==1.0.1 -wincertstore==0.2 +Werkzeug==1.0.1 \ No newline at end of file