Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AugustLigh committed May 14, 2024
1 parent 0731cfd commit fb37175
Show file tree
Hide file tree
Showing 8 changed files with 2,057 additions and 2,017 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,6 @@ cython_debug/
#.idea/
test.py
free-nature-images.jpg
test.mp3
test.aac
test.wav
9 changes: 3 additions & 6 deletions AminoLightPy/acm.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,11 @@ def get_community_user_stats(self, type: str, start: int = 0, size: int = 25):
- **Fail** : :meth:`Exceptions <AminoLightPy.lib.util.exceptions>`
"""
if type.lower() == "leader":
target = "leader"
elif type.lower() == "curator":
target = "curator"
else: raise exceptions.WrongType(type)
if type.lower() not in ("leader", "curator"):
raise exceptions.WrongType(type)

data = {
"type": target,
"type": type.lower(),
"start": start,
"size": size
}
Expand Down
21 changes: 17 additions & 4 deletions AminoLightPy/amino_socket.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=invalid-name
# pylint: disable=no-member

import ssl
import traceback
import threading

Expand Down Expand Up @@ -47,6 +48,13 @@ def on_open(self, ws):
self.debug_print("[socket][start] Socket Started")
self.thread_event.set()

def on_ping(self, ws, somesing):
self.debug_print("[socket][Ping] Servier send ping")
self.send(dumps({"t": 116, "o": {"threadChannelUserInfoList": []}}))

def on_pong(self, ws, somesing):
self.debug_print("server send pong")

def starting_process(self):
deviceId = gen_deviceId()

Expand All @@ -62,14 +70,18 @@ def starting_process(self):
f"{self.socket_url}/?signbody={final.replace('|', '%7C')}",
on_open=self.on_open,
on_error=self.on_error,
on_ping=self.on_ping,
on_pong=self.on_pong,
on_message=self.on_message,
on_close=self.on_close,
header=headers,
)

threading.Thread(target=self.socket.run_forever, kwargs={
"ping_interval": 10,
"ping_payload": '{"t": 116, "o": {"threadChannelUserInfoList": []}}'
"sslopt": {"cert_reqs": ssl.CERT_NONE},
"skip_utf8_validation": True,
"ping_interval": 60*5,
"ping_payload": dumps({"t": 116, "o": {"threadChannelUserInfoList": []}})
}).start()

def run_amino_socket(self):
Expand Down Expand Up @@ -142,7 +154,7 @@ def leave_from_live_chat(self, chatId: str):
def run_vc(self, comId: int, chatId: str, joinType: str):
while chatId in self.active_live_chats:
try:
self.join_video_chat(
self.join_voice_chat(
comId=comId,
chatId=chatId,
joinType=joinType
Expand All @@ -153,7 +165,7 @@ def run_vc(self, comId: int, chatId: str, joinType: str):
print(e)

def start_vc(self, comId: int, chatId: str, joinType: int = 1):
self.join_video_chat(
self.join_voice_chat(
comId=comId,
chatId=chatId,
joinType=joinType
Expand All @@ -162,6 +174,7 @@ def start_vc(self, comId: int, chatId: str, joinType: int = 1):
"o": {
"ndcId": int(comId),
"threadId": chatId,
"jointype": joinType,
"channelType": 1,
},
"t": 108
Expand Down
13 changes: 9 additions & 4 deletions AminoLightPy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# pylint: disable=too-many-lines

from uuid import uuid4
from base64 import b64encode
from typing import BinaryIO, Union
from .managers import Typing, Recording

Expand Down Expand Up @@ -1062,10 +1063,14 @@ def send_message(self, chatId: str, message: str = None, messageType: int = 0,
data["type"] = 3

if file:
data["content"] = None
url = upload_media(self, file)

data["mediaValue"] = url
if fileType == "audio":
data["type"] = 2
data["mediaType"] = 110
data["mediaUploadValue"] = b64encode(file.read()).decode()

else:
url = upload_media(self, file)
data["mediaValue"] = url

response = self.session.post(f"{api}/g/s/chat/thread/{chatId}/message", json=data)
return response.status_code
Expand Down
7 changes: 2 additions & 5 deletions AminoLightPy/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
api = "http://service.aminoapps.com:80/api/v1"
device_id = gen_deviceId()
cache = OrderedDict()
cache_len = 32

class AminoSession(Session):
def __init__(self) -> None:
Expand Down Expand Up @@ -74,10 +75,6 @@ def upload_media(self, file: BinaryIO) -> str:
return cache[file_hash]

fileType = guess_type(file.name)[0]
if fileType not in (
"image/gif", "image/jpg",
"audio/aac", "audio/png"
): raise SpecifyType(fileType) #but this check can be removed, I think

custom_headers = self.session.headers
custom_headers["Content-Type"] = fileType
Expand All @@ -90,7 +87,7 @@ def upload_media(self, file: BinaryIO) -> str:
)

cache[file_hash] = response.json()["mediaValue"]
if len(cache) >= 32:
if len(cache) >= cache_len:
cache.popitem(last=False)

return cache[file_hash]
Expand Down
60 changes: 39 additions & 21 deletions AminoLightPy/lib/util/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ class UserProfileList:

def __init__(self, data):
self.json = data
if not data:
for attr in self.__slots__:
setattr(self, attr, None)

return

_userObjects = tuple(UserProfile(x).UserProfile for x in data)

set_attributes(self, _userObjects)
Expand Down Expand Up @@ -190,12 +196,12 @@ class RecentBlogs:
)

def __init__(self, data):
self.json = data
if not data:
for attr in self.__slots__:
setattr(self, attr, None)

return
self.json = data

self.nextPageToken = None
self.prevPageToken = None
Expand Down Expand Up @@ -338,7 +344,7 @@ def __init__(self, data):
extensions = data.get("extensions") or {}
style = extensions.get("style") or {}
knowledgeBase = extensions.get("knowledgeBase") or {}
self.labels = WikiLabelList(extensions.get("props", [])).WikiLabelList
self.labels = WikiLabelList(extensions.get("props")).WikiLabelList

self.wikiId = data.get("itemId")
self.status = data.get("status")
Expand Down Expand Up @@ -397,18 +403,19 @@ def WikiList(self):

class WikiLabelList:
__slots__ = ("json", "title", "content", "type")
def __init__(self, data):
def __init__(self, data: dict):
if not data:
for attr in self.__slots__:
setattr(self, attr, None)

return
self.json = data
self.title = []
self.content = []
self.type = []
self.title = data.get("title")
self.content = data.get("value")
self.type = data.get("type")

@property
def WikiLabelList(self):
for x in self.json:
self.title.append(x.get("title"))
self.content.append(x.get("value"))
self.type.append(x.get("type"))

return self

Expand Down Expand Up @@ -446,7 +453,7 @@ class Community:
"welcomeMessage", "welcomeMessageEnabled", "hasPendingReviewRequest",
"frontPageLayout", "themeColor", "themeHash", "themeVersion", "themeUrl",
"themeHomePageAppearance", "themeLeftSidePanelTop", "themeLeftSidePanelBottom",
"themeLeftSidePanelColor", "customList"
"themeLeftSidePanelColor", "customList", "communityHeadList"
)

def __init__(self, data: Dict):
Expand All @@ -459,6 +466,7 @@ def __init__(self, data: Dict):
self.json = data

self.agent = UserProfile(data.get("agent")).UserProfile
self.communityHeadList = UserProfileList(data.get("communityHeadList", [])).UserProfileList

themePack: Dict = data.get("themePack") or {}
configuration: Dict = data.get("configuration") or {}
Expand Down Expand Up @@ -542,7 +550,7 @@ class CommunityList:
"welcomeMessage", "welcomeMessageEnabled", "hasPendingReviewRequest",
"frontPageLayout", "themeColor", "themeHash", "themeVersion", "themeUrl",
"themeHomePageAppearance", "themeLeftSidePanelTop", "themeLeftSidePanelBottom",
"themeLeftSidePanelColor", "customList"
"themeLeftSidePanelColor", "customList", "communityHeadList"
)

def __init__(self, data: dict):
Expand Down Expand Up @@ -882,6 +890,11 @@ class WikiCategoryList:
)

def __init__(self, data):
if not data:
for attr in self.__slots__:
setattr(self, attr, None)
return

_author = [x.get("author") for x in data]

self.json = data
Expand Down Expand Up @@ -928,7 +941,7 @@ def __init__(self, data):
childrenWrapper = data.get("childrenWrapper") or {}

self.author = UserProfile(itemCategory.get("author")).UserProfile
self.subCategory = WikiCategoryList(childrenWrapper.get("itemCategoryList", [])).WikiCategoryList
self.subCategory = WikiCategoryList(childrenWrapper.get("itemCategoryList")).WikiCategoryList

self.itemsCount = itemCategory.get("itemsCount")
self.parentCategoryId = itemCategory.get("parentCategoryId")
Expand Down Expand Up @@ -1281,8 +1294,8 @@ def __init__(self, data):
self.author = UserProfile(data.get("author")).UserProfile

extensions = data.get("extensions") or {}
videoExtensions = extensions.get("videoExtensions") or {}

self.videoExtensions = extensions.get("videoExtensions") or {}
self.sticker = Sticker(extensions.get("sticker")).Sticker

self.content = data.get("content")
Expand All @@ -1301,10 +1314,10 @@ def __init__(self, data):
self.mediaValue = data.get("mediaValue")
self.extensions = extensions
self.duration = extensions.get("duration")
self.videoDuration = videoExtensions.get("duration")
self.videoHeight = videoExtensions.get("height")
self.videoWidth = videoExtensions.get("width")
self.videoCoverImage = videoExtensions.get("coverImage")
self.videoDuration = self.videoExtensions.get("duration")
self.videoHeight = self.videoExtensions.get("height")
self.videoWidth = self.videoExtensions.get("width")
self.videoCoverImage = self.videoExtensions.get("coverImage")
self.originalStickerId = extensions.get("originalStickerId")
self.mentionUserIds = tuple(m.get("uid") for m in extensions.get("mentionedArray", ()))
self.tippingCoins = extensions.get("tippingCoins")
Expand All @@ -1321,14 +1334,17 @@ class MessageList:
"mediaValue", "chatBubbleId", "clientRefId", "chatId", "createdTime",
"chatBubbleVersion", "type", "extensions", "mentionUserIds", "duration",
"originalStickerId", "videoExtensions", "videoDuration", "videoHeight",
"videoWidth", "videoCoverImage", "tippingCoins"
"videoWidth", "videoCoverImage", "tippingCoins", "replyMessage"
)
def __init__(self, data, nextPageToken = None, prevPageToken = None):
self.json = data
self.nextPageToken = nextPageToken
self.prevPageToken = prevPageToken
_messageyObjects = tuple(Message(x).Message for x in data)

self.author = UserProfileList([x.author.json for x in _messageyObjects]).UserProfileList
self.sticker = StickerList([x.sticker.json for x in _messageyObjects]).StickerList

set_attributes(self, _messageyObjects)

@property
Expand Down Expand Up @@ -2382,4 +2398,6 @@ def set_attributes(instance, _ListObjects):
if _ListObjects:
attributes = tuple(attr for attr in dir(_ListObjects[0]) if not attr.startswith("__") and not callable(getattr(_ListObjects[0], attr)) and attr != _ListObjects[0].__class__.__name__)
for attr in attributes:
setattr(instance, attr, tuple(getattr(user, attr, None) for user in _ListObjects))
if not hasattr(instance, attr):
setattr(instance, attr, tuple(getattr(user, attr, None) for user in _ListObjects))

Loading

0 comments on commit fb37175

Please sign in to comment.