forked from jatin-dot-py/zomato-intelligence
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_menu.py
More file actions
106 lines (92 loc) · 3.69 KB
/
get_menu.py
File metadata and controls
106 lines (92 loc) · 3.69 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
import requests
import json
def get_friend_recommendations(zomato_token, res_id, proxies=None):
"""
Fetches menu data and extracts friend recommendations (what users ate).
Returns dict with success status and the parsed list of user orders.
"""
url = f"https://api.zomato.com/gw/menu/{res_id}"
headers = {
"Accept": "image/webp",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "api.zomato.com",
"X-Zomato-Access-Token": zomato_token,
"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"
}
data = {
"resID": res_id,
"mode": "delivery",
"tabId": "menu"
}
# --- Internal Parsing Helpers ---
def organize_by_user(parsed_json):
users = parsed_json.get("user_details", [])
items = parsed_json.get("recommended_items", [])
structured_users = []
for user in users:
target_uid = user.get("encrypted_owner_user_id")
user_items = []
# Filter items recommended by THIS specific user
for item in items:
if target_uid in item.get("user_ids", []):
user_items.append({
"name": item.get("name"),
"price": item.get("price"),
# Clean the image URL inline (remove query params)
"image": item.get("image_url", "").split('?')[0]
})
structured_users.append({
"user_id": target_uid,
"ordered_items": user_items
})
return structured_users
def extract_from_json(json_data):
final_output = []
def search_tree(obj):
if isinstance(obj, dict):
# Check for the specific action type and url
click_action = obj.get("click_action")
if click_action:
sheet = click_action.get("open_dish_items_bottom_sheet", {})
if sheet.get("url") == "/gw/menu/friends_recommendation":
raw_body = sheet.get("post_body")
if raw_body:
try:
parsed_body = json.loads(raw_body)
users_data = organize_by_user(parsed_body)
final_output.extend(users_data)
except json.JSONDecodeError:
pass
# Continue searching
for value in obj.values():
search_tree(value)
elif isinstance(obj, list):
for item in obj:
search_tree(item)
search_tree(json_data)
return final_output
# --------------------------------
try:
response = requests.post(url, headers=headers, data=data, proxies=proxies, verify=True if not proxies else False)
success = 200 <= response.status_code < 300
recommendations = []
if success:
recommendations = extract_from_json(response.json())
return {
'success': success,
'status_code': response.status_code,
'data': recommendations,
'error': None
}
except Exception as e:
return {
'success': False,
'status_code': None,
'data': [],
'error': str(e)
}