Remove dead code and auto-reformat

This commit is contained in:
2022-10-28 13:47:27 -04:00
parent 7d9209d52e
commit 808ab57ea9
3 changed files with 51 additions and 30 deletions

View File

@@ -23,7 +23,6 @@ def create_pokedex_chapter(pokemon: List[Pokemon]) -> epub.EpubHtml:
content = ["<h1>Pokedex</h1>"]
for p in pokemon:
p_id = p.name.lower().replace(". ", "")
content.append(f'<h2 id="{POKEMON_ID_PREFIX}{p.link_id}">{p.name}</h2>')
content.append(
f' <p><img alt="[Pokemon {p.name}]" src="../{p.img_filename}"/><br/></p>'
@@ -60,32 +59,44 @@ def patch_chapter(chapter: epub.EpubHtml, pokemon_lookup: Dict[str, Pokemon]):
link = pokemon_name_to_link(p, word)
result.append(link)
result.append([])
elif word == "Mr" and index + 2 < len(chunks) and \
chunks[index + 1] == ". " and chunks[index + 2] == "Mime":
elif (
word == "Mr"
and index + 2 < len(chunks)
and chunks[index + 1] == ". "
and chunks[index + 2] == "Mime"
):
# Handle "Mr. Mime" which is split into ["Mr", ". ", "Mime"]
p = pokemon_lookup["mr. mime"]
p.appears_in_book = True
name = "".join(chunks[index:index + 3])
name = "".join(chunks[index : index + 3])
link = pokemon_name_to_link(p, name)
index += 2
result.append(link)
result.append([])
elif word.lower() == "farfetch" and index + 2 < len(chunks) and \
chunks[index + 1] == "" and chunks[index + 2] == "d":
elif (
word.lower() == "farfetch"
and index + 2 < len(chunks)
and chunks[index + 1] == ""
and chunks[index + 2] == "d"
):
# Handle "farfetch'ed"
p = pokemon_lookup["farfetch'd"]
p.appears_in_book = True
name = "".join(chunks[index:index + 3])
name = "".join(chunks[index : index + 3])
link = pokemon_name_to_link(p, name)
index += 2
result.append(link)
result.append([])
elif word.lower() == "sirfetch" and index + 2 < len(chunks) and \
chunks[index + 1] == "" and chunks[index + 2] == "d":
elif (
word.lower() == "sirfetch"
and index + 2 < len(chunks)
and chunks[index + 1] == ""
and chunks[index + 2] == "d"
):
# Handle "sirfetch'ed"
p = pokemon_lookup["sirfetch'd"]
p.appears_in_book = True
name = "".join(chunks[index:index + 3])
name = "".join(chunks[index : index + 3])
link = pokemon_name_to_link(p, name)
index += 2
result.append(link)

View File

@@ -57,7 +57,9 @@ def download_national_index_html(national_index_filename: str):
def get_pokemon_table_row_soups(national_index_filename: str) -> List[BeautifulSoup]:
with open(national_index_filename, "r") as r:
soup = BeautifulSoup(r, "html.parser")
pokemon_list_soup = soup.find(id="List_of_Pokémon_by_National_Pokédex_number").parent
pokemon_list_soup = soup.find(
id="List_of_Pokémon_by_National_Pokédex_number"
).parent
generation_soups = pokemon_list_soup.find_next_siblings("h3")
table_row_soups = []
for generation_soup in generation_soups:
@@ -81,8 +83,7 @@ def extract_pokemon_from_table_row(table_row_soup: BeautifulSoup) -> Pokemon:
index = table_row_soup.find_next("td").next_sibling.next_sibling.text.strip()
html_url = (
BULBAPEDIA_BASE_URL
+ table_row_soup.find_next("th").next_element.attrs["href"]
BULBAPEDIA_BASE_URL + table_row_soup.find_next("th").next_element.attrs["href"]
)
img_url = table_row_soup.find("img").attrs["src"]
html_filename = os.path.join(POKEMON_CACHE_DIRECTORY, name.lower() + ".html")
@@ -105,7 +106,7 @@ def get_pokemon() -> List[Pokemon]:
os.mkdir(POKEMON_CACHE_DIRECTORY)
national_index_filename = os.path.join(POKEMON_CACHE_DIRECTORY, "pokedex.html")
download_national_index_html(national_index_filename)
table_row_soups = get_pokemon_table_row_soups(national_index_filename)
table_row_soups = get_pokemon_table_row_soups(national_index_filename)
pokemon = []
for table_row_soup in track(table_row_soups, description="Download Pokemon"):
@@ -152,7 +153,10 @@ def extend_pokemon(p: Pokemon):
if not os.path.isfile(p.img_filename):
img_url = (
content_soup.find("table").find_next_sibling("table").find("img").attrs["src"]
content_soup.find("table")
.find_next_sibling("table")
.find("img")
.attrs["src"]
)
img_url = img_url.replace("//", "https://")
p.img_url = img_url

View File

@@ -1,4 +1,4 @@
import pokemon
import pokemon
import os
import filecmp
@@ -8,31 +8,37 @@ def test_download_national_index_html(tmp_path):
pokemon.download_national_index_html(pokemon_html)
assert os.path.getsize(pokemon_html) > 500000
def test_get_pokemon_table_row_soups():
national_index = "test/test_pokedex.html"
row_soups = pokemon.get_pokemon_table_row_soups(national_index)
row_soups = pokemon.get_pokemon_table_row_soups(national_index)
assert len(row_soups) == 994
def test_extract_pokemon_from_table_row(tmp_path):
national_index = "test/test_pokedex.html"
pokemon.POKEMON_CACHE_DIRECTORY = tmp_path
row_soups = pokemon.get_pokemon_table_row_soups(national_index)
row_soups = pokemon.get_pokemon_table_row_soups(national_index)
p = pokemon.extract_pokemon_from_table_row(row_soups[42])
assert p.name == 'Vulpix'
assert p.link_id == 'vulpix'
assert p.index == '#037'
assert p.html_url == 'https://bulbapedia.bulbagarden.net/wiki/Vulpix_(Pok%C3%A9mon)'
assert p.img_url == '//archives.bulbagarden.net/media/upload/thumb/3/35/037Vulpix-Alola.png/70px-037Vulpix-Alola.png'
assert p.img_filename.endswith('vulpix.png')
assert p.json_filename.endswith('vulpix.json')
assert p.description == ''
assert p.name == "Vulpix"
assert p.link_id == "vulpix"
assert p.index == "#037"
assert p.html_url == "https://bulbapedia.bulbagarden.net/wiki/Vulpix_(Pok%C3%A9mon)"
assert (
p.img_url
== "//archives.bulbagarden.net/media/upload/thumb/3/35/037Vulpix-Alola.png/70px-037Vulpix-Alola.png"
)
assert p.img_filename.endswith("vulpix.png")
assert p.json_filename.endswith("vulpix.json")
assert p.description == ""
assert p.appears_in_book == False
def test_extend_pokemon(tmp_path):
national_index = "test/test_pokedex.html"
row_soups = pokemon.get_pokemon_table_row_soups(national_index)
row_soups = pokemon.get_pokemon_table_row_soups(national_index)
p = pokemon.extract_pokemon_from_table_row(row_soups[42])
p.img_filename = tmp_path / 'vulpix.png'
p.img_filename = tmp_path / "vulpix.png"
pokemon.extend_pokemon(p)
assert filecmp.cmp(p.img_filename, 'test/test_vulpix.png')
assert p.description.startswith("Vulpix (Japanese: \u30ed\u30b3\u30f3 Rokon)")
assert filecmp.cmp(p.img_filename, "test/test_vulpix.png")
assert p.description.startswith("Vulpix (Japanese: \u30ed\u30b3\u30f3 Rokon)")