Spaces:
Sleeping
Sleeping
Commit ·
b719f1b
1
Parent(s): 74fc277
Project Uploaded
Browse files- api_server.py +11 -7
api_server.py
CHANGED
|
@@ -11,9 +11,9 @@ from dotenv import load_dotenv
|
|
| 11 |
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
-
# --- START: CRITICAL DEFINITIONS
|
|
|
|
| 15 |
|
| 16 |
-
# 1. Logging System Definition
|
| 17 |
class LogBuffer:
|
| 18 |
def __init__(self, max_items: int = 10000):
|
| 19 |
self._buf: List[Dict[str, Any]] = []
|
|
@@ -40,7 +40,6 @@ def log(msg: str, level: str = "info", source: str = "server"):
|
|
| 40 |
logs.append(msg, level, source)
|
| 41 |
print(f"[{level.upper()}][{source}] {msg}", flush=True)
|
| 42 |
|
| 43 |
-
# 2. Base64 Helper
|
| 44 |
def decode_base64_with_padding(b64_string: str) -> bytes:
|
| 45 |
"""Decodes a Base64 string, adding missing padding if necessary."""
|
| 46 |
missing_padding = len(b64_string) % 4
|
|
@@ -49,10 +48,11 @@ def decode_base64_with_padding(b64_string: str) -> bytes:
|
|
| 49 |
try:
|
| 50 |
return base64.b64decode(b64_string)
|
| 51 |
except binascii.Error as e:
|
| 52 |
-
log(f"Error decoding base64 string: {e}", "error", "SERVER")
|
| 53 |
return b""
|
| 54 |
|
| 55 |
-
# --- END: CRITICAL DEFINITIONS
|
|
|
|
| 56 |
|
| 57 |
# Define a writable directory for ALL runtime files
|
| 58 |
WRITABLE_DIR = "/tmp"
|
|
@@ -72,6 +72,7 @@ if 'SERVICE_ACCOUNT_B64' in os.environ:
|
|
| 72 |
with open(SERVICE_ACCOUNT_FILE, 'w') as f:
|
| 73 |
f.write(decoded_service_account.decode('utf-8'))
|
| 74 |
|
|
|
|
| 75 |
GROUPS_TXT = os.environ.get("GROUPS_TXT", "groups.txt")
|
| 76 |
FINAL5_PATH = os.environ.get("FINAL5_PATH", "final5.py")
|
| 77 |
PYTHON_BIN = os.environ.get("PYTHON_BIN", "python")
|
|
@@ -90,7 +91,8 @@ GMAIL_SCOPES = [ "https://www.googleapis.com/auth/gmail.send" ]
|
|
| 90 |
os.makedirs(SCRAPE_OUTDIR, exist_ok=True)
|
| 91 |
os.makedirs(ANALYSIS_OUTDIR, exist_ok=True)
|
| 92 |
|
| 93 |
-
|
|
|
|
| 94 |
def build_gmail_service():
|
| 95 |
if not os.path.exists(SERVICE_ACCOUNT_FILE):
|
| 96 |
log("Service account file not found, Gmail unavailable.", "error", "GMAIL")
|
|
@@ -107,9 +109,11 @@ def build_gmail_service():
|
|
| 107 |
log(f"CRITICAL: Ensure your service account has Domain-Wide Delegation enabled for the user {SENDER_EMAIL}", "error", "GMAIL")
|
| 108 |
return None
|
| 109 |
|
|
|
|
| 110 |
gmail_service = build_gmail_service()
|
| 111 |
|
| 112 |
|
|
|
|
| 113 |
@dataclass
|
| 114 |
class GroupRun:
|
| 115 |
link: str
|
|
@@ -416,4 +420,4 @@ def api_get_recipients():
|
|
| 416 |
|
| 417 |
if __name__ == "__main__":
|
| 418 |
port = int(os.environ.get("PORT", 7860))
|
| 419 |
-
app.run(host="0.0.0.0", port=port)
|
|
|
|
| 11 |
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
+
# --- START: CRITICAL DEFINITIONS SECTION ---
|
| 15 |
+
# This entire block is moved to the top to ensure 'log' is always available.
|
| 16 |
|
|
|
|
| 17 |
class LogBuffer:
|
| 18 |
def __init__(self, max_items: int = 10000):
|
| 19 |
self._buf: List[Dict[str, Any]] = []
|
|
|
|
| 40 |
logs.append(msg, level, source)
|
| 41 |
print(f"[{level.upper()}][{source}] {msg}", flush=True)
|
| 42 |
|
|
|
|
| 43 |
def decode_base64_with_padding(b64_string: str) -> bytes:
|
| 44 |
"""Decodes a Base64 string, adding missing padding if necessary."""
|
| 45 |
missing_padding = len(b64_string) % 4
|
|
|
|
| 48 |
try:
|
| 49 |
return base64.b64decode(b64_string)
|
| 50 |
except binascii.Error as e:
|
| 51 |
+
log(f"Error decoding base64 string: {e}", "error", "SERVER")
|
| 52 |
return b""
|
| 53 |
|
| 54 |
+
# --- END: CRITICAL DEFINITIONS SECTION ---
|
| 55 |
+
|
| 56 |
|
| 57 |
# Define a writable directory for ALL runtime files
|
| 58 |
WRITABLE_DIR = "/tmp"
|
|
|
|
| 72 |
with open(SERVICE_ACCOUNT_FILE, 'w') as f:
|
| 73 |
f.write(decoded_service_account.decode('utf-8'))
|
| 74 |
|
| 75 |
+
# Define global constants
|
| 76 |
GROUPS_TXT = os.environ.get("GROUPS_TXT", "groups.txt")
|
| 77 |
FINAL5_PATH = os.environ.get("FINAL5_PATH", "final5.py")
|
| 78 |
PYTHON_BIN = os.environ.get("PYTHON_BIN", "python")
|
|
|
|
| 91 |
os.makedirs(SCRAPE_OUTDIR, exist_ok=True)
|
| 92 |
os.makedirs(ANALYSIS_OUTDIR, exist_ok=True)
|
| 93 |
|
| 94 |
+
|
| 95 |
+
# Define the Gmail service builder function
|
| 96 |
def build_gmail_service():
|
| 97 |
if not os.path.exists(SERVICE_ACCOUNT_FILE):
|
| 98 |
log("Service account file not found, Gmail unavailable.", "error", "GMAIL")
|
|
|
|
| 109 |
log(f"CRITICAL: Ensure your service account has Domain-Wide Delegation enabled for the user {SENDER_EMAIL}", "error", "GMAIL")
|
| 110 |
return None
|
| 111 |
|
| 112 |
+
# Now that all setup is done, build the service
|
| 113 |
gmail_service = build_gmail_service()
|
| 114 |
|
| 115 |
|
| 116 |
+
# --- The rest of the application code follows ---
|
| 117 |
@dataclass
|
| 118 |
class GroupRun:
|
| 119 |
link: str
|
|
|
|
| 420 |
|
| 421 |
if __name__ == "__main__":
|
| 422 |
port = int(os.environ.get("PORT", 7860))
|
| 423 |
+
app.run(host="0.0.0.0", port=port)```
|