Is it possible to add a default image to this code when the result returned for “avatarUrl” from JSON is null?
@staticmethod
def score_to_mod(score):
return floor((score - 10) / 2)
def __init__(self, character_id):
self.character_id = character_id
self.character_data = self.get_character_data()
self.character_image = self.character_data.get("decorations", {}).get("avatarUrl", "")[:-51]
#print(self.character_url)
def update(self):
self.character_data = self.get_character_data()
def get_character_data(self):
req = requests.get(f"https://character-service.dndbeyond.com/character/v5/character/{self.character_id}")
#print("hello")
if req.status_code != 200:
return None
try:
j = req.json()
if not "success" in j or not "data" in j:
return None
return j["data"]
except ValueError:
return None
Here is the end of the script where the data is used by my main script
return {
"max": max_hp,
"current": max_hp - self.character_data["removedHitPoints"],
"temp": self.character_data["temporaryHitPoints"],
"image": self.character_image,
"char_id": self.character_id
}
I tried adding an “if” statement but the result was unfavorable.
I tried adding the following code to the INIT, and when that didn’t work, I tried adding it to the “get_character_data”
if self.character_image is None:
self.character_image = "https://www.dndbeyond.com/avatars/39102/30/1581111423-110222058.jpeg"
2