-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdouyin_digest.py
More file actions
297 lines (241 loc) · 10.2 KB
/
douyin_digest.py
File metadata and controls
297 lines (241 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python3
# Copyright (c) 2026 Nardo. AGPL-3.0 — see LICENSE
"""Douyin 医美 digest: search keywords via MediaCrawler,
rank by engagement, dedup, send top picks to Telegram thread 2.
Usage:
python douyin_digest.py # daily auto (cron)
python douyin_digest.py --force # ignore sent flag
Requires: TELEGRAM_BOT_TOKEN_XCN, MediaCrawler at ~/MediaCrawler/
"""
import asyncio
import json
import logging
import os
import subprocess
import sys
from datetime import datetime, timezone, timedelta
from pathlib import Path
BASE_DIR = Path(__file__).parent
sys.path.insert(0, str(BASE_DIR))
from dotenv import load_dotenv
load_dotenv(BASE_DIR / ".env")
log = logging.getLogger("douyin_digest")
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(name)s] %(message)s")
# ── Config ───────────────────────────────────────────────────────────────────
BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN_XCN", "")
GROUP_ID = -1003827304557
DOUYIN_THREAD = 2
KEYWORDS = ["上海面吸", "上海颌面", "上海眉弓peek", "成都颌面"]
PICKS_PER_KEYWORD = 3
HKT = timezone(timedelta(hours=8))
SENT_FLAG = str(BASE_DIR / ".douyin_digest_sent")
CACHE_FILE = str(BASE_DIR / ".douyin_digest_cache.json")
MEDIACRAWLER_DIR = os.path.expanduser("~/MediaCrawler")
MEDIACRAWLER_VENV = os.path.join(MEDIACRAWLER_DIR, "venv", "bin", "activate")
# ── Cache ────────────────────────────────────────────────────────────────────
def _load_cache() -> dict:
try:
with open(CACHE_FILE) as f:
return json.load(f)
except Exception:
return {"seen": []}
def _save_cache(cache: dict):
seen = cache.get("seen", [])
if len(seen) > 500:
cache["seen"] = seen[-300:]
with open(CACHE_FILE, "w") as f:
json.dump(cache, f, ensure_ascii=False)
# ── MediaCrawler runner ─────────────────────────────────────────────────────
def _run_mediacrawler(keyword: str) -> list:
"""Run MediaCrawler for a single keyword, parse JSONL output."""
log.info("Running MediaCrawler for Douyin keyword: %s", keyword)
import platform as plat
is_linux = plat.system() == "Linux"
xvfb_prefix = "xvfb-run -a " if is_linux else ""
cmd = (
f"source {MEDIACRAWLER_VENV} && "
f"cd {MEDIACRAWLER_DIR} && "
f"{xvfb_prefix}python main.py --platform dy --type search "
f"--keywords \"{keyword}\" --get_comment false --headless true"
)
try:
result = subprocess.run(
["bash", "-c", cmd],
capture_output=True, text=True, timeout=180,
)
if result.returncode != 0:
log.warning("MediaCrawler exit %d for '%s': %s",
result.returncode, keyword, result.stderr[-500:] if result.stderr else "")
except subprocess.TimeoutExpired:
log.error("MediaCrawler timeout for '%s'", keyword)
return []
except Exception as e:
log.error("MediaCrawler failed for '%s': %s", keyword, e)
return []
# Find today's JSONL output
today_str = datetime.now(HKT).strftime("%Y-%m-%d")
jsonl_dir = os.path.join(MEDIACRAWLER_DIR, "data", "douyin", "jsonl")
if not os.path.isdir(jsonl_dir):
log.warning("No JSONL dir: %s", jsonl_dir)
return []
posts = []
for fname in os.listdir(jsonl_dir):
if not fname.endswith(".jsonl"):
continue
if "contents" not in fname:
continue
if today_str not in fname:
continue
fpath = os.path.join(jsonl_dir, fname)
try:
with open(fpath, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
try:
item = json.loads(line)
# Only include items from this keyword
if item.get("source_keyword", "") == keyword:
posts.append(item)
except json.JSONDecodeError:
continue
except Exception as e:
log.warning("Error reading %s: %s", fpath, e)
log.info("Found %d Douyin posts for '%s'", len(posts), keyword)
return posts
# ── Ranking ──────────────────────────────────────────────────────────────────
def _engagement(post: dict) -> int:
"""Calculate total engagement: likes + comments + shares."""
liked = _safe_int(post.get("liked_count", 0))
comments = _safe_int(post.get("comment_count", 0))
shares = _safe_int(post.get("share_count", 0))
collected = _safe_int(post.get("collected_count", 0))
return liked + comments + shares + collected
def _safe_int(val) -> int:
try:
return int(val)
except (ValueError, TypeError):
return 0
def _pick_top(posts: list, cache: dict, n: int) -> list:
"""Pick top N unseen posts by engagement."""
seen = set(cache.get("seen", []))
unseen = [p for p in posts if p.get("aweme_id", "") not in seen]
unseen.sort(key=_engagement, reverse=True)
return unseen[:n]
# ── Telegram formatting ─────────────────────────────────────────────────────
def _format_post(post: dict) -> str:
"""Format a single Douyin post as HTML card."""
import html as html_mod
title = html_mod.escape(post.get("title", "") or post.get("desc", "")[:80])
if not title:
title = "(untitled)"
liked = _safe_int(post.get("liked_count", 0))
collected = _safe_int(post.get("collected_count", 0))
comments = _safe_int(post.get("comment_count", 0))
shares = _safe_int(post.get("share_count", 0))
nickname = html_mod.escape(post.get("nickname", ""))
aweme_url = post.get("aweme_url", "")
# Fallback URL from aweme_id
aweme_id = post.get("aweme_id", "")
if not aweme_url and aweme_id:
aweme_url = f"https://www.douyin.com/video/{aweme_id}"
desc = html_mod.escape((post.get("desc", "") or "")[:120])
stats = f"\u2764\ufe0f {liked} \u2b50 {collected} \U0001f4ac {comments} \U0001f501 {shares}"
parts = [
f"<b>{title[:60]}</b>",
f"\U0001f464 {nickname}" if nickname else "",
stats,
f"{desc}" if desc and desc != title else "",
f"\U0001f517 <a href=\"{aweme_url}\">View on Douyin</a>" if aweme_url else "",
]
return "\n".join(p for p in parts if p)
# ── Main ─────────────────────────────────────────────────────────────────────
async def main():
today = datetime.now(HKT).strftime("%Y-%m-%d")
force = "--force" in sys.argv
# Sent flag check
if not force and os.path.exists(SENT_FLAG):
with open(SENT_FLAG) as f:
if today in f.read():
log.info("Already ran for %s", today)
return
if not BOT_TOKEN:
log.error("No TELEGRAM_BOT_TOKEN_XCN")
return
cache = _load_cache()
all_messages = []
for keyword in KEYWORDS:
try:
posts = _run_mediacrawler(keyword)
if not posts:
log.warning("No posts for '%s'", keyword)
all_messages.append(f"\U0001f50d <b>{keyword}</b> (0 results)\n")
continue
picks = _pick_top(posts, cache, PICKS_PER_KEYWORD)
if not picks:
log.info("No new posts for '%s' (all seen)", keyword)
all_messages.append(f"\U0001f50d <b>{keyword}</b> (all seen)\n")
continue
# Build section
section = f"\U0001f50d <b>{keyword}</b> ({len(picks)} picks)\n\n"
for i, post in enumerate(picks, 1):
section += f"{i}. {_format_post(post)}\n\n"
# Mark as seen
aweme_id = post.get("aweme_id", "")
if aweme_id:
cache.setdefault("seen", []).append(aweme_id)
all_messages.append(section)
except Exception as e:
log.error("Error processing keyword '%s': %s", keyword, e)
all_messages.append(f"\U0001f50d <b>{keyword}</b> (\u274c error)\n")
_save_cache(cache)
if not all_messages:
log.info("No messages to send")
with open(SENT_FLAG, "a") as f:
f.write(today + "\n")
return
# Send to Telegram
from telegram import Bot
bot = Bot(token=BOT_TOKEN)
header = (
f"\U0001f3ac <b>Douyin \u533b\u7f8e Digest \u2014 {today}</b>\n"
f"{'─' * 30}\n\n"
)
full_text = header + "\n".join(all_messages)
# Split if needed (TG limit 4096)
chunks = _split_message(full_text, 4000)
for chunk in chunks:
try:
await bot.send_message(
chat_id=GROUP_ID,
message_thread_id=DOUYIN_THREAD,
text=chunk,
parse_mode="HTML",
disable_web_page_preview=True,
)
await asyncio.sleep(1)
except Exception as e:
log.error("Telegram send failed: %s", e)
log.info("Sent Douyin digest with %d keyword sections", len(KEYWORDS))
with open(SENT_FLAG, "a") as f:
f.write(today + "\n")
def _split_message(text: str, limit: int = 4000) -> list:
"""Split long text for Telegram."""
if len(text) <= limit:
return [text]
chunks = []
while text:
if len(text) <= limit:
chunks.append(text)
break
cut = text.rfind("\n\n", 0, limit)
if cut <= 0:
cut = text.rfind("\n", 0, limit)
if cut <= 0:
cut = limit
chunks.append(text[:cut])
text = text[cut:].lstrip("\n")
return chunks
if __name__ == "__main__":
asyncio.run(main())