pryscraper/account_manager.py
cryptorugmunch bb77eb5f35 chore(license): re-license to dual MIT (core) + BSL 1.1 (stealth)
Re-license Pry from full Proprietary to a dual-license model:

- Core engine, extraction, templates (80+), MCP server, x402 payment rail,
  CLI, SDK, browser extension, WordPress plugin, Shopify app, and
  llm_providers: MIT (see LICENSE)
- Anti-detection / stealth subset (15 files): BSL 1.1 with Change Date
  2029-01-01 (see LICENSE-BSL-STEALTH)

BSL files (anti-detection moat):
  ultimate_scraper.py, stealth_engine.py, stealth_scripts/*.js (6),
  camoufox_integration.py, tls_fingerprint.py, cookie_warmer.py,
  behavioral_biometrics.py, adaptive.py, browser_pool.py, network.py,
  captcha_solver.py, shadow_dom.py, lazy_load.py, signup_automator.py,
  auth_connector.py

This enables community contributions to the core engine (templates,
integrations, MCP tools) while protecting the anti-detection techniques
that constitute the actual competitive moat. BSL Additional Use Grant
permits free non-production use; production deployment requires a
commercial license from enterprise@rugmunch.io.

Changes:
- Replace proprietary LICENSE with MIT LICENSE + new LICENSE-BSL-STEALTH
- Add SPDX-License-Identifier headers to 300+ source files
- Add docs/adr/0002-dual-licensing.md (ADR documenting the decision)
- Update README.md: new License section with BSL Additional Use Grant
- Update LICENSING_PRICING_STRATEGY.md: Section 3 (PryScraper) for dual license
- Update AGENTS.md: license line in header + new rule 8 (PRs touching BSL rejected)
- Update pyproject.toml: license = "MIT AND BSL-1.1" + classifiers + license-files
- Update DECISIONS.md index with ADR-0002
- Update STATUS.md (2026-07-03) and PLAN.md sprint goals

Refs: ADR-0002
2026-07-02 19:49:21 +02:00

116 lines
4.3 KiB
Python

# SPDX-License-Identifier: MIT
# Copyright (c) 2026 Rug Munch Media LLC
#
# Part of Pry — https://git.rugmunch.io/RugMunchMedia/pryscraper
# Licensed under MIT. See LICENSE.
"""Pry — Account pool management, session persistence, proxy scoring."""
import json
import logging
import os
import time
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
logger = logging.getLogger(__name__)
ACCOUNTS_DIR = Path(os.path.expanduser("~/.pry/accounts"))
ACCOUNTS_DIR.mkdir(parents=True, exist_ok=True)
class AccountPool:
"""Manage pool of registered accounts with session persistence."""
def store(self, site: str, credentials: dict[str, Any], profile_id: str = "", metadata: dict | None = None) -> str:
import uuid
account_id = uuid.uuid4().hex[:12]
account = {
"id": account_id, "site": site, "credentials": credentials,
"profile_id": profile_id, "metadata": metadata or {},
"status": "active", "created_at": datetime.now(UTC).isoformat(),
"last_used": None, "use_count": 0, "errors": [],
}
path = ACCOUNTS_DIR / f"{site}_{account_id}.json"
path.write_text(json.dumps(account, indent=2))
logger.info("account_stored", extra={"site": site, "account_id": account_id})
return account_id
def get_active(self, site: str) -> dict[str, Any] | None:
"""Get a random active account for a site."""
accounts = []
for path in ACCOUNTS_DIR.glob(f"{site}_*.json"):
try:
acct = json.loads(path.read_text())
if acct.get("status") == "active":
accounts.append(acct)
except (json.JSONDecodeError, OSError):
continue
if not accounts:
return None
import random
return random.choice(accounts)
def mark_error(self, account_id: str, error: str) -> None:
for path in ACCOUNTS_DIR.glob(f"*_{account_id}.json"):
try:
acct = json.loads(path.read_text())
acct["errors"].append({"time": datetime.now(UTC).isoformat(), "error": error[:200]})
if len(acct["errors"]) > 5:
acct["status"] = "suspended"
path.write_text(json.dumps(acct, indent=2))
except Exception:
pass
def record_use(self, account_id: str) -> None:
for path in ACCOUNTS_DIR.glob(f"*_{account_id}.json"):
try:
acct = json.loads(path.read_text())
acct["last_used"] = datetime.now(UTC).isoformat()
acct["use_count"] = acct.get("use_count", 0) + 1
path.write_text(json.dumps(acct, indent=2))
except Exception:
pass
def list_accounts(self, site: str = "") -> list[dict[str, Any]]:
accounts = []
pattern = f"{site}_*.json" if site else "*_*.json"
for path in sorted(ACCOUNTS_DIR.glob(pattern), key=os.path.getmtime, reverse=True)[:50]:
try:
data = json.loads(path.read_text())
data.pop("credentials", None) # Don't expose passwords
accounts.append(data)
except Exception:
continue
return accounts
class ProxyScorer:
"""Score and rank proxies for reliability."""
async def test_proxy(self, proxy_url: str, test_url: str = "https://httpbin.org/ip", timeout: int = 10) -> dict[str, Any]:
from client import get_client
client = await get_client()
start = time.time()
try:
resp = await client.get(test_url, timeout=timeout)
elapsed = time.time() - start
return {"proxy": proxy_url, "working": resp.is_success, "latency": round(elapsed, 2),
"status": resp.status_code, "ip": resp.text[:50] if resp.is_success else ""}
except Exception as e:
return {"proxy": proxy_url, "working": False, "error": str(e)[:80]}
def score(self, test_result: dict[str, Any]) -> int:
"""Score proxy 0-100."""
if not test_result.get("working"):
return 0
latency = test_result.get("latency", 99)
if latency < 1:
return 100
if latency < 2:
return 90
if latency < 5:
return 75
if latency < 10:
return 50
return 25