import requests from requests.exceptions import RequestException import json import platform DOMAIN = "https://solian.app/api" UA = f"SolianForPythonApp/0.000.001({platform.system()})" def _make_request(method: str, url: str, headers: dict, params: dict = None, NormalCode: int = [204],RequestsBody: dict | int = None) -> dict: """内部辅助函数,用于发送HTTP请求并处理响应""" headers['User-Agent'] = UA try: if method == 'GET': response = requests.get(url, headers=headers, params=params) elif method == 'POST': response = requests.post(url, headers=headers, data=json.dumps(RequestsBody)) elif method == 'DELETE': response = requests.delete(url, headers=headers, params=params) elif method == 'PATCH': response = requests.patch(url, headers=headers, data=json.dumps(RequestsBody)) else: return {"error": "Unsupported HTTP method"} if response.status_code != 200 and response.status_code not in NormalCode: return {"error": response.status_code} return response.json() except json.JSONDecodeError: return {"error": response.text} except RequestException as e: return {"error": str(e)} def ActivityAPIs(cursor: str = '', filter: str = '', take: int = 20, debuginclude: str = '', Authorization: str = '') -> dict: """获取首页内容""" url = f"{DOMAIN}/activities" headers = {'accept': 'application/json', 'Authorization': Authorization} params = {"cursor": cursor, "filter": filter, "take": take, "debuginclude": debuginclude} return _make_request('GET', url, headers, params) ###=========================聊天API================================= def ChatSummary(Authorization: str) -> dict: """获取聊天摘要""" url = f"{DOMAIN}/chat/summary" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('GET', url, headers) def GetChatMessageAllInfo(Authorization: str, roomid: str, offset: int = 0, take: int = 20) -> dict: """获取聊天消息""" url = f"{DOMAIN}/chat/{roomid}/message" headers = {'accept': 'application/json', 'Authorization': Authorization} params = {"offset": offset, "take": take} return _make_request('GET', url, headers, params) def SendChatMessage(Authorization: str, roomid: str,content: str,nonce: str,attachments_id: list,meta: dict,replied_message_id: str,forwarded_message_id: str) -> dict: """发送聊天消息""" RequestsBody={ "content": content, "nonce": nonce, "attachments_id": attachments_id, "meta": meta, "replied_message_id": replied_message_id, "forwarded_message_id": forwarded_message_id } url = f"{DOMAIN}/chat/{roomid}/message" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('POST', url, headers,RequestsBody=RequestsBody,NormalCode=[201]) def GetAMessageAllInfo(Authorization: str, roomid: str, messageid: str) -> dict: """获取具体聊天消息所有信息""" url = f"{DOMAIN}/chat/{roomid}/message/{messageid}" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('GET', url, headers) def ModifyChatMessage(Authorization: str, roomid: str, messageid: str,content: str,nonce: str,attachments_id: list,meta: dict,replied_message_id: str,forwarded_message_id: str) -> dict: """修改聊天消息""" RequestsBody={ "content": content, "nonce": nonce, "attachments_id": attachments_id, "meta": meta, "replied_message_id": replied_message_id, "forwarded_message_id": forwarded_message_id } url = f"{DOMAIN}/chat/{roomid}/message/{messageid}" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('PATCH', url, headers,RequestsBody=RequestsBody) def DeleteAMessageInfo(Authorization: str, roomid: str, messageid: str) -> dict: """删除聊天消息基础信息。暂时作为保留""" url = f"{DOMAIN}/chat/{roomid}/message/{messageid}" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('DELETE', url, headers) def DeleteChatMessage(Authorization: str, roomid: str, messageid: str) -> dict: """删除聊天消息""" url = f"{DOMAIN}/chat/{roomid}/message/{messageid}" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('DELETE', url, headers) def SyncChatMessage(Authorization: str, roomid: str, last_sync_timestamp: int) -> dict: """同步聊天消息""" url = f"{DOMAIN}/chat/{roomid}/sync" RequestsBody={ "last_sync_timestamp": last_sync_timestamp } headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('POST', url, headers,RequestsBody=RequestsBody) ###=========================聊天室API================================= def GetChatRoomInfo(Authorization: str, id: str) -> dict: """获取聊天房间信息""" url = f"{DOMAIN}/chat/{id}" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('GET', url, headers) def ModifyChatRoomInfo(Authorization: str, id: str, name: str, description: str, picture_id: str, backgournd_id: str, realm_id: str, is_community: bool, is_public: bool) -> dict: """修改聊天房间信息""" RequestsBody={ "name": name, "description": description, "picture_id": picture_id, "background_id": backgournd_id, "realm_id": realm_id, "is_community": is_community, "is_public": is_public, } url = f"{DOMAIN}/chat/{id}" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('PATCH', url, headers,RequestsBody=RequestsBody) def DeleteChatRoom(Authorization: str, id: str) -> dict: """删除聊天房间""" url = f"{DOMAIN}/chat/{id}" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('DELETE', url, headers,NormalCode=[204]) def GetChatList(Authorization: str) -> dict: """获取聊天列表""" url = f"{DOMAIN}/chat" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('GET', url, headers) def CreateChatWithRealm(Authorization: str, name: str, description: str, picture_id: str, backgournd_id: str, realm_id: str, is_community: bool, is_public: bool) -> dict: """创建聊天房间""" RequestsBody={ "name": name, "description": description, "picture_id": picture_id, "background_id": backgournd_id, "realm_id": realm_id, "is_community": is_community, "is_public": is_public, } url = f"{DOMAIN}/chat" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('POST', url, headers,RequestsBody=RequestsBody,NormalCode=[201]) def GetChatToAccount(Authorization: str,accountid: str) -> dict: """获取聊天对象用户信息""" url = f"{DOMAIN}/chat/direct/{accountid}" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('GET', url, headers) def GetChatRoomSelfInfo(Authorization: str,roomid:str) -> dict: """获取聊天房间自己信息""" url = f"{DOMAIN}/chat/{roomid}/members/me" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('GET', url, headers) def JoinChatRoom(Authorization: str,roomid:str) -> dict: """加入聊天房间""" url = f"{DOMAIN}/chat/{roomid}/members/me" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('POST', url, headers,NormalCode=[201]) def LeaveChatRoom(Authorization: str,roomid:str) -> dict: """退出聊天房间""" url = f"{DOMAIN}/chat/{roomid}/members/me" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('DELETE', url, headers,NormalCode=[204]) def GetChatRoomMembers(Authorization: str,roomid:str) -> dict: """获取聊天房间成员""" url = f"{DOMAIN}/chat/{roomid}/members" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('GET', url, headers) def SendChatInvites(Authorization: str,accountid: str,role: str) -> dict: """发送聊天邀请""" RequestsBody={ "related_user_id": accountid, "role": role } url = f"{DOMAIN}/chat/invites" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('POST', url, headers,RequestsBody=RequestsBody,NormalCode=[201]) def GetChatInvites(Authorization: str) -> dict: """获取聊天邀请""" url = f"{DOMAIN}/chat/invites" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('GET', url, headers) def AcceptChatInvite(Authorization: str,roomid:str) -> dict: """接受聊天邀请""" url = f"{DOMAIN}/chat/invites/{roomid}" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('POST', url, headers) def DeclineChatInvite(Authorization: str,roomid:str) -> dict: """拒绝聊天邀请""" url = f"{DOMAIN}/chat/invites/{roomid}" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('DELETE', url, headers) def SetChatNotify(Authorization: str,roomid:str,notify_level: str,break_until: str) -> dict: """设置聊天通知级别""" RequestsBody={ "notify_level": notify_level, "break_until": break_until } url = f"{DOMAIN}/chat/{roomid}/members/me/notify" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('PATCH', url, headers,RequestsBody=RequestsBody,NormalCode=[201]) def SetChatMemberRole(Authorization: str,roomid:str,membersid: str,role: int) -> dict: """设置聊天成员角色""" url=f"{DOMAIN}/chat/{roomid}/members/{membersid}/role" headers = {'accept': 'application/json', 'Authorization': Authorization} return _make_request('PATCH', url, headers,RequestsBody=role,NormalCode=[201])