forked from xboxertony/taipei-day-trip-website
/
attractions_api.py
104 lines (96 loc) · 4.02 KB
/
attractions_api.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
from flask import Blueprint, jsonify, request
from mysql.connector import pooling
import mysql.connector
import json
import os
from dotenv import load_dotenv
attractions = Blueprint('attractions',
__name__)
load_dotenv()
connection_pool = pooling.MySQLConnectionPool(pool_name=os.getenv('DB_NAME'),
pool_size=5,
pool_reset_session=True,
host=os.getenv('DB_HOST'),
user=os.getenv('DB_USER'),
password=os.getenv(
'DB_PASSWORD'),
database=os.getenv(
'DB_DATABASE'),
auth_plugin="mysql_native_password",)
@ attractions.route('/api/attractions')
def getdata():
mydb = connection_pool.get_connection()
mycursor = mydb.cursor(dictionary=True)
page = int(request.args.get("page", 0))
keyword = request.args.get("keyword", "")
start = page*12
mycursor.execute(
"SELECT * FROM attraction where name LIKE %s OR category LIKE %s LIMIT %s,%s", (('%'+keyword+'%'), ('%'+keyword+'%'), start, 12))
myresult = mycursor.fetchall()
mydb.close()
try:
if myresult:
if len(myresult)/12 == 1:
nextPage = page+1
else:
nextPage = None
data = []
for information in myresult:
data.append({
"id": information['id'],
"name": information['name'],
"category": information['category'],
"description": information['description'],
"address": information['address'],
"transport": information['transport'],
"mrt": information['mrt'],
"latitude": information['latitude'],
"longitude": information['longitude'],
"images": [f"https://{url}" for url in information['images'].split("https://")[1:]]
})
return jsonify({
"nextPage": nextPage,
"data": data}), 200
else:
return jsonify({
"error": 'true',
"message": '請重新輸入'}), 200
except 500:
return jsonify({
"error": 'true',
"message": '伺服器內部錯誤'}), 500
@ attractions.route('/api/attraction/<int:attractionId>')
def getbyId(attractionId):
mydb = connection_pool.get_connection()
mycursor = mydb.cursor(dictionary=True)
mycursor.execute("SELECT COUNT(id) FROM attraction")
result = mycursor.fetchone()
mydb.close()
total_id = int(result['COUNT(id)'])
try:
if attractionId > total_id:
return jsonify({
"error": True,
"message": "景點編號不正確"}), 400
else:
mycursor.execute(
"SELECT * FROM attraction where id = %s", (attractionId,))
res = mycursor.fetchall()
for information in res:
data = {
"id": information['id'],
"name": information['name'],
"category": information['category'],
"description": information['description'],
"address": information['address'],
"transport": information['transport'],
"mrt": information['mrt'],
"latitude": information['latitude'],
"longitude": information['longitude'],
"images": [f"https://{url}" for url in information['images'].split("https://")[1:]]
}
return jsonify({'data': data})
except 500:
return jsonify({
'error': true,
'message': '伺服器內部錯誤'}), 500