23 lines
1.3 KiB
Python
23 lines
1.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Schreibt ratgeber_reihenfolge aus artikel/*.json in die bestehenden WP-Beitraege (braucht Mu-Plugin ab 1.5.0)."""
|
|
import base64, glob, json, urllib.request
|
|
E = {}
|
|
for z in open('.env', encoding='utf-8'):
|
|
z = z.strip()
|
|
if z and not z.startswith('#') and '=' in z:
|
|
k, v = z.split('=', 1); E[k] = v.strip().strip('"')
|
|
AUTH = 'Basic ' + base64.b64encode(f"{E['WP_USER']}:{E['WP_APP_PASSWORD']}".encode()).decode()
|
|
WP = E['WP_BASE_URL'].rstrip('/') + '/wp-json/wp/v2/posts'
|
|
def ruf(url, daten=None):
|
|
r = urllib.request.Request(url, data=json.dumps(daten).encode() if daten else None, method='POST' if daten else 'GET',
|
|
headers={'Authorization': AUTH, 'Content-Type': 'application/json'})
|
|
return json.load(urllib.request.urlopen(r, timeout=60))
|
|
for f in sorted(glob.glob('artikel/artikel-*.json')):
|
|
a = json.load(open(f, encoding='utf-8'))
|
|
treffer = ruf(f"{WP}?slug={a['slug']}&status=draft,publish,pending,future,private&context=edit&_fields=id,meta")
|
|
if not treffer:
|
|
print('kein Beitrag', a['slug']); continue
|
|
pid = treffer[0]['id']
|
|
neu = ruf(f"{WP}/{pid}?_fields=id,meta", {'meta': {'ratgeber_reihenfolge': a.get('reihenfolge', 999)}})
|
|
print(pid, a['slug'], neu['meta'].get('ratgeber_reihenfolge', 'Feld unbekannt, Plugin 1.5.0 fehlt'))
|