import requests
from datetime import datetime, timedelta
from requests.auth import HTTPBasicAuth
# WordPress 網站資訊
WP_URL = “https://yestax.top/wp-json/wp/v2/posts”
WP_USER = “yestaxadmin”
WP_APP_PASS = “TWaw42451006”
# 文章內容
title = “自動發文測試”
content = “””
這是一篇自動發文文章
內容由 Python 程式透過 WordPress API 發布。
“””
# 設定未來 2 分鐘後自動發佈
future_time = (datetime.utcnow() + timedelta(minutes=2)).isoformat()
post = {
“title”: title,
“content”: content,
“status”: “future”, # ‘publish’ 立即發佈, ‘draft’ 草稿, ‘future’ 排程
“date”: future_time,
“categories”: [1], # WordPress 分類 ID
“tags”: [2] # WordPress 標籤 ID
}
# 發送請求
response = requests.post(
WP_URL,
auth=HTTPBasicAuth(WP_USER, WP_APP_PASS),
json=post
)
if response.status_code == 201:
print(“✅ 成功發佈文章”)
print(“文章連結:”, response.json().get(“link”))
else:
print(“❌ 發佈失敗:”, response.status_code, response.text)
