-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconversations.py
147 lines (122 loc) · 4.79 KB
/
conversations.py
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
import json
import logging
import urllib.parse
import requests
import websocket
from squeaky_hinge_config import *
def fetch_conversations(include_messages):
logging.info(f"START inbox flow")
with open("hinge_creds.json") as f:
data = json.load(f)
hinge_token = data["hinge_token"]
user_id = data["user_id"]
hinge_install_id = data["hinge_install_id"]
logging.info(f"Using Hinge token {hinge_token}")
logging.info(f"Using user ID {user_id}")
logging.info(f"Using Hinge install ID {hinge_install_id}")
logging.info(f"Using X-App-Version {hinge_android_package}")
logging.info(f"Using X-Device-Platform {hinge_apk_sha1sum}")
resp = requests.post(
"https://prod-api.hingeaws.net/message/authenticate",
headers={
"Authorization": f"Bearer {hinge_token}",
"X-App-Version": hinge_app_version,
"X-Device-Platform": hinge_device_platform,
"X-Install-Id": hinge_install_id,
},
json={"refresh": False},
)
logging.info(
f"Invoked message/authenticate and got response code {resp.status_code} and body: {resp.text}"
)
if not resp.ok:
raise Exception(
f"got response code {resp.status_code} when fetching Sendbird access token"
)
sendbird_auth_data = resp.json()
sendbird_access_token = sendbird_auth_data["token"]
logging.info(f"Got SendBird access token {sendbird_access_token}")
logging.info(f"Using SendBird application ID {sendbird_application_id}")
ws = websocket.create_connection(
f"wss://ws-{sendbird_application_id.lower()}.sendbird.com?"
+ urllib.parse.urlencode(
{
"ai": sendbird_application_id,
"user_id": user_id,
"access_token": sendbird_access_token,
}
),
)
sendbird_ws_message = ws.recv()
logging.info(f"Opened SendBird websocket and got message {sendbird_ws_message}")
sendbird_ws_data = json.loads(sendbird_ws_message.removeprefix("LOGI"))
sendbird_session_key = sendbird_ws_data["key"]
logging.info(f"Got SendBird session key {sendbird_session_key}")
resp = requests.get(
f"https://api-{sendbird_application_id.lower()}.sendbird.com/v3/users/{user_id}/my_group_channels",
headers={
"Accept": "application/json",
"Session-Key": sendbird_session_key,
},
params={
"show_member": "true",
"show_frozen": "true",
"public_mode": "all",
"member_state_filter": "all",
"hidden_mode": "unhidden_only",
"show_delivery_receipt": "true",
"distinct_mode": "all",
"unread_filter": "all",
"token": "",
"show_read_receipt": "true",
"super_mode": "all",
"show_metadata": "true",
"limit": "20",
"show_empty": "true",
"order": "latest_last_message",
},
)
logging.info(
f"Invoked my_group_channels and got response code {resp.status_code} and body: {resp.text}"
)
if not resp.ok:
raise Exception(
f"got response code {resp.status_code} when fetching conversations"
)
conversations = resp.json()
if include_messages:
for channel in conversations['channels']:
channel['messages'] = fetch_messages(sendbird_session_key, channel)
with open("conversations.json", "w") as f:
json.dump(conversations, f, indent=2)
f.write("\n")
logging.info(f"SUCCESS inbox flow")
def fetch_messages(sendbird_session_key, channel):
logging.info(f"START message flow {channel['channel_url']}")
messages = []
last_message_time = channel['invited_at']
while last_message_time < channel['last_message']['created_at']:
resp = requests.get(
f"https://api-{sendbird_application_id.lower()}.sendbird.com/v3/group_channels/{channel['channel_url']}/messages",
headers={
"Accept": "application/json",
"Session-Key": sendbird_session_key,
},
params={
"message_ts": last_message_time,
"next_limit": 200,
},
)
logging.info(f"Invoked group_channels and got response code {resp.status_code}")
if not resp.ok:
logging.info(
f"Invoked group_channels and got response code {resp.status_code} and body: {resp.text}"
)
raise Exception(
f"got response code {resp.status_code} when fetching messages"
)
parsed = resp.json()
messages += parsed['messages']
last_message_time = messages[-1]['created_at']
logging.info(f"SUCCESS message flow {channel['channel_url']}")
return messages