-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
187 lines (166 loc) · 5.9 KB
/
database.py
File metadata and controls
187 lines (166 loc) · 5.9 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
import sqlite3
from dataclasses import dataclass
from typing import Optional
DATABASE_NAME = 'merged.db'
@dataclass
class Place:
id: int
category: str
name: str
address: str
rating: float
reviews_count: int
latitude: float
longitude: float
image: Optional[str]
description: Optional[str]
distance_km: float
def get_nearest_eat_location(user_lat, user_lon):
conn = sqlite3.connect(DATABASE_NAME)
cursor = conn.cursor()
try:
q = '''SELECT id,
category,
name,
address,
rating,
reviews,
lon AS longitude,
lat AS latitude,
picture,
6371 * ACOS(
SIN(RADIANS(?)) * SIN(RADIANS(lat)) +
COS(RADIANS(?)) * COS(RADIANS(lat)) *
COS(RADIANS(lon) - RADIANS(?))
) AS distance_km,
description
FROM places
WHERE distance_km < 2
ORDER BY distance_km ASC LIMIT 1;
'''
cursor.execute(q, (user_lat, user_lat, user_lon))
row = cursor.fetchone()
if row:
return Place(
id=row[0],
category=row[1],
name=row[2],
address=row[3],
rating=row[4],
reviews_count=row[5],
longitude=row[6],
latitude=row[7],
image=row[8],
distance_km=row[9],
description=row[10],
)
return None
except sqlite3.Error as e:
print(f"Ошибка при выполнении запроса: {e}")
return None
finally:
conn.close()
def get_another_nearest_eat_location(user_lat, user_lon, exclude_ids):
conn = sqlite3.connect(DATABASE_NAME)
cursor = conn.cursor()
try:
exclude_ids = exclude_ids if exclude_ids else (-1,)
q = '''SELECT id,
category,
name,
address,
rating,
reviews,
lon AS longitude,
lat AS latitude,
picture,
6371 * ACOS(
SIN(RADIANS(?)) * SIN(RADIANS(lat)) +
COS(RADIANS(?)) * COS(RADIANS(lat)) *
COS(RADIANS(lon) - RADIANS(?))
) AS distance_km,
description
FROM places
WHERE id NOT IN ({})
AND distance_km < 2
ORDER BY distance_km ASC LIMIT 1;
'''.format(','.join(['?'] * len(exclude_ids)))
params = (user_lat, user_lat, user_lon) + tuple(exclude_ids)
cursor.execute(q, params)
row = cursor.fetchone()
if row:
return Place(
id=row[0],
category=row[1],
name=row[2],
address=row[3],
rating=row[4],
reviews_count=row[5],
longitude=row[6],
latitude=row[7],
image=row[8],
distance_km=row[9],
description=row[10],
)
return None
except sqlite3.Error as e:
print(f"Ошибка при выполнении запроса: {e}")
return None
finally:
conn.close()
def get_top_rated_eat_location(user_lat: float, user_lon: float, excluded_ids: list = None):
conn = sqlite3.connect(DATABASE_NAME)
cursor = conn.cursor()
try:
excluded_ids = excluded_ids or []
# Базовый запрос без фильтрации по исключенным ID
base_query = '''SELECT id,
category,
name,
address,
rating,
reviews,
lon AS longitude,
lat AS latitude,
picture,
6371 * ACOS(
SIN(RADIANS(?)) * SIN(RADIANS(lat)) +
COS(RADIANS(?)) * COS(RADIANS(lat)) *
COS(RADIANS(lon) - RADIANS(?))
) AS distance_km,
description
FROM places
WHERE distance_km < 2 {}
ORDER BY rating DESC LIMIT 1; \
'''
# Добавляем условие для исключения ID, если они есть
if excluded_ids:
id_placeholders = ','.join(['?'] * len(excluded_ids))
where_clause = f'AND id NOT IN ({id_placeholders})'
query = base_query.format(where_clause)
params = (user_lat, user_lat, user_lon) + tuple(excluded_ids)
else:
query = base_query.format('')
params = (user_lat, user_lat, user_lon)
cursor.execute(query, params)
row = cursor.fetchone()
if row:
return Place(
id=row[0],
category=row[1],
name=row[2],
address=row[3],
rating=row[4],
reviews_count=row[5],
longitude=row[6],
latitude=row[7],
image=row[8],
distance_km=row[9],
description=row[10],
)
return None
except sqlite3.Error as e:
print(f"Ошибка при выполнении запроса: {e}")
return None
finally:
conn.close()