Skip to content

Commit d8844a5

Browse files
authored
Fix request by name for encounter lists (#36)
It's a quick fix for now; this ditto module really needs some code quality improvement later.
1 parent e3610d2 commit d8844a5

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed
Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
import json
22
from pathlib import Path
3-
from typing import Dict
3+
from typing import Dict, Any
44

55
from tqdm import tqdm
66

77
from pokeapi_ditto.common import apply_base_url
88

99

10+
def _is_id(s: str):
11+
try:
12+
int(s)
13+
return True
14+
except ValueError:
15+
return False
16+
17+
18+
def _dump(path: Path, content: Any):
19+
if not path.parent.exists():
20+
path.parent.mkdir(parents=True)
21+
path.write_text(json.dumps(content, sort_keys=True, indent=4))
22+
23+
24+
# TODO: blow all this up and make it good
25+
# this is really bade code and hard to follow
26+
# all this path.parent.parent nonsense is hard to understand
27+
# clone.py is a cleaner model to follow
28+
29+
1030
def do_transform(src_dir: str, dest_dir: str, base_url: str):
1131
src_dir: Path = Path(src_dir)
1232
dest_dir: Path = Path(dest_dir)
@@ -22,16 +42,24 @@ def do_transform(src_dir: str, dest_dir: str, base_url: str):
2242
for src_path in tqdm(list(src_paths)):
2343
content: Dict = json.loads(apply_base_url(src_path.read_text(), base_url))
2444

45+
# all files
2546
dest_path = dest_dir.joinpath(src_path.relative_to(src_dir))
47+
_dump(dest_path, content)
2648

27-
if not dest_path.parent.exists():
28-
dest_path.parent.mkdir(parents=True)
29-
dest_path.write_text(json.dumps(content, sort_keys=True, indent=4))
30-
31-
if "name" in content and "id" in content:
49+
# named resource files
50+
if _is_id(dest_path.parent.name) and "name" in content:
3251
name = content["name"]
3352
dest_path = dest_path.parent.parent.joinpath(name, "index.json")
53+
_dump(dest_path, content)
3454

35-
if not dest_path.parent.exists():
36-
dest_path.parent.mkdir(parents=True)
37-
dest_path.write_text(json.dumps(content, sort_keys=True, indent=4))
55+
# a hack for pokemon/ID/encounters
56+
if (
57+
_is_id(dest_path.parent.parent.name)
58+
and dest_path.parent.name == "encounters"
59+
):
60+
pokemon_path = src_path.parent.parent.joinpath("index.json")
61+
name = json.loads(pokemon_path.read_text())["name"]
62+
dest_path = dest_path.parent.parent.parent.joinpath(
63+
name, "encounters", "index.json"
64+
)
65+
_dump(dest_path, content)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pokeapi-ditto"
3-
version = "0.6.0"
3+
version = "0.6.1"
44
description = "Ditto is a server that serves a static copy of PokeAPI's data."
55
license = "Apache-2.0"
66
authors = ["Sargun Vohra <[email protected]>"]

0 commit comments

Comments
 (0)