Skip to content

Commit

Permalink
Merge pull request #1 from tusharsadhwani/main
Browse files Browse the repository at this point in the history
At least it compiles now
  • Loading branch information
crispyaus authored Apr 28, 2021
2 parents 189bbf0 + 5653e44 commit eee3d74
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 66 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ dist/
.vscode/
.idea/
*/**/__pycache__/*
venv/
*.egg-info/

# Testing scripts
sample.py
Expand Down
94 changes: 37 additions & 57 deletions dogehouse/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ async def on_ready(self):
"""

def wrapper(_func: Awaitable):
listeners[(Convertor.convert_basic_types(name, str) if name else func.__name__).lower()] = [func, False]
listeners[(Convertor.convert_basic_types(name, str)
if name else _func.__name__).lower()] = [_func, False]

return wrapper(func) if func else wrapper

Expand Down Expand Up @@ -152,7 +153,8 @@ async def __fetch(self, op: str, data: dict):

async def __send(self, opcode: str, data: dict, *, fetch_id: str = None):
"""Internal websocket sender method."""
raw_data = dict(op=opcode, d=data)
raw_data = dict(op=opcode, d=data,
reference=str(uuid4()), version="0.2.0")
if fetch_id:
raw_data["fetchId"] = fetch_id
await self.__socket.send(dumps(raw_data))
Expand All @@ -176,7 +178,6 @@ async def execute_listener(listener: str, *args):

async def execute_command(command_name: str, ctx: Context, *args):
_command = self.__commands.get(command_name.lower())

if _command:
instance_id = f"{command_name}-{ctx.author.id}"
invoked_at = time()
Expand Down Expand Up @@ -229,11 +230,21 @@ async def execute_command(command_name: str, ctx: Context, *args):

info("Dogehouse: Starting event listener loop")
while self.__active:
res: Dict[str, Union[Dict, Any]] = loads(await self.__socket.recv())
op = res if isinstance(res, str) else res.get("op")
if op == "auth-good":
info("Dogehouse: Received client ready")
self.user = User.from_dict(dict(res["d"]["user"]))
debug('waiting for a message')
res_json = await self.__socket.recv()
try:
res: Dict[str, Union[Dict, Any]] = loads(res_json)
except:
debug("Not json")
debug(f'{res_json=}')
continue

debug("Successfully parsed json")
op = res.get("op")
debug(f'{op=}')
if op == "auth:request:reply":
info("Dogehouse: Auth successful")
self.user = User.from_dict(dict(res["p"]))
await execute_listener("on_ready")
elif op == "new-tokens":
info("Dogehouse: Received new authorization tokens")
Expand All @@ -260,22 +271,24 @@ async def execute_command(command_name: str, ctx: Context, *args):
await execute_listener("on_user_fetch", usr)
elif op == "you-joined-as-speaker":
await execute_listener("on_room_join", True)
elif op == "join_room_done":
self.room = Room.from_dict(res["d"]["room"])
elif op == "room:create:reply":
self.room = Room.from_dict(res["p"])
self.room.users.append(self.user)
await self.__send("get_current_room_users", {})
# TODO:
# await self.__send("get_current_room_users", {})
# for user in self.room.users:
# if not isinstance(user, User):
# await self.__fetch("get_user_profile", dict(userId=user.id))

# TODO: Check if joined as speaker
# TODO: Check if joined as speaker
await execute_listener("on_room_join", False)
elif op == "new_user_join_room":
user = User.from_dict(res["d"]["user"])
self.room.users.append(user)
# self.room.users.append(user)
await execute_listener("on_user_join", user)
elif op == "user_left_room":
user = [user for user in self.room.users if user.id == res["d"]["userId"]][0]
user = [user for user in self.room.users if user.id ==
res["d"]["userId"]][0]
self.room.users.remove(user)
await execute_listener("on_user_leave", user)
elif op == "new_chat_msg":
Expand Down Expand Up @@ -348,6 +361,7 @@ async def handle_command(_prefix: str):
async def heartbeat():
debug("Dogehouse: Starting heartbeat")
while self.__active:
debug("ping")
await self.__socket.send("ping")
await asyncio.sleep(heartbeatInterval)

Expand All @@ -373,15 +387,15 @@ async def perform_telemetry():
self.__socket = ws

info("Dogehouse: Attempting to authenticate")
await self.__send('auth', {
await self.__send('auth:request', {
"accessToken": self.__token,
"refreshToken": self.__refresh_token,
"reconnectToVoice": self.__reconnect_voice,
"muted": self.__muted,
"currentRoomId": self.room,
"platform": "dogehouse.py"
"platform": "dogehouse.py",
})
info("Dogehouse: Successfully authenticated")
# TODO: not waiting for successful authentication

event_loop_task = loop.create_task(event_loop())
get_top_rooms_task = loop.create_task(get_top_rooms_loop())
Expand All @@ -396,6 +410,10 @@ async def perform_telemetry():
except ConnectionClosedError as e:
if e.code == 4004:
raise InvalidAccessToken()
else:
print_exc()
except:
print_exc()

def run(self):
"""Establishes a connection to the websocket servers."""
Expand Down Expand Up @@ -505,7 +523,7 @@ async def create_room(self, name: str, description: str = "", *, public=True) ->
public (bool, optional): Whether or not the room should be publicly visible. Defaults to True.
"""
if 2 <= len(name) <= 60:
return await self.__fetch("create_room", dict(name=str(name), description=str(description),
return await self.__fetch("room:create", dict(name=str(name), description=str(description),
privacy="public" if public else "private"))

raise InvalidSize("The `name` property length should be 2-60 characters long.")
Expand Down Expand Up @@ -543,7 +561,7 @@ async def ask_to_speak(self):
Request in the current room to speak.
Raises:
NoConnectionException: Gets raised when no room has been joined yet.
NoConnectionException: Gets raised when no room has been joined yet.
"""
if not self.room:
raise NoConnectionException("No room has been joined yet.")
Expand Down Expand Up @@ -688,48 +706,10 @@ async def wait_for(self, event_name: str, *, timeout: float = 60.0, check: calla
continue
return (*data[0],) if len(data[0]) > 1 else data[0][0]

<<<<<<< HEAD
async def fetch_user(self, argument: str, *, tick=0.5, timeout=60) -> User:
"""Currently only calls the DogeClient.get_user method, will implement user fetching in the future tho."""
# try:
return self.get_user(argument)
# except MemberNotFound:
# op = "get_user_profile"
# fetch_id = str(uuid4())
#
# async def user_fetch_task():
# await self.__send(op, dict(userId=argument), fetch_id=fetch_id)
# self.__fetches[fetch_id] = op
# self.__waiting_for[op] = [*self.__waiting_for[op], fetch_id] if op in self.__waiting_for else [fetch_id]
# passed = 0
# while True:
# print("fetch", passed)
# passed += tick
# await asyncio.sleep(tick)
# if passed > timeout:
# self.__waiting_for[op].remove(fetch_id)
# raise asyncio.TimeoutError(f"wait_for event timed out while fetching user `{argument}`")
# elif fetch_id in self.__waiting_for_fetches:
# data = self.__waiting_for_fetches[fetch_id]
# return data
#
# task = asyncio.ensure_future(user_fetch_task())
# return await task

# TODO: IMPLEMENT USER FETCHING
# async def waiter():
# return await self.wait_for("user_fetch", fetch_arguments=("get_user_profile", dict(userId=value)))

# user = await waiter()

# if user:
# return user
=======
async def fetch_user(self, argument: str) -> Optional[User]:
"""
Goes through the local cache to check if a user can be found.
If no user has been found it will send a request to the server to try and fetch that user.
>>>>>>> 134de9535ed487551d22b01b4fb627c45f260f23
Args:
argument (str): The user argument
Expand Down
4 changes: 0 additions & 4 deletions dogehouse/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
# SOFTWARE.

# Semantic Version
<<<<<<< HEAD
__version__ = "2.1.3"
=======
__version__ = "2.1.4"
>>>>>>> 134de9535ed487551d22b01b4fb627c45f260f23

# The socket url for the dogehouse API
apiUrl = "wss://api.dogehouse.tv/socket"
Expand Down
7 changes: 2 additions & 5 deletions dogehouse/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ def __init__(self, room_id: str, creator_id: str, name: str, description: str, c
creator_id (str): The id of the user who created the room.
name (str): The name of the room.
description (str): The description of the room.
created_at (str): When the room was created.
is_private (bool): Whether or not the room is a private or public room
count (int): The amount of users the room has.
users (List[Union[User, UserPreview]]): A list of users whom reside in this room.
Expand All @@ -227,7 +226,6 @@ def __init__(self, room_id: str, creator_id: str, name: str, description: str, c
self.creator_id: str = creator_id
self.name: str = name
self.description: str = description
self.created_at: datetime = isoparse(created_at)
self.is_private: bool = is_private
self.count: int = count
self.users: List[Union[User, UserPreview]] = users
Expand All @@ -249,9 +247,8 @@ def from_dict(data: dict):
Returns:
Room: A parsed room object which contains the data from the dictionary.
"""
return Room(data["id"], data["creatorId"], data["name"], data["description"], data["inserted_at"],
data["isPrivate"], data["numPeopleInside"],
list(map(UserPreview.from_dict, data["peoplePreviewList"])))
return Room(data["id"], data["creatorId"], data["name"], data["description"], '',
data["isPrivate"], 1, []) #TODO


class Message(Repr):
Expand Down

0 comments on commit eee3d74

Please sign in to comment.