# 娛樂城論壇自動發文腳本 — 開發規格書

## 任務目標

在 VPS 上建立一套 Python 自動化腳本，可以：
1. 爬取指定來源的文章/資訊
2. 用 Claude API 改寫成原創文章
3. 透過 WP-CLI 自動發布到 WordPress
4. 設定 SEO 欄位（Focus Keyword、Meta Description、分類）

---

## 伺服器資訊

- **VPS**：Cloudways，SSH `master_nncjxcxymn@152.42.250.98`
- **WordPress 路徑**：`/home/1561033.cloudwaysapps.com/vyjtrkxqpm/public_html/`
- **腳本部署位置**：`/home/master_nncjxcxymn/auto_publish/`
- **WP-CLI**：`wp --path=/home/1561033.cloudwaysapps.com/vyjtrkxqpm/public_html/ --allow-root`
- **Python**：VPS 上已有 Python3（3.11）

---

## 腳本架構

```
auto_publish/
├── config.py          # API Key、設定、來源清單
├── crawler.py         # 爬蟲模組（抓來源文章）
├── rewriter.py        # Claude API 改寫模組
├── publisher.py       # WP-CLI 發佈模組
├── main.py            # 主程式（串起所有流程）
├── .env               # API Key（不進 git）
├── published.json     # 已發布清單（避免重複）
├── drafts/            # 草稿存放
├── logs/              # 執行日誌
└── raw_cache/         # 原始爬蟲快取（debug 用）
```

---

## 爬蟲來源清單（已測試可用）

| 來源 | URL | 分類 | CSS Selector |
|------|-----|------|-------------|
| wager.tw 部落格 | `https://wager.tw/page/blog` | 遊戲攻略 | `article` + `a[href*='/article/']` |
| wager.tw 娛樂城 | `https://wager.tw/page/casino` | 娛樂城評測 | `article` + `a[href*='/casino/']` |
| players.tw 評測 | `https://players.tw/category/evaluate/` | 娛樂城評測 | `article` + `.entry-title a` |
| players.tw 新聞 | `https://players.tw/news/` | 遊戲攻略 | `article` + `.entry-title a` |
| bestcasino.tw | `https://bestcasino.tw/casino/` | 娛樂城評測 | `article` + `h2 a` |
| Google News RSS | `https://news.google.com/rss/search?q=娛樂城攻略&hl=zh-TW` | 遊戲攻略 | RSS XML |

**注意**：
- 每次請求間隔 2-3 秒（避免被封）
- 原始內容存到 `raw_cache/` 供 debug
- 每來源每次最多抓 4 篇，每天發布上限 3 篇

---

## WordPress 分類 ID 對照

| 分類名稱 | ID |
|---------|-----|
| 遊戲攻略 | 9 |
| 娛樂城評測 | 8 |
| 娛樂城新聞 | 10 |
| 論壇公告 | 11 |

---

## Claude API 改寫 Prompt

每篇文章改寫後輸出格式：
```
第一行：JSON → {"post_title": "...", "meta_description": "...", "focus_keyword": "kw1,kw2,kw3"}
其餘：WordPress Gutenberg Block HTML
```

固定 CTA 區塊（每篇必須包含，放在 FAQ 之前）：
```html
<hr/>
<p><strong>準備好去玩了嗎？</strong>目前台灣玩家推薦度最高的平台是
<a href="https://ys89.fun/yse89-review/">夜色娛樂城（YS89）</a>，
新會員可免費領 <strong>NT$368 體驗金</strong>，首儲 100% 贈送、流水僅 1 倍。</p>
<div style="text-align:center;margin:20px 0">
  <a href="https://lihi2.me/LiqMX" target="_blank" ...>👉 立即領取 NT$368 體驗金</a>
</div>
<hr/>
```

---

## 模組函式簽名

```python
# crawler.py
def get_articles_from_source(source: dict, limit: int = 5) -> list[dict]
def get_article_content(url: str) -> str
def get_google_news(keyword: str, limit: int = 5) -> list[dict]
def fetch_all(limit_per_source: int = 4) -> list[dict]

# rewriter.py
def rewrite_article(title: str, content: str, keywords: list) -> dict
# 回傳：{post_title, post_content, meta_description, focus_keyword}

# publisher.py
def publish_post(title, content, category_id, focus_keyword, meta_description) -> int
# 回傳 post_id（失敗回傳 -1）
```

---

## 部署方式

### 安裝步驟（已完成）
```bash
# 1. 依賴已安裝
pip3 install anthropic requests beautifulsoup4 python-dotenv --break-system-packages

# 2. 建立 .env 檔案（需要用戶填入 API Key）
echo "ANTHROPIC_API_KEY=你的key" > ~/auto_publish/.env

# 3. 測試執行
python3 main.py --dry-run                          # 只改寫不發布
python3 main.py --single "https://目標文章網址"   # 對單篇執行
python3 main.py                                    # 正式發布
```

### Cron Job（已設定，每天早上 8 點）
```bash
0 8 * * * cd /home/master_nncjxcxymn/auto_publish && /usr/bin/python3 main.py >> logs/cron.log 2>&1
```

---

## 啟動前必做

1. 設定 API Key：
   ```bash
   ssh master_nncjxcxymn@152.42.250.98
   echo "ANTHROPIC_API_KEY=你的key" > ~/auto_publish/.env
   ```
2. 測試 dry-run：
   ```bash
   cd ~/auto_publish && python3 main.py --dry-run
   ```
3. 確認草稿內容正常後，移除 --dry-run 正式運行

---

## 安全注意事項

1. **API Key 存在 `.env`**，不要硬寫在程式碼或上傳 GitHub
2. **已發布清單** `published.json` 防止同一來源重複發布
3. **草稿存到 `drafts/`** 可手動審查，發現問題可改 Prompt
4. **爬蟲加 User-Agent**，模擬正常瀏覽器
5. **每日上限 3 篇**，避免大量低品質內容影響 SEO

---

## 完成標準

- [x] 所有 Python 模組已上傳 VPS：`~/auto_publish/`
- [x] Python 依賴已安裝（anthropic, requests, beautifulsoup4, python-dotenv）
- [x] Cron job 已設定（每天 08:00）
- [x] wager.tw 爬蟲已測試可抓到文章
- [ ] **待用戶提供**：在 `.env` 填入 `ANTHROPIC_API_KEY`
- [ ] 執行 `python3 main.py --dry-run` 驗證改寫效果
- [ ] 正式執行一次確認 WP 發布正常

---

*規格版本：v2.0 | 2026-05-06（已實際部署）*
