-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
144 lines (117 loc) · 4.35 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import re
import sys
import os
import stripe
sys.path.append(os.path.join(os.path.abspath(__file__), '..'))
from scraping.metro_parse import get_metro_products
from scraping.tnt_parse import get_tnt_products
from flask import Flask, request, jsonify
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
URL = "https://www.walmart.ca/en/grocery/dairy-eggs/N-3798"
BASE_URL = "https://www.walmart.ca/"
URL_ENDPOINT = "https://www.walmart.ca/search?q="
CHROME_DRIVER_PATH = os.path.abspath('./chromedriver_mac') if sys.platform == 'darwin' \
else os.path.abspath('./chromedriver_windows.exe')
options = webdriver.ChromeOptions()
options.headless = True
options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
options.add_argument('--headless')
driver = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)
# search_qeury = "gala%20apples"
# url = BASE_URL + "?q=" + search_qeury + "&c=10019"
# driver.get(url)
stripe.api_key = os.environ['STRIPE_API_KEY']
@app.route('/checkout', methods=['POST'])
def create_checkout_session():
request_data = request.json
purchase_data = [{
'price_data': {
'currency': 'cad',
'product_data': {
'name': 'Your Groceries',
},
'unit_amount': request_data['price'],
},
'quantity': 1,
}]
session = stripe.checkout.Session.create(
payment_method_types=['card'],
line_items=purchase_data,
mode='payment',
success_url='http://localhost:3000/success',
cancel_url='http://localhost:3000/failure',
)
response = jsonify(id=session.id)
response.headers.add('Access-Control-Allow-Origin', '*')
return response
@app.route('/')
def hello_world():
response = jsonify({'data': 'Hello, world!'})
response.headers.add('Access-Control-Allow-Origin', '*')
return response
#testing endpoint
@app.route('/website', methods=['GET', 'POST'])
def upload_file():
query = request.args.get('q')
print(query)
grocery_items = query_prices_walmart_search(query)
return grocery_items
@app.route('/products')
def return_product_list():
item_name = request.args.get('item_name')
response = jsonify({
'metro': get_metro_products(item_name),
'tnt': get_tnt_products(item_name)
})
response.headers.add('Access-Control-Allow-Origin', '*')
return response
# Obtains price data from Walmart eggs and dairy section at the moment
def query_prices_walmart():
driver.get(URL)
f = open("website-content.html", "w") #creates local html file for testing purposes
f.write(driver.page_source)
soup = BeautifulSoup(driver.page_source, 'html.parser')
results = soup.find_all('div', class_='product-details-container')
grocery_item_dict = {}
for result in results:
match = re.search(r'<h2 class="thumb-header">([^<]*)', str(result))
item_name = match.group(1)
match = re.search(r'data-bind="possibleRangedCurrencyText: { data: price, simpleFormatting: simpleFormatting } "><span>\$</span>([\d\.]*)', str(result))
if match is not None:
item_price = match.group(1)
grocery_item_dict[item_name] = item_price
return grocery_item_dict
# Obtains price data from term that is serached up
def query_prices_walmart_search(search_qeury):
url = URL_ENDPOINT + search_qeury + "&c=10019"
driver.get(url)
# driver.get(url)
f = open("website-content.html", "w") #creates local html file for testing purposes
f.write(driver.page_source)
soup = BeautifulSoup(driver.page_source, 'html.parser')
results = soup.find_all('div', class_='css-vh2dix e1m8uw910')
grocery_item_dict = {}
for result in results:
name = result.find('p', {'data-automation': 'name'}).text
href = result.find('a', class_="css-n8po8v e1m8uw911").attrs['href']
price = result.find('span', {'data-automation': 'current-price'})
print(price)
if "¢" in price.text:
price = price.text
price = "0." + price
else:
price = price.text
grocery_item_dict[name] = {
'price': price,
'href': BASE_URL + href
}
return grocery_item_dict
#alternative approaches:
# takes in list of inputs