Update app.py
Browse files
app.py
CHANGED
|
@@ -28,7 +28,7 @@ token_cache = {"token": None, "expires_at": 0}
|
|
| 28 |
|
| 29 |
|
| 30 |
def build_payload(query, page=1, size=20):
|
| 31 |
-
"""Payload fiel ao app-7.py
|
| 32 |
q = query or "*"
|
| 33 |
return {
|
| 34 |
"query": {
|
|
@@ -72,15 +72,15 @@ def build_payload(query, page=1, size=20):
|
|
| 72 |
],
|
| 73 |
"highlight": {
|
| 74 |
"fields": {
|
| 75 |
-
"ementa_texto": {"matched_fields": ["ementa_texto.plural"], "type": "fvh", "fragment_size":
|
| 76 |
-
"acordao_ata": {"matched_fields": ["acordao_ata.plural"], "type": "fvh", "fragment_size":
|
| 77 |
-
"decisao_texto": {"matched_fields": ["decisao_texto.plural"], "type": "fvh", "fragment_size":
|
| 78 |
-
"documental_tese_texto": {"matched_fields": ["documental_tese_texto.plural"], "type": "fvh", "fragment_size":
|
| 79 |
},
|
| 80 |
"pre_tags": ["<em>"],
|
| 81 |
"post_tags": ["</em>"],
|
| 82 |
-
"fragment_size":
|
| 83 |
-
"number_of_fragments":
|
| 84 |
"order": "score"
|
| 85 |
},
|
| 86 |
"size": size,
|
|
@@ -94,7 +94,7 @@ def get_fresh_token():
|
|
| 94 |
global token_cache
|
| 95 |
if token_cache["token"] and time.time() < token_cache["expires_at"]:
|
| 96 |
return token_cache["token"]
|
| 97 |
-
logger.info("🔄
|
| 98 |
try:
|
| 99 |
with sync_playwright() as p:
|
| 100 |
browser = p.chromium.launch(headless=True, args=[
|
|
@@ -105,8 +105,7 @@ def get_fresh_token():
|
|
| 105 |
viewport={"width": 1920, "height": 1080},
|
| 106 |
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
|
| 107 |
locale="pt-BR",
|
| 108 |
-
timezone_id="America/Sao_Paulo"
|
| 109 |
-
extra_http_headers={"Accept-Language": "pt-BR,pt;q=0.9,en;q=0.8"}
|
| 110 |
)
|
| 111 |
page = context.new_page()
|
| 112 |
page.goto("https://jurisprudencia.stf.jus.br/pages/search", wait_until="networkidle", timeout=45000)
|
|
@@ -119,31 +118,27 @@ def get_fresh_token():
|
|
| 119 |
if token:
|
| 120 |
token_cache["token"] = token
|
| 121 |
token_cache["expires_at"] = time.time() + 3300
|
| 122 |
-
logger.info(f"✅ Token OK: {token[:20]}...")
|
| 123 |
return token
|
| 124 |
-
logger.error("❌ Token não encontrado nos cookies")
|
| 125 |
except Exception as e:
|
| 126 |
-
logger.error(f"
|
| 127 |
return None
|
| 128 |
|
| 129 |
|
| 130 |
def do_search(payload):
|
| 131 |
-
"""Executa POST na API e extrai hits/total"""
|
| 132 |
token = get_fresh_token()
|
| 133 |
if not token:
|
| 134 |
return None, "Token WAF não obtido"
|
| 135 |
headers = HEADERS.copy()
|
| 136 |
headers["Cookie"] = f"aws-waf-token={token}"
|
| 137 |
try:
|
| 138 |
-
resp = requests.post(URL_API, headers=headers, json=payload, verify=False, timeout=
|
| 139 |
logger.info(f"HTTP {resp.status_code}")
|
| 140 |
if resp.status_code == 403:
|
| 141 |
token_cache["token"] = None
|
| 142 |
return None, "Token expirado (403)"
|
| 143 |
if resp.status_code != 200:
|
| 144 |
-
return None, f"HTTP {resp.status_code}
|
| 145 |
data = resp.json()
|
| 146 |
-
# ⚠️ A API STF retorna {"result": {"hits": {...}}}
|
| 147 |
hits_root = data.get("result", data)
|
| 148 |
hits = hits_root.get("hits", {})
|
| 149 |
return {"hits": hits.get("hits", []), "total": hits.get("total", {}).get("value", 0)}, None
|
|
@@ -158,19 +153,21 @@ def format_results(raw, query):
|
|
| 158 |
source = hit.get("_source", {})
|
| 159 |
hl = hit.get("highlight", {})
|
| 160 |
|
| 161 |
-
#
|
| 162 |
-
|
| 163 |
-
hl.get("ementa_texto", [None])[0]
|
| 164 |
-
hl.get("documental_tese_texto", [None])[0]
|
| 165 |
-
hl.get("acordao_ata", [None])[0]
|
| 166 |
-
hl.get("decisao_texto", [None])[0]
|
| 167 |
-
source.get("ementa_texto")
|
| 168 |
-
source.get("documental_tese_texto")
|
| 169 |
-
source.get("acordao_ata")
|
| 170 |
-
source.get("decisao_texto")
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
| 174 |
|
| 175 |
url_doc = (
|
| 176 |
source.get("inteiro_teor_url") or
|
|
@@ -189,7 +186,7 @@ def format_results(raw, query):
|
|
| 189 |
"relator": source.get("relator_processo_nome"),
|
| 190 |
"data": source.get("julgamento_data"),
|
| 191 |
"url_documento": url_doc,
|
| 192 |
-
"ementa": ementa.strip(),
|
| 193 |
"citacao": citacao.strip()
|
| 194 |
})
|
| 195 |
return results
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
def build_payload(query, page=1, size=20):
|
| 31 |
+
"""Payload fiel ao app-7.py - TEXTO COMPLETO"""
|
| 32 |
q = query or "*"
|
| 33 |
return {
|
| 34 |
"query": {
|
|
|
|
| 72 |
],
|
| 73 |
"highlight": {
|
| 74 |
"fields": {
|
| 75 |
+
"ementa_texto": {"matched_fields": ["ementa_texto.plural"], "type": "fvh", "fragment_size": 999999},
|
| 76 |
+
"acordao_ata": {"matched_fields": ["acordao_ata.plural"], "type": "fvh", "fragment_size": 999999},
|
| 77 |
+
"decisao_texto": {"matched_fields": ["decisao_texto.plural"], "type": "fvh", "fragment_size": 999999},
|
| 78 |
+
"documental_tese_texto": {"matched_fields": ["documental_tese_texto.plural"], "type": "fvh", "fragment_size": 999999}
|
| 79 |
},
|
| 80 |
"pre_tags": ["<em>"],
|
| 81 |
"post_tags": ["</em>"],
|
| 82 |
+
"fragment_size": 999999, # Máximo
|
| 83 |
+
"number_of_fragments": 0, # Fragmento único COMPLETO
|
| 84 |
"order": "score"
|
| 85 |
},
|
| 86 |
"size": size,
|
|
|
|
| 94 |
global token_cache
|
| 95 |
if token_cache["token"] and time.time() < token_cache["expires_at"]:
|
| 96 |
return token_cache["token"]
|
| 97 |
+
logger.info("🔄 Token...")
|
| 98 |
try:
|
| 99 |
with sync_playwright() as p:
|
| 100 |
browser = p.chromium.launch(headless=True, args=[
|
|
|
|
| 105 |
viewport={"width": 1920, "height": 1080},
|
| 106 |
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
|
| 107 |
locale="pt-BR",
|
| 108 |
+
timezone_id="America/Sao_Paulo"
|
|
|
|
| 109 |
)
|
| 110 |
page = context.new_page()
|
| 111 |
page.goto("https://jurisprudencia.stf.jus.br/pages/search", wait_until="networkidle", timeout=45000)
|
|
|
|
| 118 |
if token:
|
| 119 |
token_cache["token"] = token
|
| 120 |
token_cache["expires_at"] = time.time() + 3300
|
|
|
|
| 121 |
return token
|
|
|
|
| 122 |
except Exception as e:
|
| 123 |
+
logger.error(f"Token erro: {e}")
|
| 124 |
return None
|
| 125 |
|
| 126 |
|
| 127 |
def do_search(payload):
|
|
|
|
| 128 |
token = get_fresh_token()
|
| 129 |
if not token:
|
| 130 |
return None, "Token WAF não obtido"
|
| 131 |
headers = HEADERS.copy()
|
| 132 |
headers["Cookie"] = f"aws-waf-token={token}"
|
| 133 |
try:
|
| 134 |
+
resp = requests.post(URL_API, headers=headers, json=payload, verify=False, timeout=45)
|
| 135 |
logger.info(f"HTTP {resp.status_code}")
|
| 136 |
if resp.status_code == 403:
|
| 137 |
token_cache["token"] = None
|
| 138 |
return None, "Token expirado (403)"
|
| 139 |
if resp.status_code != 200:
|
| 140 |
+
return None, f"HTTP {resp.status_code}"
|
| 141 |
data = resp.json()
|
|
|
|
| 142 |
hits_root = data.get("result", data)
|
| 143 |
hits = hits_root.get("hits", {})
|
| 144 |
return {"hits": hits.get("hits", []), "total": hits.get("total", {}).get("value", 0)}, None
|
|
|
|
| 153 |
source = hit.get("_source", {})
|
| 154 |
hl = hit.get("highlight", {})
|
| 155 |
|
| 156 |
+
# Prioridade: highlight COMPLETO > source
|
| 157 |
+
ementa_candidates = [
|
| 158 |
+
hl.get("ementa_texto", [None])[0],
|
| 159 |
+
hl.get("documental_tese_texto", [None])[0],
|
| 160 |
+
hl.get("acordao_ata", [None])[0],
|
| 161 |
+
hl.get("decisao_texto", [None])[0],
|
| 162 |
+
source.get("ementa_texto"),
|
| 163 |
+
source.get("documental_tese_texto"),
|
| 164 |
+
source.get("acordao_ata"),
|
| 165 |
+
source.get("decisao_texto")
|
| 166 |
+
]
|
| 167 |
+
ementa = next((c for c in ementa_candidates if c), "")
|
| 168 |
+
|
| 169 |
+
# NÃO CORTA MAIS — texto COMPLETO com highlight
|
| 170 |
+
# Se highlight tem fragment_size=999999 → texto todo
|
| 171 |
|
| 172 |
url_doc = (
|
| 173 |
source.get("inteiro_teor_url") or
|
|
|
|
| 186 |
"relator": source.get("relator_processo_nome"),
|
| 187 |
"data": source.get("julgamento_data"),
|
| 188 |
"url_documento": url_doc,
|
| 189 |
+
"ementa": ementa.strip(), # TEXTO COMPLETO
|
| 190 |
"citacao": citacao.strip()
|
| 191 |
})
|
| 192 |
return results
|