Skip to content

Commit 1a1e880

Browse files
committed
refactor: replace print with logging and document production deploy
1 parent 505e226 commit 1a1e880

9 files changed

Lines changed: 142 additions & 62 deletions

File tree

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,55 @@ curl http://localhost:8765/api/health
255255
curl http://localhost:8765/api/info
256256
```
257257

258+
### 6.5 生产部署 / 反向代理 / HTTPS
259+
260+
如果你要把它部署给别人使用,推荐的最小方案是:
261+
262+
1. ContentPipe 只监听内网或 Docker 网络
263+
2. 用 Nginx / Caddy 做反向代理
264+
3. 打开 `CONTENTPIPE_AUTH_TOKEN`
265+
4. 通过 HTTPS 暴露外部访问
266+
5.`CONTENTPIPE_PUBLIC_BASE_URL` 设成最终对外域名
267+
268+
示例(Nginx):
269+
270+
```nginx
271+
server {
272+
listen 80;
273+
server_name contentpipe.example.com;
274+
return 301 https://$host$request_uri;
275+
}
276+
277+
server {
278+
listen 443 ssl http2;
279+
server_name contentpipe.example.com;
280+
281+
ssl_certificate /path/to/fullchain.pem;
282+
ssl_certificate_key /path/to/privkey.pem;
283+
284+
client_max_body_size 25m;
285+
286+
location / {
287+
proxy_pass http://127.0.0.1:8765;
288+
proxy_set_header Host $host;
289+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
290+
proxy_set_header X-Forwarded-Proto $scheme;
291+
proxy_set_header Upgrade $http_upgrade;
292+
proxy_set_header Connection "upgrade";
293+
}
294+
}
295+
```
296+
297+
对应 `.env` 建议:
298+
299+
```bash
300+
CONTENTPIPE_AUTH_TOKEN=change-me
301+
CONTENTPIPE_PUBLIC_BASE_URL=https://contentpipe.example.com
302+
OPENCLAW_GATEWAY_URL=http://host.docker.internal:18789
303+
```
304+
305+
如果只在本机使用,可以不配反代和 HTTPS;但**只要要给别人访问,就建议必须开 HTTPS + 鉴权**
306+
258307
---
259308

260309
## 7. Web UI

scripts/env_loader.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
import os
1212
from pathlib import Path
1313

14+
from logutil import get_logger
15+
16+
17+
logger = get_logger(__name__)
1418

1519
OPENCLAW_CONFIG_PATHS = [
1620
Path.home() / ".clawdbot" / "openclaw.json",
@@ -82,8 +86,8 @@ def load_keys_from_openclaw() -> dict[str, str]:
8286
if __name__ == "__main__":
8387
keys = load_keys_from_openclaw()
8488
if keys:
85-
print(f"✅ 从 OpenClaw 加载了 {len(keys)} 个 API keys:")
89+
logger.info("从 OpenClaw 加载了 %s 个 API keys", len(keys))
8690
for k, v in keys.items():
87-
print(f" {k} = {v}")
91+
logger.info("%s = %s", k, v)
8892
else:
89-
print("❌ 未找到 OpenClaw 配置或无可用 keys")
93+
logger.warning("未找到 OpenClaw 配置或无可用 keys")

scripts/formatter.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
import jinja2
2323
import yaml
2424

25+
from logutil import get_logger
26+
27+
logger = get_logger(__name__)
28+
2529

2630
SKILL_DIR = Path(__file__).parent.parent
2731
CONFIG_DIR = SKILL_DIR / "config"
@@ -417,7 +421,7 @@ def format_article(run_id: str, output_dir: Path, platform: str = "wechat") -> s
417421
(output_dir / "formatted.html").write_text(html, encoding="utf-8")
418422
(output_dir / "content_body.html").write_text(content_html, encoding="utf-8")
419423

420-
print(f"📐 Formatted: {len(html)} chars, {len(image_map)} images, template={template_name}")
424+
logger.info("Formatted: %s chars, %s images, template=%s", len(html), len(image_map), template_name)
421425
return html
422426

423427

@@ -434,7 +438,7 @@ def format_article(run_id: str, output_dir: Path, platform: str = "wechat") -> s
434438
output_dir = DEFAULT_OUTPUT_BASE / args.run_id
435439

436440
if not output_dir.exists():
437-
print(f"❌ Output dir not found: {output_dir}", file=sys.stderr)
441+
logger.error("Output dir not found: %s", output_dir)
438442
sys.exit(1)
439443

440444
format_article(args.run_id, output_dir, args.platform)

scripts/hot_news.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
import sys
2525
from typing import Any
2626

27+
from logutil import get_logger
28+
29+
logger = get_logger(__name__)
30+
2731
import httpx
2832

2933
HEADERS = {
@@ -54,7 +58,7 @@ def fetch_baidu(top: int = 30) -> list[dict]:
5458
for i, it in enumerate(items[:top]) if isinstance(it, dict) and it.get("word")
5559
]
5660
except Exception as e:
57-
print(f"⚠️ 百度: {e}", file=sys.stderr)
61+
logger.warning("百度: %s", e)
5862
return []
5963

6064

@@ -98,12 +102,12 @@ def fetch_twitter(top: int = 15, keywords: list[str] | None = None) -> list[dict
98102
"retweets": tweet.get("retweet_count", 0),
99103
})
100104
except FileNotFoundError:
101-
print("⚠️ xreach 未安装,跳过 Twitter", file=sys.stderr)
105+
logger.warning("xreach 未安装,跳过 Twitter")
102106
return results
103107
except subprocess.TimeoutExpired:
104-
print(f"⚠️ xreach 超时: {query}", file=sys.stderr)
108+
logger.warning("xreach 超时: %s", query)
105109
except Exception as e:
106-
print(f"⚠️ Twitter ({query}): {e}", file=sys.stderr)
110+
logger.warning("Twitter (%s): %s", query, e)
107111

108112
# 去重
109113
seen = set()
@@ -137,7 +141,7 @@ def fetch_weibo(top: int = 20) -> list[dict]:
137141
if it.get("word")
138142
]
139143
except Exception as e:
140-
print(f"⚠️ 微博: {e}", file=sys.stderr)
144+
logger.warning("微博: %s", e)
141145
return []
142146

143147

@@ -168,7 +172,7 @@ def fetch_zhihu(top: int = 20) -> list[dict]:
168172
})
169173
return results
170174
except Exception as e:
171-
print(f"⚠️ 知乎: {e}", file=sys.stderr)
175+
logger.warning("知乎: %s", e)
172176
return []
173177

174178

@@ -198,7 +202,7 @@ def fetch_via_jina(url: str, platform: str = "jina") -> list[dict]:
198202
})
199203
return results
200204
except Exception as e:
201-
print(f"⚠️ Jina ({url}): {e}", file=sys.stderr)
205+
logger.warning("Jina (%s): %s", url, e)
202206
return []
203207

204208

@@ -242,7 +246,7 @@ def fetch_tophub(board_id: str, platform_name: str, top: int = 30) -> list[dict]
242246
results[-1]["heat"] = int(hm.group(1)) * 10000
243247
return results
244248
except Exception as e:
245-
print(f"⚠️ tophub ({platform_name}): {e}", file=sys.stderr)
249+
logger.warning("tophub (%s): %s", platform_name, e)
246250
return []
247251

248252

@@ -282,7 +286,7 @@ def fetch_all(
282286

283287
total = sum(len(v) for v in results.values())
284288
parts = [f"{k} {len(v)}" for k, v in results.items() if v]
285-
print(f"🔥 热搜: {total} 条 ({', '.join(parts) or '全部失败'})", file=sys.stderr)
289+
logger.info("热搜: %s 条 (%s)", total, ", ".join(parts) or "全部失败")
286290
return results
287291

288292

scripts/image_engines/api_pollinations.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import httpx
2424

2525
from .base import ImageEngine, ImageResult
26+
from logutil import get_logger
27+
28+
logger = get_logger(__name__)
2629

2730

2831
class PollinationsEngine(ImageEngine):
@@ -187,7 +190,7 @@ def _translate_prompt(chinese_prompt: str) -> str:
187190
if translated and len(translated) > 10:
188191
return translated
189192
except Exception as e:
190-
print(f" ⚠️ Prompt translation failed: {e}")
193+
logger.warning("Prompt translation failed: %s", e)
191194

192195
# Fallback: 原样返回(Pollinations 也能处理部分中文)
193196
return chinese_prompt

scripts/jimeng.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616

1717
import httpx
1818

19+
from logutil import get_logger
20+
1921
GATEWAY_URL = "http://localhost:18789"
2022
JIMENG_TAB_ID = "633824CAA4F7CEFB0C523AFB972A9E08" # 默认即梦 tab
2123

24+
logger = get_logger(__name__)
25+
2226

2327
def _browser_action(action: str, **kwargs) -> dict:
2428
"""调用 OpenClaw Gateway 的 browser tool"""
@@ -139,11 +143,11 @@ def get_highres_url(thumbnail_url: str, size: int = 2048) -> str:
139143
if __name__ == "__main__":
140144
import sys
141145
prompt = sys.argv[1] if len(sys.argv) > 1 else "一只可爱的卡通猫咪在花园里玩耍"
142-
print(f"Prompt: {prompt}")
146+
logger.info("Prompt: %s", prompt)
143147
try:
144148
urls = generate_images(prompt)
145-
print(f"✅ 生成 {len(urls)} 张图片:")
149+
logger.info("生成 %s 张图片", len(urls))
146150
for i, url in enumerate(urls):
147-
print(f" [{i+1}] {url[:100]}...")
151+
logger.info("[%s] %s...", i+1, url[:100])
148152
except Exception as e:
149-
print(f"❌ {e}")
153+
logger.error("%s", e)

0 commit comments

Comments
 (0)