This repository was archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsub_client.py
105 lines (95 loc) · 4.18 KB
/
sub_client.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
import json
import aiohttp
from lib.client_data import ClientData
from lib.devices import Device
from lib.exceptions import *
from lib.headers import Headers
from lib.objects import *
class SubClient:
"""
Class contains actions inside a specific community
"""
def __init__(self, communityId=None):
self.headers = Headers().headers
self.mobile_headers = Headers().mobile_headers
self.device = Device().create_device()
self.interface = "https://aminoapps.com/api"
self.mobile_interface = "https://service.narvii.com/api/v1"
self.communityId = communityId
self.headers["cookie"] = ClientData().data["session"]
self.mobile_headers["NDCAUTH"] = ClientData().data["session"]
self.cookies = ClientData().data["session"]
async def join_chat(self, chat_id):
data = {
"ndcId": f"x{self.communityId}",
"threadId": chat_id
}
async with aiohttp.ClientSession() as session:
async with session.post(f"{self.interface}/join-thread", json=data, headers=self.headers) as result:
response = await result.text()
response = json.loads(response)
if response["code"] == 200:
return response["code"]
else:
raise UnexpectedException(response)
async def get_my_chats(self, start_offset=0, end_offset=10):
async with aiohttp.ClientSession() as session:
async with session.get(
f"{self.mobile_interface}/x{self.communityId}/s/chat/thread?type=joined-me&start={start_offset}&size={end_offset}",
headers=self.mobile_headers) as result:
response = await result.json()
try:
response = response["threadList"]
chat = Chats(response)
return chat
except:
raise UnexpectedException(response)
async def chatThreadMessages(self, chatId, size=25):
data = {
"ndcId": f"x{self.communityId}",
"threadId": chatId,
"size": size
}
async with aiohttp.ClientSession() as session:
async with session.post(f"{self.interface}/chat-thread-messages", json=data,
headers=self.headers) as result:
response = await result.text()
response = json.loads(response)
if response["code"] == 200:
return response["result"]
else:
raise UnexpectedException(response)
async def get_online_users(self, start_offset=0, end_offset=10):
async with aiohttp.ClientSession() as session:
async with session.post(
f"{self.interface}/chat-thread-messages",
headers=self.headers) as result:
response = await result.json()
try:
response = response["userProfileList"]
users = User(response)
return users
except:
raise UnexpectedException(response)
async def start_conversation(self, targetId, text):
data = {
"ndcId": self.communityId,
"inviteeUids": targetId,
"initialMessageContent": text,
"type": 0
}
async with aiohttp.ClientSession() as session:
async with session.post(f"{self.interface}/create-chat-thread", json=data, headers=self.headers) as result:
response = await result.text()
response = json.loads(response)
if response["code"] == 200:
return response["code"]
else:
if response["api_status_code"] == 270:
raise RequestedApproval(response)
elif response["api_status_code"] == 603:
raise UserBlocked(response["message"])
elif response["api_status_code"] == 1611:
raise DisabledInviting(response)
else:
raise UnexpectedException(response)