-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·51 lines (42 loc) · 1.73 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
from flask import Flask, render_template, url_for, request, redirect, flash
from flask_sqlalchemy import SQLAlchemy
from flask_apscheduler import APScheduler
import pandas as pd
import threading
from Utilities import token_settings
app = Flask(__name__)
scheduler = APScheduler()
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)
import Search
from Scraper import Scraper
scraper = Scraper()
# scraper_thread = threading.Thread(target=scraper.run)
# @app.before_first_request
# def thread_start():
# scraper_thread.start()
scheduler.add_job(id = 'Book Search', func = scraper.run)
scheduler.start()
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'POST':
if request.form['password'] == token_settings['client_secret']:
try:
files = request.files.getlist('files')
db.session.query(Search.Search).delete()
for f in files:
searches = pd.read_csv(f, dtype={'book_id': str, 'max_price': float})
searches = pd.DataFrame(searches, columns=['book_id', 'max_price'])
for _, row in searches.T.iteritems():
new_search = Search.Search(book_id=row['book_id'], max_price=row['max_price'])
db.session.add(new_search)
db.session.commit()
return '<h1 style="text-align:center">Upload successful</h1>'
except:
return '<h1 style="text-align:center">There was an issue adding your files</h1>'
else:
return '<h1 style="text-align:center">Incorrect password</h1>'
else:
return render_template('index.html')
if __name__ == "__main__":
app.run()