Files
Skrift-Auto-Article/build/nw_score.py

25 lines
1.4 KiB
Python

# -*- coding: utf-8 -*-
"""Lokaler NeuronWriter-Score, identischer Pruefkoerper wie WF2 (H1 + Kurzantwort + Inhalt + FAQ).
Aufruf: python3 build/nw_score.py artikel/artikel-SLUG.json [...]"""
import json, os, sys, urllib.request
def env(p='.env'):
d = {}
for z in open(p, encoding='utf-8'):
z = z.strip()
if z and not z.startswith('#') and '=' in z:
k, v = z.split('=', 1); d[k] = v.strip().strip('"')
return d
E = env()
for pfad in sys.argv[1:]:
a = json.load(open(pfad, encoding='utf-8'))
faq = '\n'.join(f"<h3>{f.get('frage','')}</h3><p>{f.get('antwort','')}</p>" for f in a.get('faq') or [])
html = '\n'.join([f"<h1>{a['titel']}</h1>", f"<p>{a['kurzantwort']}</p>" if a.get('kurzantwort') else '', a['content_html'],
f"<h2>{a.get('faq_ueberschrift') or 'Häufige Fragen'}</h2>\n{faq}" if faq else ''])
body = json.dumps({'query': a['query_id'], 'html': html, 'title': a.get('seo_titel') or a['titel'],
'description': a.get('seo_beschreibung', '')}).encode()
r = urllib.request.Request('https://app.neuronwriter.com/neuron-api/0.5/writer/evaluate-content', data=body,
headers={'X-API-KEY': E['NEURONWRITER_API_KEY'], 'Content-Type': 'application/json'})
print(a['slug'], json.load(urllib.request.urlopen(r, timeout=90)).get('content_score'))