Skip to content

Commit

Permalink
added Group fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
phishontop authored Dec 1, 2022
1 parent 24eca34 commit 47ed15d
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 20 deletions.
16 changes: 6 additions & 10 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import roblox
import threading

client = roblox.Client()
for i in range(2000):
try:
user = client.fetch_user(i+1)
print(user.name)

except roblox.models.ErrorModel.InvalidUser:
print(f"{i+1} ID is not found")

except roblox.models.ErrorModel.RateLimit:
print(f"Ratelimited")

user = client.fetch_user(1)
print(user.name)
group = client.fetch_group(1)
print(group.name)
10 changes: 6 additions & 4 deletions roblox/client/Client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from ..services import UserService

from ..services import GroupService

class Client:

def __init__(self) -> None:
print(f"Client object has been created")
pass

def fetch_user(self, roblox_id: int):
print(f"Client fetching {roblox_id}")
return UserService.fetch_user(roblox_id=roblox_id)
return UserService.fetch_user(roblox_id=roblox_id)

def fetch_group(self, group_id: int):
return GroupService.fetch_group(group_id=group_id)
5 changes: 2 additions & 3 deletions roblox/http/HttpClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def port(self) -> int:
raise Exception(f"URL must start with http or https not {self.protocol}")

def new_connection(self):
print(f"HttpClient started new connection {self.host}")
sock = socket.socket()
sock.connect((self.host, self.port))

Expand All @@ -63,7 +62,7 @@ def new_connection(self):

return sock

def get_payload(self):
def get_payload(self) -> str:
payload_header = ""
for header_type, header_value in self.headers.items():
payload_header += f"{header_type}: {header_value}\r\n"
Expand All @@ -75,7 +74,7 @@ def get_payload(self):
content_length = len(json.dumps(self.data))
return f"{self.method} {self.path} HTTP/1.1\r\nHost: {self.host}\r\n{payload_header}Content-Length: {content_length}\r\nContent-Type: application/json\r\n\r\n{self.data}".encode()

def get_response(self):
def get_response(self) -> Response:
data = self.get_payload()
if self.sock is None:
self.sock = self.new_connection()
Expand Down
16 changes: 16 additions & 0 deletions roblox/managers/GroupFactory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from ..models.GroupModel import GroupModel
from ..models.ErrorModel import InvalidGroup, RateLimit


class GroupFactory:

@staticmethod
def create(response):
if response.status_code == 400:
raise InvalidGroup("Group ID is invalid")

elif response.status_code == 429:
raise RateLimit("group.roblox.com has ratelimited requests")

return GroupModel(response.json())

1 change: 0 additions & 1 deletion roblox/managers/UserFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class UserFactory:
"""
@staticmethod
def create(response):
print(response.json(), response.status_code)
if response.status_code == 404:
raise InvalidUser("User ID is invalid")

Expand Down
7 changes: 7 additions & 0 deletions roblox/models/ErrorModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ class RateLimit(Exception):
Raises Exception when request becomes ratelimited.
"""
pass


class InvalidGroup(Exception):
"""
Raises Exception when the group is invalid.
"""
pass
10 changes: 10 additions & 0 deletions roblox/models/GroupModel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


class GroupModel:

def __init__(self, data: dict) -> None:
#super().__init__(roblox_id=data["id"])
self.name = data["name"]
self.id = data["id"]
self.description = data["description"]
self.member_count = data["memberCount"]
1 change: 1 addition & 0 deletions roblox/models/UserModel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ..managers.UserManager import UserManager


class UserModel(UserManager):

def __init__(self, data: dict) -> None:
Expand Down
11 changes: 11 additions & 0 deletions roblox/services/GroupService.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ..http import HttpService
from ..managers.GroupFactory import GroupFactory


class GroupService:

@staticmethod
def fetch_group(group_id: int):
http_client = HttpService().client
response = http_client.send_request(method="GET", link=f"https://groups.roblox.com/v1/groups/{group_id}")
return GroupFactory.create(response)
1 change: 0 additions & 1 deletion roblox/services/UserService.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class UserService:

@staticmethod
def fetch_user(roblox_id: int):
print(f"Manager User: fetch_user({roblox_id})")
http_client = HttpService().client
response = http_client.send_request(method="GET", link=f"https://users.roblox.com/v1/users/{roblox_id}")
return UserFactory.create(response)
3 changes: 2 additions & 1 deletion roblox/services/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .UserService import UserService
from .UserService import UserService
from .GroupService import GroupService

0 comments on commit 47ed15d

Please sign in to comment.