Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ comp = compendium()
print(comp.get_all()) # get all entries
print(comp.get_entry("silver_lynel")) # get a specific entry with it's name
print(comp.get_entry(1)) # get a specific entry with it's ID
print(comp).get_category("monsters")) # get all entries in a category
print(comp.get_category("monsters")) # get all entries in a category
comp.get_image("silver_lynel").download() # download entry image
```

Expand Down
54 changes: 46 additions & 8 deletions pyrule_compendium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ class compendium(object):
* `default_timeout`: Default seconds to wait for response for all API calling functions until raising `requests.exceptions.ReadTimeout`
- default: `None` (no timeout)
- type: integer, float, tuple (for connect and read timeouts)
- notes: If an API calling function has a parameter `timeout`, it will overide this
- notes: If an API calling function has a parameter `timeout`, it will override this
"""

def __init__(self, base_url: str="https://botw-compendium.herokuapp.com/api/v2", default_timeout: Union[int, float, None]=None):
def __init__(self, base_url: str="https://botw-compendium.herokuapp.com/api/v2", default_timeout: Union[int, float, None]=None, master_mode: bool = False):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think master_mode should be passed into the functions rather than to the class, because we do not want to create another object for master_mode data.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that having master_mode passed when the class is initialized would remove the need for a redundant parameter in every function of the class.

self.api: api = api(base_url)
self.default_timeout = default_timeout
self.master_mode = master_mode
if self.master_mode: self.master_api: api = api("https://botw-compendium.herokuapp.com/api/v2/master_mode")

def get_entry(self, entry: types.entry, timeout: types.timeout=None) -> dict:
"""
Expand All @@ -42,10 +44,11 @@ def get_entry(self, entry: types.entry, timeout: types.timeout=None) -> dict:
timeout = self.default_timeout

res: dict = self.api.request(f"/entry/{entry}", timeout)
if res == {}:
raise exceptions.NoEntryError(entry)
if not res:
res = self.master_api.request(f"/entry/{entry}", timeout)

return res
if res: return res
raise exceptions.NoEntryError(entry)

def get_category(self, category: str, timeout: types.timeout=None) -> Union[dict, list]:
"""
Expand All @@ -70,7 +73,10 @@ def get_category(self, category: str, timeout: types.timeout=None) -> Union[dict
if category not in ["creatures", "equipment", "materials", "monsters", "treasure"]:
raise exceptions.NoCategoryError(category)

return self.api.request(f"/category/{category}", timeout)
if self.master_mode and category == "monsters":
return self.api.request("/category/monsters", timeout) + api_req(self.master_api.base_url, timeout)
else:
return self.api.request(f"/category/{category}", timeout)

def get_all(self, timeout: types.timeout=None) -> Union[dict, list]:
"""
Expand All @@ -88,7 +94,12 @@ def get_all(self, timeout: types.timeout=None) -> Union[dict, list]:
if not timeout:
timeout = self.default_timeout

return api_req(self.api.base_url, timeout)
if self.master_mode:
res: dict = api_req(self.api.base_url, timeout)
res["monsters"] += api_req(self.master_api.base_url, timeout)
return res
else:
return api_req(self.api.base_url, timeout)

def get_image(self, entry: types.entry) -> objects.entry_image:
"""
Expand All @@ -102,4 +113,31 @@ def get_image(self, entry: types.entry) -> objects.entry_image:
- type: `objects.entry_image`
"""

return objects.entry_image(self.get_entry(entry), self.api)
if self._is_master_mode_entry(entry): return objects.entry_image(self.get_entry(entry), self.master_api)
else: return objects.entry_image(self.get_entry(entry), self.api)

def _is_master_mode_entry(self, entry: types.entry, timeout: types.timeout=None):
"""
Determines if an entry is from master mode or not.

Parameters:
* `entry`: The ID or name of the entry.
- type: str, int
* `timeout`: Seconds to wait for response until raising `requests.exceptions.ReadTimeout`
- default: `compendium.default_timeout`
- type: int, float, tuple (for connect and read timeouts)

Returns: Whether an entry is from master mode or not
- type: `objects.entry_image`
"""

if not timeout:
timeout = self.default_timeout

res: dict = self.api.request(f"/entry/{entry}", timeout)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I plan on adding a dlc field to entry data in v3. Until then, is there a better way of determining if it is master_mode or not without two requests?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than using local data, I don’t see any way to determine if an entry is from master_mode without two requests.

if res: return False

res = self.master_api.request(f"/entry/{entry}", timeout)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too many requests?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I don’t see a way to minimize the number of requests we have here other than using local data.

if res: return True

else: raise exceptions.NoEntryError(entry)
13 changes: 10 additions & 3 deletions pyrule_compendium/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from typing import Union, Any
from requests import get


class types:
entry = Union[int, str]
timeout = Union[int, float, None]

def api_req(url: str, timeout: types.timeout=None) -> Union[list, dict]: return get(url, timeout=timeout).json()["data"]

class api():
def api_req(url: str, timeout: types.timeout = None) -> Union[list, dict]: return get(url, timeout=timeout).json()[
"data"]


class api:
def __init__(self, base_url: str): self.base_url = base_url

def endpoint(self, target_endpoint: str) -> str: return f"{self.base_url}{target_endpoint}"
def request(self, endpoint: str, timeout: types.timeout=None) -> Any: return api_req(f"{self.endpoint(endpoint)}", timeout)

def request(self, endpoint: str, timeout: types.timeout = None) -> Any: return api_req(f"{self.endpoint(endpoint)}",
timeout)