forked from theriley106/WaltonAnalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
292 lines (230 loc) · 8.62 KB
/
app.py
File metadata and controls
292 lines (230 loc) · 8.62 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from flask import Flask, render_template, request, url_for, redirect, Markup, jsonify, make_response, send_from_directory, session
import csv
import gpxpy.geo
import bs4
#import PullData
import Analytics
import random
import requests
import os
import time
import json
import subprocess
import main
from operator import itemgetter
import threading
listOfStores = main.GrabAllStoreNumbers()
proxies = {}
app = Flask(__name__)
Timeout = 60
@app.route('/dataviz', methods=['GET', 'POST'])
def returnStores(database="static/Walmarts.csv"):
a = []
with open(database) as csvfile:
rows = csv.reader(csvfile)
res = list(rows)
for lines in res:
try:
information = {}
information["Title"] = lines[0]
longLat = lines[2]
if len(longLat) < 2:
raise Exception("No LongLat Found")
Long = longLat.partition(',')[0].strip().replace("(", "")
Lat = longLat.partition(',')[2].strip().replace(")", "")
information["Long"] = Long
information["Lat"] = Lat
a.append(information)
except Exception as exp:
pass
return render_template("map.html", stores=a)
@app.route('/download/<file>')
def download_csv(file):
return send_from_directory(directory='static', filename=file)
def Status(zip):
with open('Data/Economics.csv', 'rb') as f:
reader = csv.reader(f)
lis = list(reader)
for e in lis:
if str(zip) in str(e):
return int(float(str(e[1]).replace(",", '')))
def MarkupIgnore(markup):
markup = str(markup.getText())
return markup
def LatLong(address):
address = address.replace(" ", "+")
response = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address={}'.format(address))
resp_json_payload = response.json()
return (resp_json_payload['results'][0]['geometry']['location'])
def diffLatLong(lat1, long1, lat2, long2):
return gpxpy.geo.haversine_distance(lat1, lon1, lat2, lon2)
def EditGoogleMaps(iframe, width, height):
try:
iframe = str(iframe).replace('width="250"', 'width="{}"'.format(width))
iframe = str(iframe).replace('height="250"', 'height="{}"'.format(height))
except:
print(iframe)
return iframe
def ReturnStoreInfo(store):
try:
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
Store = {}
url = 'https://www.walmart.com/store/{}'.format(store)
res = requests.get(url, headers=headers, proxies=proxies)
page = bs4.BeautifulSoup(res.text, "lxml")
Store['Number'] = store
Google = EditGoogleMaps(page.select('#store-side-bar > div.StoreSideBar > div.GoogleMapsIframe > iframe')[0], 400, 400)
Store['GoogleMaps'] = Markup(Google)
if len(page.select('.open-24-hours')) == 0:
Store['StoreHours'] = 'Not 24 Hours'
else:
Store['StoreHours'] = '24 Hours'
Store['Phone'] = MarkupIgnore(page.select('.phone')[0])
Store['Address2'] = MarkupIgnore(page.select('.address2')[0])
Store['Address1'] = MarkupIgnore(page.select('.address1')[0])
Store['Name'] = MarkupIgnore(page.select('.heading-d')[0])
Store['ItemCount'] = len(Analytics.ConvertStoreToDict(store))
Store['Econ'] = str("{:,.2f}".format(Status(Store['Address2'][-5:])))[:-3]
Store.update(Analytics.ReturnStoreInfo(store))
return Store
except Exception as exp:
print(exp)
def GrabFromSpreadsheet(spreadsheet):
ListOfStores = []
with open(spreadsheet, 'r') as f:
reader = csv.reader(f)
your_list = list(reader)
for line in your_list:
a = {}
if 'Walmart Supercenter' in str(line[1]):
a['number'] = line[0]
ListOfStores.append(a)
return ListOfStores
def CheckUser(username):
ListOfUsernames = ["Matt", "Christopher"]
if username != None and str(username) in ListOfUsernames:
return True
else:
return False
@app.route('/')
def Home(username=None):
return render_template('Home.html', database=GrabFromSpreadsheet("{}/static/Walmarts.csv".format(os.getcwd())))
###############################
@app.route('/Login', methods=['POST'])
def LogIn():
#Loading Page
print(session.get('my_var', None))
session['Username'] = request.form['username']
session['Password'] = request.form['password']
if Username == 'admin' and Password == 'password':
return "True"
#store = ReturnStoreInfo(store)
else:
return 'False'
#return redirect(url_for('ItemPage', storenum=store))
#return render_template('testing.html', store=store)
###############################################################################################################################
@app.route('/Item/')
def SearchItem(username=None):
return render_template('ItemSearch.html', database=GrabFromSpreadsheet("{}/static/Walmarts.csv".format(os.getcwd())))
@app.route('/Item/', methods=['POST'])
def SearchItemByNumber(username=None, SKU=None, store=None):
#Loading Page
ZIP = request.form['zip']
SKU = request.form['item']
#store = ReturnStoreInfo(store)
return redirect(url_for('ItemPage', storenum=store))
#return render_template('testing.html', store=store)
@app.route('/Item/<sku>/')
def ItemPage(sku):
Info = ReturnSKUInfo(sku)
StockList = MultiStoreSearch(sku)
#Stock == StockList
return render_template('ItemPage.html', info=Info)
#########################################################################################
###################SINGLE ITEM SEARCH
@app.route('/LowestPrice/')
def SearchLow(username=None):
Stock = [{'Store': '', 'Price': '', 'Quantity': '', 'Aisle': ''}]
BasicItem = {'name': '', 'Title': '', 'OnlinePrice': '', 'ListPrice': '', 'PercentQuantity': '', 'ReviewCount': '', 'AverageReview': ''}
return render_template('LowSearch.html', Stock=Stock, BasicItem=BasicItem)
@app.route('/LowestPrice/<sku>')
def GrabLowPrice(sku):
Stock = [{'Store': '', 'Price': '', 'Quantity': '', 'Aisle': ''}]
BasicItem = {'name': '', 'Title': '', 'OnlinePrice': '', 'ListPrice': '', 'PercentQuantity': '', 'ReviewCount': '', 'AverageReview': ''}
return render_template('LowestPriceItem.html', Stock=Stock, BasicItem=BasicItem)
@app.route('/LowestPrice/', methods=['POST'])
def SearchItemByLow(username=None, SKU=None, store=None):
#Loading Page
SKU = request.form['yourname']
os.system("python itemGrab.py 30 {} DEBUG TIMEOUT 1".format(SKU))
#a = 0
'''while os.path.isfile("{}.json".format(SKU)) == False:
time.sleep(1)
a = a + 1
if a > 20:
return render_template('error.html')'''
return redirect(url_for('searchIndex', SKU=SKU))
#return render_template('testing.html', store=store)
#@app.route('/', defaults={'SKU': "106607294"})
@app.route('/searchbysku/<SKU>', methods=["GET"])
def searchIndex(SKU):
return jsonify(json.loads(str(subprocess.check_output("cat {}.json".format(SKU), shell=True))))
@app.route('/suggestions/<SKU>', defaults={'lowestPrice': 999999999999})
@app.route('/suggestions/<SKU>/<lowestPrice>')
def suggestions(lowestPrice, SKU):
stores = listOfStores
while len(stores) > 0:
try:
try:
print(lowestPrice)
except:
lowestPrice = 99999999999
suggestions_list = random.choice(listOfStores)
stores.remove(suggestions_list)
data = main.SearchStore(suggestions_list, "106607294")
return render_template('suggestions.html', store=suggestions_list, information=data, lowestPrice=lowestPrice)
except Exception as exp:
print exp
pass
##################################################################3
@app.route('/Store/')
def SearchStore(username=None):
return render_template('StoreSearch.html', database=GrabFromSpreadsheet("{}/static/Walmarts.csv".format(os.getcwd())))
@app.route('/Store/', methods=['POST'])
def Search(username=None, SKU=None, store=None):
#Loading Page
store = request.form['yourname']
#store = ReturnStoreInfo(store)
return redirect(url_for('StorePage', storenum=store))
#return render_template('testing.html', store=store)
@app.route('/Store/<csvs>/html/')
def ToHTMLDoc(csvs):
csvs = 'static/' + str(csvs) + '.csv'
with open(csvs, 'rb') as f:
reader = csv.reader(f)
your_list = list(reader)
print(len(your_list))
return render_template('ToHTML.html', Stock=your_list)
@app.route('/Store/<storenum>', methods=['GET'])
def StorePage(storenum):
print('blah')
store = ReturnStoreInfo(str(int(storenum)))
if store != None:
return render_template('testing.html', store=store)
else:
return render_template('error.html')
#################################################################################################3
@app.route('/_add_numbers')
def add_numbers():
a = request.args.get('a', 0, type=str)
store = request.args.get('b', 0, type=str)
# a is the SKIN or search Query
a = Analytics.LocalPrice(store, str(a))
a = '{} - {} In Stock'.format('${:,.2f}'.format(a[0]), a[1])
if a != None:
return jsonify(result=str(a))
else:
return jsonify(result='Item Not Available')
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8000, debug=True)