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
126 lines
4.4 KiB
Python
126 lines
4.4 KiB
Python
"""Pry — Result Deduplication using SimHash.
|
|
Detects near-duplicate content (e.g., 95% similar pages) using SimHash.
|
|
Useful for crawl deduplication, change detection, and content grouping."""
|
|
|
|
# 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.
|
|
|
|
import hashlib
|
|
import logging
|
|
import re
|
|
from typing import Any
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class SimHash:
|
|
"""SimHash implementation for near-duplicate detection."""
|
|
|
|
@staticmethod
|
|
def hash(text: str, n_features: int = 128) -> int:
|
|
"""Generate a SimHash fingerprint for text."""
|
|
tokens = re.findall(r"\w+", text.lower())
|
|
if not tokens:
|
|
return 0
|
|
vector = [0] * n_features
|
|
for token in tokens:
|
|
token_hash = int(hashlib.md5(token.encode()).hexdigest(), 16)
|
|
for i in range(n_features):
|
|
if token_hash & (1 << i):
|
|
vector[i] += 1
|
|
else:
|
|
vector[i] -= 1
|
|
result = 0
|
|
for i in range(n_features):
|
|
if vector[i] > 0:
|
|
result |= 1 << i
|
|
return result
|
|
|
|
@staticmethod
|
|
def hamming_distance(hash1: int, hash2: int) -> int:
|
|
"""Count differing bits between two hashes (lower = more similar)."""
|
|
x = hash1 ^ hash2
|
|
distance = 0
|
|
while x:
|
|
distance += x & 1
|
|
x >>= 1
|
|
return distance
|
|
|
|
@staticmethod
|
|
def similarity(hash1: int, hash2: int, n_features: int = 128) -> float:
|
|
"""Calculate similarity (0-1) between two SimHashes."""
|
|
dist = SimHash.hamming_distance(hash1, hash2)
|
|
return 1.0 - (dist / n_features)
|
|
|
|
|
|
class Deduplicator:
|
|
"""Find near-duplicate content across documents."""
|
|
|
|
def __init__(self, threshold: float = 0.85):
|
|
self.threshold = threshold
|
|
self._hashes: dict[str, int] = {}
|
|
|
|
def add(self, doc_id: str, text: str) -> int:
|
|
"""Add a document and return its SimHash."""
|
|
h = SimHash.hash(text)
|
|
self._hashes[doc_id] = h
|
|
return h
|
|
|
|
def find_duplicates(self, text: str) -> list[dict[str, Any]]:
|
|
"""Find existing documents that are near-duplicates of this text."""
|
|
target_hash = SimHash.hash(text)
|
|
duplicates: list[dict[str, Any]] = []
|
|
for doc_id, doc_hash in self._hashes.items():
|
|
sim = SimHash.similarity(target_hash, doc_hash)
|
|
if sim >= self.threshold:
|
|
duplicates.append(
|
|
{
|
|
"doc_id": doc_id,
|
|
"similarity": round(sim, 3),
|
|
"distance": SimHash.hamming_distance(target_hash, doc_hash),
|
|
}
|
|
)
|
|
return sorted(duplicates, key=lambda x: -x["similarity"])
|
|
|
|
def cluster(self, texts: dict[str, str]) -> dict[str, list[str]]:
|
|
"""Cluster texts by similarity. Returns {cluster_id: [doc_ids]}."""
|
|
hashes = {doc_id: SimHash.hash(text) for doc_id, text in texts.items()}
|
|
clusters: dict[str, list[str]] = {}
|
|
visited: set[str] = set()
|
|
cluster_id = 0
|
|
for doc_id, hash_val in hashes.items():
|
|
if doc_id in visited:
|
|
continue
|
|
cluster = [doc_id]
|
|
visited.add(doc_id)
|
|
for other_id, other_hash in hashes.items():
|
|
if other_id in visited:
|
|
continue
|
|
if SimHash.similarity(hash_val, other_hash) >= self.threshold:
|
|
cluster.append(other_id)
|
|
visited.add(other_id)
|
|
if cluster:
|
|
clusters[f"cluster_{cluster_id}"] = cluster
|
|
cluster_id += 1
|
|
return clusters
|
|
|
|
def diff(self, text1: str, text2: str) -> dict[str, Any]:
|
|
"""Show what changed between two versions of the same content."""
|
|
hash1 = SimHash.hash(text1)
|
|
hash2 = SimHash.hash(text2)
|
|
sim = SimHash.similarity(hash1, hash2)
|
|
distance = SimHash.hamming_distance(hash1, hash2)
|
|
return {
|
|
"similarity": round(sim, 3),
|
|
"hamming_distance": distance,
|
|
"changed": sim < self.threshold,
|
|
"change_severity": (
|
|
"high" if distance > 20
|
|
else "medium" if distance > 10
|
|
else "low" if distance > 3
|
|
else "minimal"
|
|
),
|
|
}
|