Files
Investidor-V2/tools/legacy-financial-origin-check.py
nala 20971d523f
tests / ci (push) Canceled after 0s
oportunidades 6
2026-10-10 21:17:46 +02:00

46 lines
2.6 KiB
Python

"""Verify the untouched origin field independently of the editorial adapter."""
import hashlib
import html
import json
import re
import urllib.request
from datetime import datetime, timezone
from pathlib import Path
root = Path(__file__).resolve().parents[1]
directory = root / 'storage/app/private/investmoz-v2/portal-audit/legacy-financial-origin-20261010'
directory.mkdir(parents=True, exist_ok=True)
url = 'https://investemoz.tempo.co.mz/mercado-financeiro'
agent = 'Mozilla/5.0 InvestMoz-QA-content-inventory'
with urllib.request.urlopen(urllib.request.Request(url, headers={'Accept': 'text/html', 'User-Agent': agent}), timeout=35) as response:
raw = response.read()
status_html = response.status
match = re.search(r'data-page="([^"]+)"', raw.decode('utf-8'))
assert match
page = json.loads(html.unescape(match.group(1)))
post = page['props']['post']
assert post['id'] == 171994
headers = {'Accept': 'application/json', 'User-Agent': agent, 'X-Inertia': 'true', 'X-Requested-With': 'XMLHttpRequest', 'X-Inertia-Version': page['version']}
with urllib.request.urlopen(urllib.request.Request(url, headers=headers), timeout=35) as response:
raw_json = response.read()
status_json = response.status
data = json.loads(raw_json)
assert data['props']['post']['id'] == post['id']
# Preserve anonymous HTTP snapshots privately; never serve shared session props.
(directory / 'origin-html-private.bin').write_bytes(raw)
(directory / 'origin-inertia-private.bin').write_bytes(raw_json)
proof = {'captured_at': datetime.now(timezone.utc).isoformat(), 'source_url': url,
'legacy_id': post['id'], 'html_http_status': status_html, 'inertia_http_status': status_json,
'origin_html_bytes_sha256': hashlib.sha256(raw).hexdigest(),
'origin_inertia_bytes_sha256': hashlib.sha256(raw_json).hexdigest(),
'initial_origin_content_type': type(post['content']).__name__,
'initial_origin_content_length': len(post['content']),
'inertia_origin_content_length': len(data['props']['post']['content']),
'contents_identical': data['props']['post']['content'] == post['content'],
'origin_content_sha256': hashlib.sha256(post['content'].encode()).hexdigest(),
'extraction_or_sanitization_applied': False, 'publication_applied': False,
'conclusion': 'Origin returns an empty string in both raw server payloads; not an extraction loss.'}
assert proof['initial_origin_content_length'] == 0 and proof['inertia_origin_content_length'] == 0
(directory / 'proof.json').write_text(json.dumps(proof, ensure_ascii=False, indent=2), encoding='utf-8')
print(json.dumps(proof, ensure_ascii=False))