Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
72 lines (60 loc) · 1.73 KB
/
Copy pathapp.py
File metadata and controls
72 lines (60 loc) · 1.73 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
from flask import Flask, render_template,request,json
from flaskext.mysql import MySQL
import string,random
app = Flask(__name__)
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'Bucket'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
conn = mysql.connect()
cursor = conn.cursor()
@app.route("/")
def main():
return render_template('index.html')
@app.route("/<url>")
def sendData(url):
print(url)
return render_template('navigate.html')
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
@app.route("/url/<hashUrl>")
def sendUrl(hashUrl):
query = "select tags from book where value=%s"
args = (hashUrl)
cursor.execute(query,args)
# fetch all of the rows from the query
data = cursor.fetchall()
# print the rows
#print data
myData = ""
for row in data:
#log data
#print row[0]
myData = str(row[0])
if len(myData) > 0:
conn.commit()
return myData
else:
return "not cool"
@app.route('/createUrl',methods=['POST'])
def createUrl():
data = request.get_data()
val = id_generator()
query = "INSERT INTO book(value,tags) VALUES(%s,%s);"
args = (val,data)
print (val , data)
cursor.execute(query,args)
happn = cursor.fetchall()
if len(happn) is 0:
conn.commit()
return "successfully saved data"
else:
return json.dumps({'error':str(data[0])})
@app.errorhandler(404)
def page_not_found(e):
# your processing here
return render_template('index.html')
if __name__ == "__main__":
app.run(debug=True)