From 1b0c4885b83ad2548ffbd49804b11b9c599239ea Mon Sep 17 00:00:00 2001 From: fab Date: Mon, 16 Dec 2024 10:29:50 +0100 Subject: [PATCH] Update check_pagespeed_performances.py --- checks/check_pagespeed_performances.py | 73 +++++++++++++------------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/checks/check_pagespeed_performances.py b/checks/check_pagespeed_performances.py index ba1b027..cfad886 100644 --- a/checks/check_pagespeed_performances.py +++ b/checks/check_pagespeed_performances.py @@ -1,49 +1,48 @@ import requests -import json -from requests.exceptions import RequestException, Timeout, HTTPError +import logging +from requests.exceptions import RequestException, HTTPError -def check_pagespeed_performances(website: str, api_key: str) -> int: +logger = logging.getLogger(__name__) + +def check_pagespeed_performances(website: str, api_key: str = None) -> str: """ - Check the PageSpeed performance score of the given website using Google's PageSpeed Insights API. + Checks the PageSpeed Insights performance score for a website. Args: website (str): The URL of the website to be checked. - api_key (str): The API key for accessing the PageSpeed Insights API. + api_key (str, optional): The Google PageSpeed Insights API key. Defaults to None. Returns: - int: The PageSpeed performance score (0-100), or 0 in case of any errors. + str: + - An integer representing the PageSpeed score if successful. + - "⚪" if any errors occur during the check or if no API key was provided. """ - # Construct the PageSpeed API URL - pagespeed_url = f"https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://{website}&key={api_key}" + if not api_key: + logger.error("No API key provided for PageSpeed check.") + return "⚪" - headers = { - 'User-Agent': 'PageSpeedChecker/1.0' - } - try: - # Make a request to the PageSpeed Insights API - pagespeed_response = requests.get(pagespeed_url, headers=headers, timeout=10) - pagespeed_response.raise_for_status() # Raise an exception for HTTP errors - - # Parse the API response JSON - pagespeed_data = pagespeed_response.json() - - # Extract the performance score - pagespeed_score = pagespeed_data["lighthouseResult"]["categories"]["performance"]["score"] * 100 - - # Return the rounded score as an integer - print(f"PageSpeed score for {website} is {round(pagespeed_score)}.") - return round(pagespeed_score) - - except (Timeout, HTTPError) as e: - print(f"Timeout or HTTP error occurred while fetching PageSpeed data for {website}: {e}") - return 0 - except RequestException as e: - print(f"Request-related error occurred while fetching PageSpeed data for {website}: {e}") - return 0 - except KeyError: - print(f"Error parsing PageSpeed data for {website}: The expected data structure was not found.") - return 0 + pagespeed_url = f"https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={website}&key={api_key}" + response = requests.get(pagespeed_url, timeout=20) + response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx). + data = response.json() + + score = data.get("lighthouseResult", {}).get("categories", {}).get("performance", {}).get("score") + if score is not None: + logger.info(f"PageSpeed score for {website} is {int(score * 100)}.") + return str(int(score * 100)) + logger.warning(f"PageSpeed score not found for {website}.") + return "⚪" + + except HTTPError as http_err: + logger.error(f"Timeout or HTTP error occurred while fetching PageSpeed data for {website}: {http_err}") + return "⚪" # Grey: API error. + except RequestException as req_err: + logger.error(f"Timeout or HTTP error occurred while fetching PageSpeed data for {website}: {req_err}") + return "⚪" # Grey: Request error. + except ValueError as json_err: + logger.error(f"JSON parsing error occurred while fetching PageSpeed data for {website}: {json_err}") + return "⚪" # Grey: JSON parsing error. except Exception as e: - print(f"An unexpected error occurred while checking PageSpeed for {website}: {e}") - return 0 + logger.error(f"An unexpected error occurred while checking PageSpeed data for {website}: {e}") + return "⚪" # Grey: Unexpected error.