Код: Выделить всё
json_urls = {
"championsleague": "https://www.sofascore.com/tournament/football/europe/uefa-champions-league/7#id:76953",
}
def download_and_save_logo(team_name, logo_url, headers, league):
# Check for generic "no-logo" URLs and placeholders
if not logo_url or logo_url.endswith("/blank.gif") or 'placeholder' in logo_url or logo_url.endswith("/no-logo.gif"):
return False
team_filename = sanitize_team_name(team_name)
# The final target path is ALWAYS .png (what the E2 interface expects)
filename_png = resolveFilename(SCOPE_PLUGINS,
"Extensions/FootOnSat/assets/standings/{}.png".format(team_filename))
# Check if PNG version exists
if os.path.exists(filename_png):
return True
#logdata("Logos", "Downloading logo for '%s' from: %s" % (team_name, logo_url))
# Determine file extension from URL (used for temp filename)
ext = ".gif" if logo_url.lower().endswith(".gif") else (".png" if logo_url.lower().endswith(".png") else ".jpg")
#logdata("ext", "Downloading logo for '%s'" % ext)
# Temporary file path (using the actual downloaded extension)
temp_file = os.path.join("/tmp", "{}{}".format(team_filename, ext))
#logdata("temp_file", "Downloading logo for '%s' from: %s" % (team_filename, ext))
try:
# --- The network request uses the fixed 'headers' ---
# SofaScore API requires standard desktop headers
logo_headers = headers.copy()
logo_headers["Accept"] = "image/avif,image/webp,image/apng,image/svg+xml,image/*;q=0.8"
if not PY3:
try:
logo_headers["Referer"] = "https://www.sofascore.com/"
r = requests.get(logo_url, headers=logo_headers, timeout=3, verify=False)
r.raise_for_status()
data = r.content
# Ensure it’s actually image data
if not (data.startswith(b'\x89PNG') or data.startswith(b'\xff\xd8') or data.startswith(b'GIF')):
#logdata("Logos", "Invalid PNG data from %s (probably 403 HTML)" % logo_url)
return False
with open(temp_file, "wb") as f:
f.write(data)
except Exception as e:
#logdata("Logos", "Requests fetch failed for %s: %s" % (logo_url, str(e)))
trace_error()
return False
else:
req = compat_Request(logo_url, headers=logo_headers)
resp = compat_urlopen(req, timeout=3)
# Save the raw file content to the temporary location
with open(temp_file, "wb") as f:
f.write(resp.read())
success = False
if ext == ".png":
# If already PNG, just copy the file from /tmp to the final .png path
shutil.copyfile(temp_file, filename_png)
#logdata("Logos", "Successfully saved PNG logo for '%s'." % team_name)
success = True
elif PIL_AVAILABLE:
# --- PIL CONVERSION LOGIC ---
try:
img = Image.open(temp_file)
#logdata("img", "Downloading logo for '%s'" % img)
# Handle potential transparent GIF/JPG by converting to RGBA
if img.mode not in ('RGB', 'RGBA'):
img = img.convert('RGBA')
img.save(filename_png, 'PNG')
#logdata("Logos", "Converted and saved %s logo for '%s' to PNG via PIL." % (ext[1:].upper(), team_name))
success = True
except Exception as e:
#logdata("Logos", "PIL conversion FAILED for %s: %s" % (team_name, str(e)))
trace_error() # Include trace for better debugging
# Fallback to simple copy if PIL fails (e.g., corrupted file)
shutil.copyfile(temp_file, filename_png)
success = True # Still logged as found
else:
# --- NO PIL FALLBACK (Will cause display error) ---
#logdata("Logos", "WARNING: PIL not available, saving raw %s data as PNG file for '%s'." % (ext[1:].upper(), team_name))
shutil.copyfile(temp_file, filename_png)
success = True
# Clean up the temporary file
if os.path.exists(temp_file):
os.remove(temp_file)
return success
except Exception as e:
#logdata("Logos", "Failed to download/process logo for %s: %s" % (team_name, str(e)))
trace_error()
return False
finally:
# Ensure cleanup regardless of success/failure
if os.path.exists(temp_file):
os.remove(temp_file)
Код: Выделить всё
Logos : Downloading logo for 'Paris Saint-Germain' from: https://api.sofascore.com/api/v1/team/1644/image
ext : Downloading logo for '.jpg'
temp_file : Downloading logo for 'Paris_Saint-Germain' from: .jpg
img : Downloading logo for ''
Код: Выделить всё
Logos : Downloading logo for 'Paris Saint-Germain' from: https://api.sofascore.com/api/v1/team/1644/image
ext : Downloading logo for '.jpg'
temp_file : Downloading logo for 'Paris_Saint-Germain' from: .jpg
Logos : Invalid PNG data from https://api.sofascore.com/api/v1/team/1644/image (probably 403 HTML)
Подробнее здесь: https://stackoverflow.com/questions/797 ... ebp-to-png