Schools NL, Ghost post utils, nude + age detection

This commit is contained in:
Luciano Gervasoni
2025-04-30 15:50:54 +02:00
parent aa369d0458
commit ccfd0f9188
11 changed files with 841 additions and 246 deletions

View File

@@ -10,6 +10,7 @@
"import time\n",
"import jwt\n",
"import requests\n",
"from datetime import datetime, timedelta, timezone\n",
"\n",
"admin_api_url = \"\"\n",
"admin_api_key = \"\"\n",
@@ -25,7 +26,15 @@
" '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"
" return token\n",
"\n",
"# 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",
"}"
]
},
{
@@ -34,33 +43,100 @@
"metadata": {},
"outputs": [],
"source": [
"# Get token\n",
"jwt_token = _create_jwt(os.getenv(\"GHOST_ADMIN_API_KEY\"))\n",
"DELETE_ALL_POSTS = False\n",
"\n",
"headers = {\n",
" 'Authorization': f'Ghost {jwt_token}',\n",
" 'Content-Type': 'application/json'\n",
"}\n",
"if DELETE_ALL_POSTS:\n",
" while (True):\n",
" # GET /admin/posts/\n",
" response = requests.get(os.path.join(admin_api_url, \"posts\"), headers=headers)\n",
" dict_response = response.json()\n",
"\n",
"deleted_post = True\n",
" if (len(dict_response.get(\"posts\")) == 0):\n",
" break\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",
" # Iterate posts\n",
" for p in dict_response.get(\"posts\"):\n",
" # Post ID\n",
" post_id = p.get(\"id\")\n",
"\n",
" if (len(dict_response.get(\"posts\")) == 0):\n",
" deleted_post = False\n",
" break\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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"PUBLISH_SAMPLE = False\n",
"\n",
" # Iterate posts\n",
" for p in dict_response.get(\"posts\"):\n",
" # Post ID\n",
" post_id = p.get(\"id\")\n",
"def _create_ghost_post(jwt_token, admin_api_url, post_data):\n",
" # Get Admin API URL\n",
" admin_api_url = os.getenv(\"GHOST_ADMIN_API_URL\")\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)"
" headers = {\n",
" 'Authorization': f'Ghost {jwt_token}',\n",
" 'Content-Type': 'application/json'\n",
" }\n",
" \n",
" post_data = {\"posts\": [post_data]}\n",
"\n",
" response = requests.post(\n",
" os.path.join(admin_api_url, \"posts\"),\n",
" json=post_data,\n",
" headers=headers,\n",
" params={\"source\":\"html\"}\n",
" )\n",
"\n",
" if response.status_code == 201:\n",
" print(\"Ghost post published successfully\")\n",
" return response.json()\n",
" else:\n",
" print(\"Ghost - Failed to publish post: {} {}\".format(response.status_code, response.text))\n",
" return None\n",
"\n",
"if (PUBLISH_SAMPLE):\n",
" url_id = 150\n",
"\n",
" post_data = {\n",
" # \"slug\": \"hey-short\",\n",
" \"title\": \"Hey there, sample title\",\n",
" \"html\": \"<p>Hey there!</p>\",\n",
" # \"feature_image\": photo_url,\n",
" # \"feature_image_caption\": \"\",\n",
" \"status\": \"published\",\n",
" \"tags\": [\"#url-id-{}\".format(url_id)]\n",
" }\n",
"\n",
" # Publish post\n",
" payload = _create_ghost_post(jwt_token, admin_api_url, post_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Filter by post title\n",
"post_title = \"Funds raised for legal action over failure to stop grooming gangs\"\n",
"# Filter by published date\n",
"iso_time = (datetime.now(timezone.utc) - timedelta(hours=48)).strftime('%Y-%m-%dT%H:%M:%S') + 'Z'\n",
"# Parameter for filter\n",
"params = {\"filter\": \"title:'{}'+published_at:>{}\".format(post_title, iso_time)}\n",
"\n",
"# Filter by URL ID\n",
"url_id = 150\n",
"# Parameter for filter\n",
"params = {\"filter\": \"tags:hash-url-id-{}\".format(url_id)}\n",
"\n",
"# Get posts using filter\n",
"response = requests.get(os.path.join(admin_api_url, \"posts\"), params=params, headers=headers)\n",
"dict_response = response.json()\n",
"\n",
"len(dict_response.get(\"posts\"))"
]
}
],