Skip to content

Commit

Permalink
cache location_area_encounters
Browse files Browse the repository at this point in the history
  • Loading branch information
GregHilmes committed Jul 19, 2018
1 parent 6356645 commit 5752b67
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
23 changes: 17 additions & 6 deletions pokebase/api.py
Expand Up @@ -2,8 +2,8 @@

import requests

from .common import BASE_URL, ENDPOINTS, api_url_build, sprite_url_build
from .cache import save, load, save_sprite, load_sprite, get_sprite_path
from .common import BASE_URL, ENDPOINTS, api_url_build, sprite_url_build, subresource_url_build
from .cache import save, load, save_sprite, load_sprite, get_sprite_path, save_subresource, load_subresource


def _call_api(endpoint, resource_id=None):
Expand Down Expand Up @@ -74,10 +74,21 @@ def get_sprite(sprite_type, sprite_id, **kwargs):
return data


# "location_area_encounters": "/api/v2/pokemon/12/encounters",
def get_encounters(encouter_url):
def get_encounters(endpoint, resource_id, subresource, **kwargs):

response = requests.get('http://pokeapi.co' + encouter_url)
if not kwargs.get('force_lookup', False):
try:
data = load_subresource(endpoint, resource_id, subresource)
return data
except KeyError:
pass

url = subresource_url_build(endpoint, resource_id, subresource)

response = requests.get(url)
response.raise_for_status()

return response.json()
data = response.json()
save_subresource(data, endpoint, resource_id, subresource)

return data
32 changes: 31 additions & 1 deletion pokebase/cache.py
Expand Up @@ -3,7 +3,7 @@
import os
import shelve

from .common import cache_uri_build, sprite_filepath_build
from .common import cache_uri_build, sprite_filepath_build, subresource_cache_uri_build

# Cache locations will be set at the end of this file.
CACHE_DIR = None
Expand Down Expand Up @@ -143,3 +143,33 @@ def set_cache(new_path=None):


CACHE_DIR, API_CACHE, SPRITE_CACHE = set_cache()


def save_subresource(data, endpoint, resource_id, subresource):

uri = subresource_cache_uri_build(endpoint, resource_id, subresource)

with shelve.open(API_CACHE) as cache:
cache[uri] = data

return None


def load_subresource(endpoint, resource_id, subresource):

uri = subresource_cache_uri_build(endpoint, resource_id, subresource)

print('getting cached')

try:
with shelve.open(API_CACHE) as cache:
data = cache[uri]
except OSError as error:
if error.errno == 11:
# Cache open by another person/program
# print('Cache unavailable, skipping load')
raise KeyError('Cache could not be opened.')
else:
raise

return data
12 changes: 12 additions & 0 deletions pokebase/common.py
Expand Up @@ -65,6 +65,18 @@ def sprite_url_build(sprite_type, sprite_id, **kwargs):
return url


def subresource_url_build(endpoint, resource_id, subresource):

url = '/'.join([BASE_URL, endpoint, resource_id, subresource])
return url


def subresource_cache_uri_build(endpoint, resource_id, subresource):

uri = '/'.join([endpoint, resource_id, subresource])
return uri


def sprite_filepath_build(sprite_type, sprite_id, **kwargs):
"""returns the filepath of the sprite *relative to SPRITE_CACHE*"""

Expand Down
4 changes: 3 additions & 1 deletion pokebase/interface.py
Expand Up @@ -140,7 +140,9 @@ def _load(self):

if key == 'location_area_encounters' \
and self.endpoint == 'pokemon':
encounters = get_encounters(val)

params = val.split('/')[-3:len(val)]
encounters = get_encounters(*params)
data[key] = [_make_obj(enc) for enc in encounters]
continue

Expand Down

0 comments on commit 5752b67

Please sign in to comment.