Пример #1
0
    def _crafatar_fetch(cls, url: str) -> Response:
        """
        This is the function for fetching the JSON from the Crafatar API.

        Parameters
        ----------
        url: `str`
            The Crafatar URL, whose JSON is supposed to be fetched.

        Returns
        -------
        `t.Optional[dict]`
            The JSON response from the Crafatar API, which is returned.
        """
        with requests.get(f"https://crafatar.com/{url}",
                          timeout=TIMEOUT) as response:
            if response.status_code == 422:
                raise InvalidArgumentError(
                    "Invalid data passed for conversion!")

            try:
                return response
            except Exception:
                raise CrafatarAPIError(
                    "There seems to be some problem with the content type or the API IS down."
                )
Пример #2
0
    def get_guild(self,
                  name: t.Optional[str] = None,
                  uuid: t.Optional[str] = None) -> guild.Guild:
        """
        Get the Info about a Hypixel Guild, Either using Name or UUID.

        Parameters:
            name (t.Optional[str]): The Name of the Guild.
            uuid (t.Optional[str]): The ID Of the guild.

        Returns:
            guild (Guild): The Guild Object with certain Attributes for you to access, and use it.
        """
        if uuid:
            json, success = self._fetch("/guild", {"id": uuid})
        elif name:
            json, success = self._fetch("/guild", {"name": name})
        else:
            raise InvalidArgumentError(
                "Please provide a Named argument of the guild's Name or guild's ID."
            )

        if not success:
            raise HypixelAPIError(
                "The Key given is invalid, or something else has problem.")

        if not json["guild"]:
            raise GuildNotFoundError("Return Value is null")

        return guild.Guild(json["guild"])
Пример #3
0
    def find_guild(
            self,
            guild_name: t.Optional[str] = None,
            player_uuid: t.Optional[str] = None) -> find_guild.FindGuild:
        """
        Finds the Guild By the Guild's Name or using a Player's UUID

        Parameters:
            guild_name (t.Optional[str]) : The name of the Guild
            player_uuid (t.Optional[str]): The UUID of the Player to find his guild.

        Returns:
            guild_id (FindGuild): The ID of the guild being find.
        """
        if guild_name:
            json, success = self._fetch("/findGuild", {"byName": guild_name})
        elif player_uuid:
            json, success = self._fetch("/findGuild", {"byUuid": player_uuid})
        else:
            raise InvalidArgumentError(
                "Please provide a Named argument of the guild's Name or guild's ID."
            )

        if not success:
            raise HypixelAPIError(
                "The Key given is invalid, or something else has problem.")

        return find_guild.FindGuild(json)
Пример #4
0
    def get_player(self,
                   name: t.Optional[str] = None,
                   uuid: t.Optional[str] = None) -> player.Player:
        """
        Get the Info about a Hypixel Player using either his Username or UUID.

        Parameters:
            name (t.Optional[str]): The Optional string value for the Username
            uuid (t.Optional[str]): The Optional string Value to the UUID

        Returns:
            player (Player): The Player Class Object, Which depicts the Player Data Model.
        """
        if name:
            json, success = self._fetch("/player", {"name": name})
        elif uuid:
            json, success = self._fetch("/player", {"uuid": uuid})
        else:
            raise InvalidArgumentError(
                "Please provide a Named argument of the player's username or player's UUID."
            )

        if not success:
            raise HypixelAPIError(
                "The Key given is invalid, or something else has problem.")

        if not json["player"]:
            if name:
                player_model = name
            else:
                player_model = uuid
            raise PlayerNotFoundError("Null Value is returned", player_model)

        return player.Player(json["player"])
Пример #5
0
    async def _crafatar_fetch(cls, url: str) -> t.Any:
        """
        This is the function for fetching the JSON from the Crafatar API.

        Parameters
        ----------
        url: `str`
            The Crafatar URL, whose JSON is supposed to be fetched.

        Returns
        -------
        `t.Optional[dict]`
            The JSON response from the Crafatar API, which is returned.
        """
        session = aiohttp.ClientSession()

        async with session.get(f"https://crafatar.com/{url}",
                               timeout=TIMEOUT) as response:
            if response.status == 422:
                raise InvalidArgumentError(
                    "Invalid data passed for conversion!")

            try:
                return await response.text()
            except Exception:
                raise CrafatarAPIError(
                    "There seems to be some problem with the content type or the API IS down."
                )

        await session.close()
Пример #6
0
    def _filter_name_uuid(name: t.Optional[str] = None, uuid: t.Optional[str] = None) -> str:
        if not name and not uuid:
            raise InvalidArgumentError("Please provide a named argument of the player's username or player's UUID.")

        if name:
            uuid = Converters.username_to_uuid(name)

        return uuid
Пример #7
0
    def _fetch(cls, url: str) -> t.Optional[dict]:
        with requests.get(f"{MOJANG_API}{url}", timeout=TIMEOUT) as response:
            if response.status_code != 200:
                raise InvalidArgumentError(
                    "Invalid Username/UUID passed for conversion!")

            try:
                json = response.json()
                return json
            except Exception as e:
                raise MojangAPIError(
                    "There seems to be some problem with the content type or the API IS down."
                )
Пример #8
0
    def get_friends(self, uuid: t.Optional[str] = None) -> friends.Friends:
        """
        Get the List of Friends of a Hypixel Player and their Info.

        Parameters:
            uuid (t.Optional[str]): The UUID of a Certain Hypixel Player.

        Returns:
            friends (Friends):
                Returns the Friend Data Model, Which has the List of Friends, Each with a List of Attributes.
        """
        if uuid:
            json, success = self._fetch("/friends", {"uuid": uuid})
        else:
            raise InvalidArgumentError(
                "Please provide a Named argument of the player's UUID")

        if not success:
            raise HypixelAPIError(
                "The Key given is invalid, or something else has problem.")

        return friends.Friends(json["records"])