Files
matitos_news/utils/Ghost-Posts.ipynb
Luciano Gervasoni 8ea3ec1bda Utils
2025-04-23 16:26:08 +02:00

89 lines
2.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import time\n",
"import jwt\n",
"import requests\n",
"\n",
"admin_api_url = \"\"\n",
"admin_api_key = \"\"\n",
"\n",
"def _create_jwt(admin_api_key):\n",
" id_, secret = admin_api_key.split(':')\n",
" iat = int(time.time())\n",
" exp = iat + 5 * 60 # 5 minutes\n",
" header = {'alg': 'HS256', 'kid': id_}\n",
" payload = {\n",
" 'iat': iat,\n",
" 'exp': exp,\n",
" 'aud': '/v5/admin/' # Adjust depending on your Ghost version\n",
" }\n",
" token = jwt.encode(payload, bytes.fromhex(secret), algorithm='HS256', headers=header)\n",
" return token\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get token\n",
"jwt_token = _create_jwt(os.getenv(\"GHOST_ADMIN_API_KEY\"))\n",
"\n",
"headers = {\n",
" 'Authorization': f'Ghost {jwt_token}',\n",
" 'Content-Type': 'application/json'\n",
"}\n",
"\n",
"deleted_post = True\n",
"\n",
"while (deleted_post):\n",
" # GET /admin/posts/\n",
" response = requests.get(os.path.join(admin_api_url, \"posts\"), headers=headers)\n",
" dict_response = response.json()\n",
"\n",
" if (len(dict_response.get(\"posts\")) == 0):\n",
" deleted_post = False\n",
" break\n",
"\n",
" # Iterate posts\n",
" for p in dict_response.get(\"posts\"):\n",
" # Post ID\n",
" post_id = p.get(\"id\")\n",
"\n",
" # DELETE /admin/posts/{id}/\n",
" r = requests.delete(os.path.join(admin_api_url, \"posts\", \"{}\".format(post_id)), headers=headers)\n",
" print(\"Post:\", post_id, \"Status:\", r.status_code, r.text)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "matitos_urls",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}