diff --git a/src/api/main.py b/src/api/main.py index 3d5ea32..64fa783 100644 --- a/src/api/main.py +++ b/src/api/main.py @@ -20,7 +20,7 @@ def get_app() -> FastAPI: """ app = FastAPI( title="Masquer API", - summary="A tool to generate random user-agent and referer data for GET requests.", + summary="A tool to generate random user-agent and referer data for HTTP requests.", description=DESCRIPTION, version=__version__, license_info={ diff --git a/update.py b/update.py index 4c41e55..1a82f5b 100644 --- a/update.py +++ b/update.py @@ -1,6 +1,7 @@ import json import os import requests +import sys from bs4 import BeautifulSoup @@ -21,10 +22,11 @@ def update_useragents() -> bool: with open(os.path.join(ASSETS_DIR, "useragents.json"), "w") as f: json.dump(json_string, f) + print(f"{sys.argv[0]}: fetched useragent data") return True except Exception as e: - print(f"Error fetching useragent data: {e}") + print(f"{sys.argv[0]}: error fetching useragent data — {e}") return False @@ -65,10 +67,11 @@ def update_referers() -> bool: with open(os.path.join(ASSETS_DIR, "referers.json"), "w") as f: json.dump(output, f) + print(f"{sys.argv[0]}: fetched referer data") return True except Exception as e: - print(f"Error fetching referer data: {e}") + print(f"{sys.argv[0]}: error fetching referer data — {e}") return False @@ -123,14 +126,15 @@ def update_assets() -> bool: f.write("USERAGENT_WEIGHTS = " + str(useragent_weights)) f.write("\n") + print(f"{sys.argv[0]}: saved useragent and referer JSON data to assets.py") return True except FileNotFoundError: - print("Error: JSON asset(s) missing") + print(f"{sys.argv[0]}: asset update error — JSON assets not found") return False except Exception as e: - print(f"{type(e)}: {e}") + print(f"{sys.argv[0]}: asset update error — {type(e)}: {e}") return False @@ -139,9 +143,9 @@ def update_assets() -> bool: rf = update_referers() if ua and rf: assets_updated = update_assets() - if not assets_updated: - print("Failed to update assets.py") + if assets_updated: + sys.exit(0) else: - print("Update successful") + sys.exit(2) else: - print("Failed to update JSON assets") + sys.exit(3) diff --git a/update.sh b/update.sh index 6877482..3dabea9 100755 --- a/update.sh +++ b/update.sh @@ -1,20 +1,49 @@ #!/bin/bash +set -eu -# Activates venv and runs ./update.py -# to get and save latest assets +# 'update.sh' activates a virtual environment and runs 'update.py' +# to get and save the latest user-agent and referer assets from +# external sources, then deactivates the virtual environment. -source .venv/bin/activate +# Usage: +# "$ bash update.sh" -if ! source .venv/bin/activate; then - echo "Error: couldn't activate venv." - echo "Create venv or update this script's venv path." +this_script=$(basename "$0") +python_script="update.py" +venv_path=".venv/bin/activate" + +if [[ ! -f "${venv_path}" ]]; then + echo "${this_script}: error — virtual environment not found at ${venv_path}" + echo " create a new virtual environment or update 'venv_path' in ${this_script}" exit 1 fi -python3 update.py +source "${venv_path}" -if [[ $? -eq 1 ]]; then - echo "Update failed — ensure BeautifulSoup4 is installed." +if [[ ! -f "${python_script}" ]]; then + echo "${this_script}: error — ${python_script} not found" + deactivate + exit 1 fi +python3 "${python_script}" +exit_code=$? + +case ${exit_code} in + 0) + echo "${this_script}: asset update successful" + ;; + 2) + echo "${this_script}: asset update failed — check file paths in ${python_script}" + ;; + 3) + echo "${this_script}: asset update failed — ensure BeautifulSoup4 is" + echo " installed and check source URLs in ${python_script} are live" + ;; + *) + echo "${this_script}: asset update failed with unexpected exit code ${exit_code}" + ;; +esac + deactivate +exit "${exit_code}"