70 lines
2.5 KiB
Python
70 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Scarica il JSON dell'ultimo digest RIUSCITO del workflow v3 e lo salva in latest-digest.json.
|
|
Chiamato dal cronjob prima della narrazione. Include il testo dei README per la narrazione esaustiva."""
|
|
import json, sys, urllib.request, urllib.error
|
|
|
|
C = json.load(open('/opt/data/hn-oss-digest/creds.json'))
|
|
H = {'X-N8N-API-KEY': C['n8n_key']}
|
|
BASE = 'https://n8n.as89.it/api/v1'
|
|
WID = 'tZwGIY9rcdybQLPr'
|
|
|
|
def http(url):
|
|
req = urllib.request.Request(url, headers=H)
|
|
with urllib.request.urlopen(req, timeout=60) as r:
|
|
return r.read().decode()
|
|
|
|
d = json.loads(http(f'{BASE}/executions?limit=10&status=success&workflowId={WID}'))
|
|
data = d.get('data', [])
|
|
if not data:
|
|
print('NESSUNA ESECUZIONE RIUSCITA TROVATA')
|
|
sys.exit(1)
|
|
|
|
eid = data[0]['id']
|
|
x = json.loads(http(f'{BASE}/executions/{eid}?includeData=true'))
|
|
rd = x['data']['resultData']['runData']
|
|
sel = rd['Seleziona OSS'][0]['data']['main'][0][0]['json']
|
|
|
|
def node_json(node):
|
|
"""Primo json di output non vuoto del nodo, o None."""
|
|
runs = rd.get(node)
|
|
if not runs:
|
|
return None
|
|
for conn in runs[0].get('data', {}).get('main', []):
|
|
if conn:
|
|
return conn[0].get('json')
|
|
return None
|
|
|
|
g = node_json('Gemini riassume')
|
|
src = 'Gemini riassume'
|
|
if not g or 'candidates' not in g:
|
|
g = node_json('Gemini lite (fallback)')
|
|
src = 'Gemini lite (fallback)'
|
|
if not g or 'candidates' not in g:
|
|
print('NESSUN RIASSUNTO DISPONIBILE')
|
|
sys.exit(1)
|
|
print(f'sorgente riassunto: {src}')
|
|
txt = g['candidates'][0]['content']['parts'][0]['text']
|
|
dj = json.loads(txt)
|
|
|
|
# item originali (con il testo dei README) allegati alle sezioni finali
|
|
readme_by_url = {it.get('url', ''): it.get('readme', '') for it in sel.get('items', [])}
|
|
for k in ('show', 'repo', 'news'):
|
|
for it in dj.get('sections', {}).get(k, []):
|
|
it['readme'] = readme_by_url.get(it.get('url', ''), '')
|
|
|
|
out = {
|
|
'executionId': eid,
|
|
'count': sel.get('count'),
|
|
'conReadme': sel.get('conReadme'),
|
|
'sinceOld': sel.get('sinceOld'),
|
|
'sinceNew': sel.get('sinceNew'),
|
|
'geminiSource': src,
|
|
'sections': dj.get('sections', {}),
|
|
}
|
|
with open('/opt/data/hn-oss-digest/latest-digest.json', 'w') as f:
|
|
json.dump(out, f, ensure_ascii=False, indent=1)
|
|
|
|
tot = sum(len(out['sections'].get(k, [])) for k in ('show', 'repo', 'news'))
|
|
conr = sum(1 for k in ('show', 'repo', 'news') for it in out['sections'].get(k, []) if it.get('readme'))
|
|
print(f'exec {eid}: {tot} item salvati in latest-digest.json | README allegati: {conr}')
|