Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend update scripts #14

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand Down
20 changes: 12 additions & 8 deletions update.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import requests
import sys
from bs4 import BeautifulSoup


Expand All @@ -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


Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand All @@ -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)
47 changes: 38 additions & 9 deletions update.sh
Original file line number Diff line number Diff line change
@@ -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}"