-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
133 lines (109 loc) · 3.51 KB
/
Copy pathapp.py
File metadata and controls
133 lines (109 loc) · 3.51 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
from flask import Flask, request, jsonify
import mysql.connector
import database
import csv
import json
import os
import hashlib
import threading
import access
# Store csv
jsonArray = []
jsonString=""
hex_dig=""
def csv_to_json(csvFilePath):
#read csv file
with open(csvFilePath, encoding='utf_8_sig') as csvf:
#load csv file data using csv library's dictionary reader
csvReader = csv.DictReader(csvf)
#convert each csv row into python dict
for row in csvReader:
#add this python dict to json array
jsonArray.append(row)
jsonString = json.dumps(jsonArray)
# create an MD5 hash object
hash_object = hashlib.md5()
# update the hash object with the string
hash_object.update(jsonString.encode('utf-8'))
# get the hexadecimal representation of the hash
hex_dig = hash_object.hexdigest()
return jsonString, hex_dig
csvFilePath = os.getcwd()+r'/info.csv'
jsonString,hex_dig=csv_to_json(csvFilePath)
database.setup()
# Set up a connection to the MySQL server
cnx = mysql.connector.connect(
host='localhost',
user='root',
password='AS324S1',
database='mydatabase'
)
#start the thread of getting token
# start a new thread to run the function every 2 hours
# thread = threading.Thread(target=access.gettoken)
# thread.daemon = True
# thread.start()
token = "fUjXn2r5u8x!A%D*"
# Create a Flask app instance
app = Flask(__name__)
@app.route('/update', methods=['GET','POST'])
def update_record():
# Check the integrity of the request
if not request.json or 'id' not in request.json or 'activity' not in request.json or request.json['token']!=token:
return "Invalid request", 400
# Get the name and age values from the request
id = request.json['id']
activity = request.json['activity']
# Create a cursor object to execute queries
cursor = cnx.cursor()
# Check if a record with the given name exists in the table
query = "SELECT * FROM mytable WHERE name = %s"
values = (id,)
cursor.execute(query, values)
result = cursor.fetchone()
if result:
#use activity to identify whether need update or not
if activity == "get":
data = {'activities': result[1]}
cursor.close()
return jsonify(data)
# The record exists, so update the age value
query = "UPDATE mytable SET activity = %s WHERE name = %s"
values = (activity,id)
cursor.execute(query, values)
cnx.commit()
cursor.close()
return f"Record for {id} updated successfully{activity}"
else:
if activity == "get":
data = {'activities': ''}
cursor.close()
return jsonify(data)
# The record doesn't exist, so insert a new record
query = "INSERT INTO mytable (name, activity) VALUES (%s, %s)"
values = (id, activity)
cursor.execute(query, values)
cnx.commit()
cursor.close()
return f"New record for {id} created successfully"
# Close the cursor
@app.route("/mapdata", methods=['POST'])
def returncsv():
if not request.json or request.json['token']!=token:
return "Invalid request", 400
if request.json['md5']!=hex_dig:
print(hex_dig)
return jsonString
else:
return "check"
# @app.route("/mini_token")
# def returntoken():
# return access.token
@app.route("/mini_id", methods=['POST'])
def returnopenid():
if not request.json or request.json['token']!=token:
return "Invalid request", 400
return access.getopenid(request.json['usercode'])
# Run the app on localhost:5001
if __name__ == '__main__':
app.run(debug=True,port=5001)