Skip to content

Commit

Permalink
v0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
alexraskin committed Oct 15, 2023
1 parent e896c39 commit fa6a9d2
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 34 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ client = Overwatch()

heros = client.heroes(role="tank")
for hero in heros:
print(hero.name)
print(hero.name)

game_modes = client.gamemodes()
for game_mode in game_modes:
print(game_mode.name)
print(game_mode.description)
player = client.all_player_data(battletag="TeKrop#2217")

print(player.data)
```

## License
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Installation
============
.. code-block:: bash
pip install overwatchpy
pip install overwatchpy
Usage
=====
Expand Down
2 changes: 2 additions & 0 deletions overwatchpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .core import Overwatch

__version__: str = "0.0.4"
4 changes: 2 additions & 2 deletions overwatchpy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
from .const import locale
from .errors import OverwatchAPIError

__version__ = "0.0.4"

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())
logger.setLevel(logging.DEBUG)

__version__: str = "0.0.3"


class EndPoint(Enum):
domain: str = "overfast-api.tekrop.fr"
Expand Down
117 changes: 95 additions & 22 deletions overwatchpy/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import absolute_import

import subprocess
import logging
import re
from typing import Callable, Literal, Optional
Expand Down Expand Up @@ -29,18 +28,19 @@
OverwatchPlayerSearch,
PlayerProfileSummary,
OverwatchPlayerStats,
AllPlayerStats,
)

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())

__version__ = "0.0.1"


class Overwatch(Client):
client = Client()

def __init__(self,) -> None:
def __init__(
self,
) -> None:
"""
Parameters
----------
Expand All @@ -55,6 +55,21 @@ def __init__(self,) -> None:
"""
super().__init__()

def format_battletag(self, battletag: str) -> str:
"""
Formats the battletag
Parameters
----------
battletag : str
The player's battletag
returns
-------
str : str
"""
return str(battletag).replace("#", "-")

def battle_tag_check(self, battletag: str) -> bool:
"""
Checks if the battletag is valid
Expand Down Expand Up @@ -109,10 +124,19 @@ def player_search(
The offset
limit : int
The limit
returns
-------
Callable[[OverwatchPlayerSearch], OverwatchAPIError]
"""
if not self.battle_tag_check(battletag):
if battletag is None:
raise InvalidBattletag("Battletag is required")

if not self.battle_tag_check(updated_battletag):
raise InvalidBattletag("Invalid battletag")

updated_battletag = self.format_battletag(battletag)

if privacy not in ["public", "private"]:
raise InvalidPrivacySettings("Privacy must be either 'public', 'private'")

Expand All @@ -134,7 +158,7 @@ def player_search(
"Order by must be either 'player_id:asc', 'player_id:desc', 'name:asc', 'name:desc', 'privacy:asc', 'privacy:desc'"
)
params = {
"name": battletag,
"name": updated_battletag,
"privacy": privacy,
"platform": platform,
"gamemode": gamemode,
Expand Down Expand Up @@ -173,18 +197,22 @@ def player_summary(
-------
Callable[[dict], OverwatchAPIError]
"""
if self.battle_tag_check(battletag):
raise InvalidBattletag("Invalid battletag")
if battletag is None:
raise InvalidBattletag("Battletag is required")

if self.battle_tag_check(battletag):
raise InvalidBattletag("Invalid battletag")

updated_battletag = self.format_battletag(battletag)

response = self.client.request(
EndPoint.player_summary_url.value.format(battletag=battletag)
EndPoint.player_summary_url.value.format(battletag=updated_battletag)
)
return PlayerProfileSummary(**response)

def all_player_data(
self, battletag: Optional[str] = None
) -> Callable[[dict], OverwatchAPIError]:
) -> Callable[[AllPlayerStats], OverwatchAPIError]:
"""
Returns the player's all data
Expand All @@ -195,16 +223,24 @@ def all_player_data(
returns
-------
Callable[[dict], OverwatchAPIError]
Callable[[AllPlayerStats], OverwatchAPIError]
"""
if self.battle_tag_check(battletag):
raise InvalidBattletag("Invalid battletag")
if battletag is None:
raise InvalidBattletag("Battletag is required")

print(self.battle_tag_check(battletag))

if not self.battle_tag_check(battletag):
raise InvalidBattletag("Invalid battletag")

updated_battletag = self.format_battletag(battletag)

response = self.client.request(
EndPoint.domain.all_player_data_url.value.format(battletag=self.battletag)
EndPoint.domain.all_player_data_url.value.format(
battletag=updated_battletag
)
)
return response # build a class for this response
return AllPlayerStats.parse(data=response)

def player_stats(
self,
Expand All @@ -228,10 +264,14 @@ def player_stats(
-------
Callable[[dict], OverwatchAPIError]
"""
if self.battle_tag_check(battletag):
raise InvalidBattletag("Invalid battletag")
if battletag is None:
raise InvalidBattletag("Battletag is required")

if not self.battle_tag_check(battletag):
raise InvalidBattletag("Invalid battletag")

updated_battletag = self.format_battletag(battletag)

if gamemode is None:
raise InvalidGamemode("Gamemode is required")
if platform is None:
Expand All @@ -241,7 +281,7 @@ def player_stats(
"platform": platform,
}
response = self.client.request(
EndPoint.player_stats_summary_url.value.format(battletag=battletag),
EndPoint.player_stats_summary_url.value.format(battletag=updated_battletag),
params=urlencode(params),
)
return OverwatchPlayerStats(**response)
Expand Down Expand Up @@ -271,10 +311,14 @@ def player_career(
-------
Callable[[dict], OverwatchAPIError]
"""
if self.battle_tag_check(battletag):
raise InvalidBattletag("Invalid battletag")
if battletag is None:
raise InvalidBattletag("Battletag is required")

if not self.battle_tag_check(battletag):
raise InvalidBattletag("Invalid battletag")

updated_battletag = self.format_battletag(battletag)

if gamemode is None:
raise InvalidGamemode("Gamemode is required")
if platform is None:
Expand All @@ -285,10 +329,10 @@ def player_career(
"hero": "all-heroes" if hero is None else hero,
}
return self.client.request(
EndPoint.player_career_url.value.format(battletag=battletag),
EndPoint.player_career_url.value.format(battletag=updated_battletag),
params=urlencode(params),
)

def roles(self) -> Callable[[OverwatchRole], OverwatchAPIError]:
"""
Returns the roles
Expand Down Expand Up @@ -348,3 +392,32 @@ def heroes(
EndPoint.heroes_url.value, params=urlencode(params)
)
return [OverwatchHeros(**response) for response in response]

def hero(
self,
hero: str,
locale: Optional[str] = "en-us",
) -> Callable[[OverwatchHero], OverwatchAPIError]:
"""
Returns the hero
Parameters
----------
hero : str
The hero
locale : str
The locale (default: en-us)
returns
-------
Callable[[OverwatchHero], OverwatchAPIError]
"""
params = {
"locale": locale if locale in self.local else "en-us",
}
if hero is None:
raise InvalidGamemode("Hero is required")
response = self.client.request(
EndPoint.hero_url.value.format(hero=hero), params=urlencode(params)
)
return OverwatchHero(**response)
31 changes: 28 additions & 3 deletions overwatchpy/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class BaseClass:
def __init__(self) -> None:
...


class Ping(BaseClass):
def __init__(self, ping: int) -> None:
super().__init__()
Expand All @@ -13,6 +14,7 @@ def __init__(self, ping: int) -> None:
def __str__(self) -> str:
return f"Ping: {self.ping}"


class OverwatchHeros(BaseClass):
def __init__(self, key: str, name: str, portrait: str, role: str):
super().__init__()
Expand Down Expand Up @@ -108,6 +110,25 @@ def __str__(self) -> str:
return f"Total Results: {self.total}\nResults: {self.results}"


class AllPlayerStats(BaseClass):
def __init__(self, **kwargs) -> None:
super().__init__()
self.data = kwargs

def __str__(self) -> str:
return f"Data: {self.data}"

@classmethod
def parse(cls, data: Dict[str, Any]) -> "AllPlayerStats":
return cls(**data)

def get(self, key: str) -> Any:
return getattr(self, key, None)

def __getitem__(self, key: str) -> Any:
return self.get(key)


class PlayerProfileSummary(BaseClass):
def __init__(
self,
Expand All @@ -118,7 +139,7 @@ def __init__(
endorsement: Dict[str, str],
competitive: Dict[str, Dict[str, Any]],
privacy: str,
):
):
super().__init__()
self.username: str = username
self.avatar: str = avatar
Expand All @@ -136,8 +157,12 @@ class OverwatchPlayerStats(BaseClass):
def __init__(self, data: Dict[str, Any]) -> None:
super().__init__()
self.general = self.GeneralStats(data["general"])
self.heroes = {hero: self.HeroStats(stats) for hero, stats in data["heroes"].items()}
self.roles = {role: self.RoleStats(stats) for role, stats in data["roles"].items()}
self.heroes = {
hero: self.HeroStats(stats) for hero, stats in data["heroes"].items()
}
self.roles = {
role: self.RoleStats(stats) for role, stats in data["roles"].items()
}

class OverwatchGeneralStats:
def __init__(self, data: Dict[str, Any]) -> None:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "overwatchpy"
version = "0.0.3"
version = "0.0.4"
description = "Python wrapper for Overwatch API"
authors = ["alexraskin <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit fa6a9d2

Please sign in to comment.