Skip to content

Commit

Permalink
Fixed the issue with pulling the frog list resulting in occasional Ke…
Browse files Browse the repository at this point in the history
…yErrors.
  • Loading branch information
LevBernstein committed May 7, 2024
1 parent 9d59997 commit 9a60924
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions misc.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Beardless Bot miscellaneous methods

import re
import json
from datetime import datetime
from logging import error
from json import loads
from random import choice, randint
from typing import List, Optional, Tuple, Union
from urllib.parse import quote_plus
Expand Down Expand Up @@ -214,7 +213,7 @@ def animal(animalType: str, breed: Optional[str] = None) -> str:
return f"https://placebear.com/{randint(200, 400)}/{randint(200,400)}"

elif animalType == "frog":
frog = choice(frogList)
frog = choice(frogList)["name"]
return (
f"https://raw.githubusercontent.com/a9-i/frog/main/ImgSetOpt/{frog}"
)
Expand All @@ -226,21 +225,18 @@ def animal(animalType: str, breed: Optional[str] = None) -> str:
raise Exception(str(r) + ": " + animalType)


# Load the list of frog images only once. Might fail; retry up to 10 times.
# Amortize the cost of pulling the frog images by making one initial call.
# Two possible layouts, one when formatting fails.
def getFrogList() -> List[str]:
for i in range(10):
r = requests.get("https://github.com/a9-i/frog/tree/main/ImgSetOpt")
soup = BeautifulSoup(r.content.decode("utf-8"), "html.parser")
try:
return [
i["name"] for i in (
json.loads(
soup.findAll("script")[-1].text
)["payload"]["tree"]["items"]
)
]
except KeyError as e:
error("getFrogList attempt" + str(i) + " failed: " + str(e))
r = requests.get("https://github.com/a9-i/frog/tree/main/ImgSetOpt")
soup = BeautifulSoup(r.content.decode("utf-8"), "html.parser")
try:
j = loads(soup.findAll("script")[-1].text)["payload"]
except KeyError:
j = loads(
soup.findAll("script")[-2].text.replace("\\", "\\\\")
)["payload"]
return j["tree"]["items"]


frogList = getFrogList()
Expand Down

0 comments on commit 9a60924

Please sign in to comment.