multiple tries to get json from remote
This commit is contained in:
20
generate.py
20
generate.py
@@ -1,5 +1,6 @@
|
|||||||
from PIL import Image, ImageDraw, ImageFont
|
from PIL import Image, ImageDraw, ImageFont
|
||||||
import requests, os
|
import requests, os
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
DNDB_BASE_URL = "https://www.dndbeyond.com/character/"
|
DNDB_BASE_URL = "https://www.dndbeyond.com/character/"
|
||||||
DNDB_CHARS = [
|
DNDB_CHARS = [
|
||||||
@@ -137,14 +138,19 @@ def generate_main(char: dict):
|
|||||||
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0',
|
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0',
|
||||||
'Referer': 'https://www.dndbeyond.com/'
|
'Referer': 'https://www.dndbeyond.com/'
|
||||||
}
|
}
|
||||||
response = requests.get(f"{DNDB_BASE_URL}/{char['characterId']}/json", headers=headers)
|
|
||||||
|
|
||||||
if response.status_code == 200:
|
for i in range(3):
|
||||||
data = response.json()
|
response = requests.get(f"{DNDB_BASE_URL}/{char['characterId']}/json", headers=headers)
|
||||||
print(data["stats"])
|
if response.status_code == 200:
|
||||||
|
data = response.json()
|
||||||
|
print(data["stats"])
|
||||||
|
break
|
||||||
|
sleep(1)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("Error: Failed to retrieve data from URL.")
|
print("Error: Failed to retrieve data from URL.")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Load the background image
|
# Load the background image
|
||||||
background = Image.open("parts/background-main.png")
|
background = Image.open("parts/background-main.png")
|
||||||
|
|
||||||
@@ -200,6 +206,6 @@ def generate_main(char: dict):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
# Save the resulting image
|
# Save the resulting image
|
||||||
generate_main(DNDB_CHARS[0])
|
# generate_main(DNDB_CHARS[0])
|
||||||
# for char in DNDB_CHARS:
|
for char in DNDB_CHARS:
|
||||||
# generate_main(char)
|
generate_main(char)
|
||||||
|
|||||||
Reference in New Issue
Block a user