iero
April 27, 2021, 11:40pm
1
Hello
I would like to automatically export my ghost blog (the json file) using a script.
It seems that the /api/v3/admin/db/
access point does not exist anymore? I tried replacing v3 with v4 and with ‘canary’… but nothing works.
I have this error:
{"errors":[{"message":"The server does not support the functionality required to fulfill the request.","context":null,"type":"NotImplementedError","details":null,"property":null,"help":null,"code":null,"id":"6b0bd680-a7b1-11eb-ad7e-0db24bf6ee6d"}]}ie
disclaimer: I know that it is not documented and not stable, etc.
iero
April 29, 2021, 12:47am
2
Ok, just found that I have to use a user/pwd authentication and not a token based.
@admins : do you plan to add DB access using token authentication?
For the records, here is a working python code
import argparse
import json
import requests
import re
import logging
from datetime import datetime
logger = logging.getLogger(__name__)
def get_config(config_file):
try:
with open(config_file) as json_file:
json_data = json.load(json_file)
return json_data
except (IOError, json.decoder.JSONDecodeError):
logger.error("Incorrect of missing JSON config file")
exit(1)
def authenticate(config, server):
data = {
'username': config[server]['user_name'],
'password': config[server]['user_password']
}
url = '{}/ghost/api/v3/admin/session/'.format(
config[server]['ghost_url'])
session = requests.Session()
r = session.post(
url=url,
data=data)
if r.status_code != 201:
"Cannot connect to server"
exit(1)
return session
def export(session, config, server):
url = '{}/ghost/api/v3/admin/db/'.format(
config[server]['ghost_url'])
r = session.get(url)
if r.status_code == 401:
"Cannot connect to server to get post"
exit(1)
elif r.status_code == 200:
return r.json()
def write_json(f_path, server, data):
now = datetime.now().utcnow()
f_name = now.strftime(
'{}/{}_%Y-%m-%d-%H-%M-%S.json'.format(
f_path, server))
with open(f_name, 'w') as f:
json.dump(data, f)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, required=True)
parser.add_argument("--server", type=str, required=True)
parser.add_argument("--backup", type=str, required=True)
option = parser.parse_args()
config = get_config(option.config)
session = authenticate(config, option.server)
json_export = export(session, config, option.server)
ghost_url = config[option.server]['ghost_url']
url = re.compile(r"https?://(www\.)?")
ghost_url_name = url.sub('', ghost_url).strip().strip('/')
ghost_url_name = ghost_url_name.replace('.', '_')
write_json(option.backup, ghost_url_name, json_export)