forked from jatin-dot-py/zomato-intelligence
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_contacts.py
More file actions
87 lines (71 loc) · 2.94 KB
/
get_contacts.py
File metadata and controls
87 lines (71 loc) · 2.94 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
import requests
import re
def find_items_recursive(obj):
"""Recursively find all items arrays."""
all_items = []
if isinstance(obj, dict):
for key, value in obj.items():
if key == 'items' and isinstance(value, list):
for item in value:
all_items.append(item)
all_items.extend(find_items_recursive(item))
else:
all_items.extend(find_items_recursive(value))
elif isinstance(obj, list):
for item in obj:
all_items.extend(find_items_recursive(item))
return all_items
def get_contacts(zomato_token, proxies=None):
url = "https://api.zomato.com/gw/user-preference/recommendation/get-contacts"
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-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"
}
payload = {
"request_type": "initial",
"additional_filters": {},
"page_type": "contacts_management",
"removed_snippet_ids": [],
"page_index": "1",
"count": 99999,
"is_gold_mode_on": False,
"keyword": "",
"load_more": True
}
response = requests.post(url, json=payload, headers=headers, proxies=proxies, verify=True if not proxies else False)
response.raise_for_status()
data = response.json()
if 'results' not in data:
return []
all_items = find_items_recursive(data['results'])
users = []
for item in all_items:
# Skip container objects (no title with text = not a contact)
name = item.get('title', {}).get('text', '')
if not name:
continue
# Extract recommendations
subtitle = item.get('subtitle', {}).get('text', '')
reco_match = re.search(r'(\d+)\s+recommendation', subtitle)
recommendations = int(reco_match.group(1)) if reco_match else 0
# Extract encrypted_owner_user_id - this is the indicator of a zomato user
url_str = item.get('click_action', {}).get('deeplink', {}).get('url', '')
enc_match = re.search(r'"encrypted_owner_user_id":\s*"([^"]+)"', url_str)
encrypted_owner_user_id = enc_match.group(1) if enc_match else None
is_zomato_user = encrypted_owner_user_id is not None
users.append({
'name': name,
'is_zomato_user': is_zomato_user,
'has_recommendations': recommendations > 0,
'recommendations': recommendations,
'encrypted_owner_user_id': encrypted_owner_user_id
})
return users