Spaces:
Paused
Paused
Update routers/wordpress.py
Browse files- routers/wordpress.py +17 -8
routers/wordpress.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import httpx
|
| 3 |
from fastapi import APIRouter, HTTPException
|
|
|
|
| 4 |
|
| 5 |
router = APIRouter()
|
| 6 |
|
|
@@ -14,17 +15,25 @@ if not WP_URL or not WP_USER or not WP_PASS:
|
|
| 14 |
|
| 15 |
AUTH = (WP_USER, WP_PASS)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# 🚀 Criar post no WordPress
|
| 18 |
@router.post("/wordpress/post")
|
| 19 |
-
async def create_wordpress_post(
|
| 20 |
async with httpx.AsyncClient(timeout=20.0) as client:
|
| 21 |
-
# Se houver imagem → baixar e enviar como mídia destacada
|
| 22 |
featured_media_id = None
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
try:
|
| 25 |
-
img_resp = await client.get(image_url, timeout=15.0)
|
| 26 |
if img_resp.status_code == 200:
|
| 27 |
-
filename = image_url.split("/")[-1].split("?")[0]
|
| 28 |
media_headers = {
|
| 29 |
"Content-Disposition": f"attachment; filename={filename}",
|
| 30 |
"Content-Type": img_resp.headers.get("content-type", "image/jpeg")
|
|
@@ -45,9 +54,9 @@ async def create_wordpress_post(title: str, content: str, image_url: str = None,
|
|
| 45 |
|
| 46 |
# Criar o post
|
| 47 |
post_data = {
|
| 48 |
-
"title": title,
|
| 49 |
-
"content": content,
|
| 50 |
-
"status": status
|
| 51 |
}
|
| 52 |
if featured_media_id:
|
| 53 |
post_data["featured_media"] = featured_media_id
|
|
|
|
| 1 |
import os
|
| 2 |
import httpx
|
| 3 |
from fastapi import APIRouter, HTTPException
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
|
| 6 |
router = APIRouter()
|
| 7 |
|
|
|
|
| 15 |
|
| 16 |
AUTH = (WP_USER, WP_PASS)
|
| 17 |
|
| 18 |
+
# 📦 Modelo de entrada
|
| 19 |
+
class WPPost(BaseModel):
|
| 20 |
+
title: str
|
| 21 |
+
content: str
|
| 22 |
+
image_url: str | None = None
|
| 23 |
+
status: str = "publish"
|
| 24 |
+
|
| 25 |
# 🚀 Criar post no WordPress
|
| 26 |
@router.post("/wordpress/post")
|
| 27 |
+
async def create_wordpress_post(post: WPPost):
|
| 28 |
async with httpx.AsyncClient(timeout=20.0) as client:
|
|
|
|
| 29 |
featured_media_id = None
|
| 30 |
+
|
| 31 |
+
# Se houver imagem → baixar e enviar como mídia
|
| 32 |
+
if post.image_url:
|
| 33 |
try:
|
| 34 |
+
img_resp = await client.get(post.image_url, timeout=15.0)
|
| 35 |
if img_resp.status_code == 200:
|
| 36 |
+
filename = post.image_url.split("/")[-1].split("?")[0]
|
| 37 |
media_headers = {
|
| 38 |
"Content-Disposition": f"attachment; filename={filename}",
|
| 39 |
"Content-Type": img_resp.headers.get("content-type", "image/jpeg")
|
|
|
|
| 54 |
|
| 55 |
# Criar o post
|
| 56 |
post_data = {
|
| 57 |
+
"title": post.title,
|
| 58 |
+
"content": post.content,
|
| 59 |
+
"status": post.status
|
| 60 |
}
|
| 61 |
if featured_media_id:
|
| 62 |
post_data["featured_media"] = featured_media_id
|