forked from jatin-dot-py/zomato-intelligence
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_res_meta.py
More file actions
55 lines (52 loc) · 2.38 KB
/
get_res_meta.py
File metadata and controls
55 lines (52 loc) · 2.38 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
import requests
import json
def fetch_restaurant_coordinates(res_id: int | str, proxies=None) -> dict:
url = f"https://api.zomato.com/gw/menu/res_info/{res_id}"
payload = {
"should_fetch_res_info_from_agg": True
}
headers = {
"Accept": "image/webp",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Content-Type": "application/json; charset=utf-8",
"Host": "api.zomato.com",
"X-Zomato-API-Key": "7749b19667964b87a3efc739e254ada2",
"X-Zomato-App-Version": "931",
"X-Zomato-App-Version-Code": "1710019310",
"X-Zomato-Client-Id": "5276d7f1-910b-4243-92ea-d27e758ad02b",
"X-Zomato-Is-Metric": "true",
"X-Zomato-UUID": "b2691abb-5aac-48a5-9f0e-750349080dcb"
}
try:
response = requests.post(url, json=payload, headers=headers, proxies=proxies, verify=True if not proxies else False)
response.raise_for_status()
data = response.json()
lat, lng = extract_lat_long(data)
success = lat is not None and lng is not None
return {"latitude": lat, "longitude": lng, "success": success}
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return {"latitude": None, "longitude": None, "success": False}
except json.JSONDecodeError:
print("Response was not JSON:")
print(response.text)
return {"latitude": None, "longitude": None, "success": False}
def extract_lat_long(data: dict) -> tuple[float | None, float | None]:
"""Extract latitude and longitude from the JSON."""
if not data:
return None, None
for result in data.get('results', []):
if 'v4_image_text_snippet_type_3' in result:
items = result['v4_image_text_snippet_type_3'].get('items', [])
for item in items:
icon_containers = item.get('icon_text_containers', [])
for container in icon_containers:
click_action = container.get('click_action', {})
if click_action.get('type') == 'open_map':
open_map = click_action.get('open_map', {})
lat = open_map.get('latitude')
lng = open_map.get('longitude')
if lat is not None and lng is not None:
return lat, lng
return None, None