forked from jatin-dot-py/zomato-intelligence
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_contact_collection.py
More file actions
179 lines (153 loc) · 6.44 KB
/
get_contact_collection.py
File metadata and controls
179 lines (153 loc) · 6.44 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
import requests
import json
import re
def get_contact_collection(zomato_token, encrypted_owner_user_id, owner_name, proxies=None):
"""
Fetch a contact's restaurant collection.
Uses impossible location (9999999999) so Zomato can't filter by proximity.
Extracts res_id from postback_params (the actual saved restaurant ID).
"""
url = "https://api.zomato.com/gateway/search/v1/get_listing_by_usecase"
headers = {
"Accept": "image/webp",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/json; charset=UTF-8",
"Host": "api.zomato.com",
"X-Client-Id": "zomato_android_v2",
"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"
}
# Impossible location - Zomato can't filter by proximity
FAKE_LOCATION = {
"entity_type": "subzone",
"entity_id": "9999999999",
"place_type": "DSZ",
"place_id": "9999999999",
}
all_restaurants = []
real_user_id = None
processed_chain_ids = []
shown_res_count = 0
has_more = True
page = 0
raw_responses = []
# Suppress console output from this function to keep the orchestrator clean
# You can re-enable these prints for debugging if needed
# print_enabled = False
while has_more and page < 5:
page += 1
if page == 1:
payload = {
"request_type": "initial",
"page_type": "collection_page",
"friend_recommendation_params": json.dumps({
"collection_type": "RECOMMENDATION",
"owner_user_name": owner_name,
"encrypted_owner_user_id": encrypted_owner_user_id
}),
"count": 10,
"view_type": "RESTAURANT",
"additional_filters": {},
"removed_snippet_ids": [],
"page_index": "1",
"location": FAKE_LOCATION,
"is_gold_mode_on": False,
"keyword": "",
"load_more": False
}
else:
payload = {
"request_type": "load_more",
"page_type": "collection_page",
"friend_recommendation_params": json.dumps({
"collection_type": "RECOMMENDATION",
"owner_user_name": owner_name,
"encrypted_owner_user_id": encrypted_owner_user_id
}),
"count": 10,
"view_type": "RESTAURANT",
"postback_params": json.dumps({
"processed_chain_ids": processed_chain_ids,
"shown_res_count": shown_res_count,
"view_type": "RESTAURANT",
"encrypted_owner_user_id": encrypted_owner_user_id,
"owner_user_name": owner_name,
"collection_type": "RECOMMENDATION"
}),
"previous_search_params": json.dumps({"ViewType": "RESTAURANT"}),
"additional_filters": {},
"removed_snippet_ids": [],
"page_index": "1",
"location": FAKE_LOCATION,
"is_gold_mode_on": False,
"keyword": "",
"load_more": True
}
response = requests.post(url, headers=headers, json=payload, proxies=proxies, verify=True if not proxies else False)
if response.status_code != 200:
# if print_enabled: print(f"Page {page}: Error {response.status_code}")
break
data = response.json()
results = data.get('results', [])
raw_responses.append({"page": page, "response": data})
# if print_enabled: print(f"Page {page}: {len(results)} results, has_more={data.get('has_more')}")
if not results:
break
for r in results:
if 'v2_res_snippet_type_3' not in r:
continue
snippet = r['v2_res_snippet_type_3']
displayed_id = r['layout_config']['id']
name = snippet.get('title', {}).get('text', '')
# Extract res_id from postback_params (the ACTUAL saved restaurant)
right_btn = snippet.get('right_button', {})
manage = right_btn.get('click_action', {}).get('manage_collection', {})
postback_str = manage.get('postback_params', '{}')
try:
postback = json.loads(postback_str)
res_id = postback.get('res_id', displayed_id)
chain_id = postback.get('chain_id', displayed_id)
except:
res_id = displayed_id
chain_id = displayed_id
# Track for pagination
if int(displayed_id) not in processed_chain_ids:
processed_chain_ids.append(int(displayed_id))
# Extract real user ID from tracking data
if not real_user_id:
for t in snippet.get('tracking_data', []):
# Updated regex to capture both numeric (\d+) and alphanumeric/hex (\w+) user IDs.
match = re.search(r'recommendation_tenant_o2_user_(\w+)', t.get('payload', ''))
if match:
real_user_id = match.group(1)
# Once found, we can stop searching in this snippet's tracking data
break
all_restaurants.append({
'name': name,
'res_id': res_id,
'chain_id': chain_id,
'displayed_id': displayed_id
})
shown_res_count = len(all_restaurants)
has_more = data.get('has_more', False)
# Dedupe by res_id
seen = set()
unique = []
for r in all_restaurants:
if r['res_id'] not in seen:
seen.add(r['res_id'])
unique.append(r)
try:
int(real_user_id)
except:
real_user_id = None
return {
'success': True,
'real_user_id': real_user_id, # This will be None if no ID was found
'total': len(unique),
'restaurants': unique,
'raw_responses': raw_responses
}