-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathauto_commenter.py
More file actions
596 lines (497 loc) · 25.5 KB
/
Copy pathauto_commenter.py
File metadata and controls
596 lines (497 loc) · 25.5 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
"""
auto_commenter.py -- Threads Timeline Fetcher (Affiliate Commenter)
"""
import json
import logging
import os
import re
import sys
import time
import random
from typing import Any, Dict, List, Optional
from urllib.parse import urlencode
import stats_tracker
import gemini_manager
import agent_loader
# WAJIB di paling atas untuk memuat semua variabel environment
from dotenv import load_dotenv
load_dotenv()
try:
from google import genai
_GENAI_AVAILABLE = True
except ImportError:
_GENAI_AVAILABLE = False
from services.session_manager import SessionManager, SessionStatus
from services.affiliate_strategy import get_random_affiliate_link
logging.basicConfig(level=logging.INFO, format="[%(levelname)s] %(message)s", stream=sys.stdout)
logger = logging.getLogger(__name__)
ANTI_AI_PROMPT = """
ATURAN GAYA BAHASA (ANTI-AI WRITING LAYER):
1. HARUS terdengar seperti pengguna Threads Indonesia asli yang sedang ngobrol kasual. DILARANG menggunakan gaya bahasa AI, Customer Support, Motivator, atau Interviewer.
2. DILARANG KERAS menggunakan frasa basi: "sangat menarik", "terima kasih telah berbagi", "apa yang membuat kamu", "saya setuju", "hal yang menarik", "menurut saya", "bagaimana pendapatmu", "bisakah kamu menjelaskan".
3. Gunakan kata gaul secukupnya secara natural (contoh: wkwk, anjir, relate banget, gue juga, jujur, kadang, sering banget, ngl). DILARANG memaksakan slang di setiap balasan.
4. STRUKTUR YANG DIHARAPKAN: Reaksi -> Opini -> Observasi Pribadi. DILARANG KERAS banyak bertanya.
5. Biarkan ada sedikit ketidaksempurnaan atau kata kasual. JANGAN gunakan tata bahasa formal/kaku.
"""
_gemini_client = None
_comment_history = []
THREADS_HOME = "https://www.threads.com/"
GRAPHQL_URL = "https://www.threads.com/graphql/query"
GRAPHQL_DUMP = "graphql_dump.json"
REPLY_ENDPOINT = "https://www.threads.com/api/v1/media/configure_text_only_post/"
AUTO_MODE = True
_TARGET_NAMES = {"BarcelonaFeedDirectQuery", "BarcelonaHomeTimeline", "BarcelonaFeedQuery"}
BASE_HEADERS = {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9",
"content-type": "application/x-www-form-urlencoded",
"origin": "https://www.threads.com",
"referer": "https://www.threads.com/",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
"x-ig-app-id": "238260118697367",
"x-asbd-id": "359341",
"x-fb-friendly-name": "BarcelonaFeedDirectQuery",
}
def parse_ai_reply(reply: str, product: Optional[Dict]) -> str:
# 1. Cek SKIP Intent
if reply.strip().upper() == "SKIP":
return "SKIP"
is_affiliate = False
cleaned_reply = reply.strip()
cleaned_reply_upper = cleaned_reply.upper()
# Deteksi intent dari string utuh (sebelum prefix dibersihkan)
if cleaned_reply_upper.startswith("AFFILIATE:"):
is_affiliate = True
elif cleaned_reply_upper.startswith("NO_AFFILIATE:"):
is_affiliate = False
else:
# Fallback if no prefix but contains link
if product and product.get('link_affiliate', '') in cleaned_reply:
is_affiliate = True
# Bersihkan semua variasi prefix di awal string agar tidak ter-post
cleaned_reply = re.sub(r'^NO_AFFILIATE:?\s*', '', cleaned_reply, flags=re.IGNORECASE)
cleaned_reply = re.sub(r'^AFFILIATE:?\s*', '', cleaned_reply, flags=re.IGNORECASE)
cleaned_reply = cleaned_reply.strip()
if not cleaned_reply:
return "SKIP"
# 2. Cek Banned Phrases (Post-Generation Validation)
banned_phrases = ["bener banget nih", "semangat terus ya", "kalau butuh cek ini", "saya rekomendasikan", "coba produk ini"]
if any(banned in cleaned_reply.lower() for banned in banned_phrases):
print(f"[VALIDATION FAILED] Terdeteksi frasa dilarang: {cleaned_reply}")
return "SKIP"
MAX_REPLY_LENGTH = 350
if is_affiliate and product:
link = product.get('link_affiliate', '')
# Hapus link yang digenerate AI (jika ada) agar tidak double
cleaned_reply = cleaned_reply.replace(link, "").strip()
if len(cleaned_reply) + len(link) + 1 > MAX_REPLY_LENGTH:
cleaned_reply = cleaned_reply[:MAX_REPLY_LENGTH - len(link) - 4] + "..."
# Tempelkan link secara paksa di akhir
cleaned_reply = f"{cleaned_reply} {link}"
else:
# Safeguard: Remove link if hallucinated under NO_AFFILIATE
if product and product.get('link_affiliate', '') in cleaned_reply:
cleaned_reply = cleaned_reply.replace(product.get('link_affiliate', ''), "").strip()
if len(cleaned_reply) > MAX_REPLY_LENGTH:
cleaned_reply = cleaned_reply[:MAX_REPLY_LENGTH - 3] + "..."
return cleaned_reply
def generate_reply(caption: str, product: Optional[Dict] = None) -> str:
global _gemini_client, _comment_history
total_keys = gemini_manager.get_total_keys()
if total_keys == 0:
return "(Error: GEMINI_API_KEY tidak ditemukan di .env)"
recent_comments = "\n".join([f"- {c}" for c in _comment_history]) if _comment_history else "(Belum ada komentar sebelumnya)"
base_prompt = agent_loader.load_agent('marketing-threads-affiliate-agent')
if base_prompt:
system_prompt = base_prompt.replace("{caption}", caption).replace("{recent_comments}", recent_comments)
else:
system_prompt = f"""Kamu adalah netizen Threads Indonesia yang asik, natural, dan manusiawi.
EVALUASI CAPTION BERIKUT: "{caption}"
TUGAS UTAMA:
Kamu WAJIB merespons dengan memilih SATU dari format berikut:
AFFILIATE:
<komentar>
ATAU
NO_AFFILIATE:
<komentar>
ATURAN PEMILIHAN FORMAT:
Gunakan "AFFILIATE:" HANYA JIKA caption menunjukkan salah satu:
- mencari rekomendasi barang
- ingin membeli sesuatu
- membandingkan produk
- meminta saran produk
- mencari solusi yang secara jelas berhubungan dengan produk
- review produk
- pertanyaan sebelum membeli
- kebutuhan yang dapat diselesaikan oleh produk tertentu
Gunakan "NO_AFFILIATE:" JIKA caption termasuk:
- nostalgia, meme, cerita sehari-hari, curhat, opini
- politik, agama, berita, duka
- posting umum, obrolan santai, pengalaman pribadi
- posting yang TIDAK menunjukkan intent membeli
ATURAN KOMENTAR (KEDUANYA):
- JIKA caption sangat tidak pantas dibalas (duka berat/politik kotor), balas dengan 1 kata: SKIP
- Balesan harus natural, nyambung dengan konteks caption, dan seperti manusia sungguhan.
- MAKSIMAL 2-3 kalimat santai.
- DILARANG KERAS menggunakan frasa filler basi ini: "Bener banget nih", "Semangat terus ya", "Kalau butuh cek ini", "Saya rekomendasikan", "Coba produk ini".
- DILARANG terdengar seperti AI, bot, sales marketing, atau motivator.
MEMORI KOMENTAR LAMA (Jangan pernah ulangi frasa dari komentar lama ini):
{recent_comments}
"""
if product:
system_prompt += f"""
JIKA KAMU MEMILIH "AFFILIATE:":
- Kamu hanya boleh menyebutkan nama produk ini secara natural: {product.get('nama', '')}
- DILARANG menulis link apapun di dalam komentarmu. Link akan ditambahkan otomatis oleh sistem.
JIKA KAMU MEMILIH "NO_AFFILIATE:":
- DILARANG menyebut produk.
- DILARANG menyebut link apapun.
- DILARANG mempromosikan apapun.
"""
else:
system_prompt += "\nBalaslah secara natural, maksimal 2 kalimat santai."
system_prompt += "\n" + ANTI_AI_PROMPT
last_exc = None
if gemini_manager.is_quota_locked():
logger.warning("[API] Quota lock aktif. Mengandalkan router (Groq fallback).")
try:
cached_reply = gemini_manager.check_cache(caption)
if cached_reply:
parsed_cache = parse_ai_reply(cached_reply, product)
if parsed_cache == "SKIP":
return "SKIP"
return parsed_cache
import llm_router
reply = llm_router.generate(system_prompt)
print("\n========== ROUTER DEBUG ==========")
print(f"[RAW LLM RESPONSE]: {reply}")
print("==================================\n")
reply = parse_ai_reply(reply, product)
if reply == "SKIP":
return "SKIP"
is_aff_logged = product and product.get('link_affiliate', '') in reply
print("\n========== FINAL DEBUG ==========")
print(f"[FINAL REPLY]: {reply}")
print(f"[FINAL LENGTH]: {len(reply)}")
print(f"[INTENT DETECTED]: {'AFFILIATE' if is_aff_logged else 'NO_AFFILIATE'}")
print("=================================\n")
gemini_manager.save_to_cache(caption, reply)
_comment_history.append(reply)
if len(_comment_history) > 3:
_comment_history.pop(0)
return reply
except Exception as exc:
stats_tracker.log_error(f"LLM Router Error: {exc}")
stats_tracker.increment("llm_router_errors")
return f"Error AI: {exc}"
def post_reply(session, post_id: str, text: str) -> bool:
csrf = (
session.cookies.get("csrftoken", domain=".threads.com")
or session.cookies.get("csrftoken")
or ""
)
headers = {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9",
"content-type": "application/x-www-form-urlencoded",
"origin": "https://www.threads.com",
"referer": "https://www.threads.com/",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"user-agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/124.0.0.0 Safari/537.36"
),
"x-ig-app-id": "238260118697367",
"x-csrftoken": csrf,
}
data = urlencode({
"caption": text,
"barcelona_source_reply_id": post_id,
"publish_mode": "text_post",
"upload_id": str(int(time.time() * 1000)),
"text_post_app_info": json.dumps({"reply_id": post_id, "entry_point": "create_reply", "text_with_entities": {"entities": [], "text": text}}),
})
try:
resp = session.post(REPLY_ENDPOINT, data=data, headers=headers, timeout=30)
return resp.status_code == 200
except Exception as e:
print(f"[REPLY_ERROR] {e}")
return False
def human_delay():
delay = random.randint(840, 1020) # 14-17 Menit
print(f"\n[INFO] Bot beristirahat {int(delay/60)} menit agar natural...")
time.sleep(delay)
def load_latest_query_config() -> Dict[str, str]:
# Default fallback dari capture yang sudah ada (hardcoded bypass)
DEFAULT_DOC_ID = "37542980015300902"
DEFAULT_VARIABLES = '{"data":{"pagination_source":"text_post_feed_threads","reason":"cold_start_fetch"},"variant":"for_you","__relay_internal__pv__BarcelonaIsLoggedInrelayprovider":true,"__relay_internal__pv__BarcelonaHasDearAlgoConsumptionrelayprovider":true,"__relay_internal__pv__BarcelonaHasEventBadgerelayprovider":false,"__relay_internal__pv__BarcelonaGenAIRepliesEnabledrelayprovider":false,"__relay_internal__pv__BarcelonaIsSearchDiscoveryEnabledrelayprovider":false,"__relay_internal__pv__BarcelonaHasCommunitiesrelayprovider":true,"__relay_internal__pv__BarcelonaHasGameScoreSharerelayprovider":false,"__relay_internal__pv__BarcelonaHasPublicViewCountCardrelayprovider":true,"__relay_internal__pv__BarcelonaHasCommunityEntityCardrelayprovider":true,"__relay_internal__pv__BarcelonaHasScorecardCommunityrelayprovider":true,"__relay_internal__pv__BarcelonaHasSportTeamAllegianceCardrelayprovider":false,"__relay_internal__pv__BarcelonaHasMusicrelayprovider":true,"__relay_internal__pv__BarcelonaHasNewspaperLinkStylerelayprovider":false,"__relay_internal__pv__BarcelonaMessagingHasGroupChatsrelayprovider":false,"__relay_internal__pv__BarcelonaHasMessagingrelayprovider":false,"__relay_internal__pv__BarcelonaHasViewerRepliedrelayprovider":false,"__relay_internal__pv__BarcelonaHasGhostPostEmojiActivationrelayprovider":false,"__relay_internal__pv__BarcelonaOptionalCookiesEnabledrelayprovider":true,"__relay_internal__pv__BarcelonaHasDearAlgoWebProductionrelayprovider":false,"__relay_internal__pv__BarcelonaHasWebFaviconsrelayprovider":false,"__relay_internal__pv__BarcelonaIsCrawlerrelayprovider":false,"__relay_internal__pv__BarcelonaHasCommunityTopContributorsrelayprovider":false,"__relay_internal__pv__BarcelonaCanSeeSponsoredContentrelayprovider":false,"__relay_internal__pv__BarcelonaShouldShowFediverseM075Featuresrelayprovider":true,"__relay_internal__pv__BarcelonaIsInternalUserrelayprovider":false}'
dumps = []
if os.path.exists(GRAPHQL_DUMP):
with open(GRAPHQL_DUMP, "r", encoding="utf-8") as f:
try:
dumps = json.load(f)
except json.JSONDecodeError:
dumps = []
if not isinstance(dumps, list):
dumps = [dumps] if dumps else []
for entry in dumps:
if not isinstance(entry, dict):
continue
payload = entry.get("request_payload") or {}
doc_id = entry.get("doc_id") or payload.get("doc_id", "")
friendly = entry.get("friendly_name") or payload.get("fb_api_req_friendly_name", "")
variables = payload.get("variables", "")
# Ekstrak dari list jika hasil parsing berupa list (ex: dari parse_qs)
if isinstance(doc_id, list): doc_id = doc_id[0] if doc_id else ""
if isinstance(friendly, list): friendly = friendly[0] if friendly else ""
if isinstance(variables, list): variables = variables[0] if variables else ""
# Pastikan variables menjadi string untuk payload GraphQL
variables_str = variables if isinstance(variables, str) else json.dumps(variables)
if friendly and friendly in _TARGET_NAMES and doc_id:
return {"doc_id": str(doc_id), "variables": variables_str}
if "pagination_source" in variables_str and "text_post_feed_threads" in variables_str and doc_id:
return {"doc_id": str(doc_id), "variables": variables_str}
# Jika tidak ketemu atau file tidak ada, gunakan default fallback yang sudah di-capture
print("\n[INFO] Menggunakan fallback GraphQL config bawaan (Default).")
return {"doc_id": DEFAULT_DOC_ID, "variables": DEFAULT_VARIABLES}
def get_fresh_tokens(session) -> Dict[str, str]:
tokens: Dict[str, str] = {"lsd": "", "fb_dtsg": ""}
try:
resp = session.get(THREADS_HOME, timeout=20)
for pat in [r'"LSD",\[\],\{"token":"([^"]+)"\}', r'"lsd":"([^"]+)"']:
m = re.search(pat, resp.text)
if m: tokens["lsd"] = m.group(1); break
for pat in [r'"DTSGInitialData",\[\],\{"token":"([^"]+)"\}', r'"fb_dtsg":"([^"]+)"']:
m = re.search(pat, resp.text)
if m: tokens["fb_dtsg"] = m.group(1); break
except Exception:
pass
return tokens
def fetch_timeline(session, tokens: Dict[str, str], query_config: Dict[str, str]) -> Optional[Dict[str, Any]]:
headers = dict(BASE_HEADERS)
friendly = query_config.get("friendly_name", "BarcelonaFeedDirectQuery")
headers["x-fb-friendly-name"] = friendly
csrf = session.cookies.get("csrftoken")
if csrf: headers["x-csrftoken"] = csrf
payload = urlencode({
"lsd": tokens.get("lsd", ""),
"fb_dtsg": tokens.get("fb_dtsg", ""),
"doc_id": query_config["doc_id"],
"variables": query_config["variables"],
"server_timestamps": "true",
"fb_api_req_friendly_name": friendly,
})
try:
resp = session.post(GRAPHQL_URL, data=payload, headers=headers, timeout=30)
print(f"[DEBUG] fetch_timeline status code: {resp.status_code}")
if resp.status_code != 200:
print(f"[DEBUG] fetch_timeline error response: {resp.text[:500]}")
return None
return json.loads(resp.text)
except Exception as e:
print(f"[DEBUG] fetch_timeline exception: {e}")
return None
def extract_posts(json_data: Dict[str, Any]) -> List[Dict[str, Any]]:
posts: List[Dict[str, Any]] = []
data = json_data.get("data") or {}
feed_data = data.get("feedData") or (data.get("viewer") or {}).get("home_feed_units")
if not feed_data:
print(f"[DEBUG] extract_posts: feedData atau home_feed_units tidak ditemukan. Keys di data: {list(data.keys()) if data else 'data is None'}")
return posts
edges = feed_data.get("edges", []) if isinstance(feed_data, dict) else []
for edge in edges:
items = (edge.get("node") or {}).get("text_post_app_thread", {}).get("thread_items", [])
if not items: continue
post = items[0].get("post") or {}
# --- METRICS DISCOVERY LOGGING ---
like_count = post.get("like_count")
text_post_app_info = post.get("text_post_app_info", {})
triage_comments = post.get("triage_comments")
reply_count = None
if isinstance(text_post_app_info, dict):
reply_count = text_post_app_info.get("direct_reply_count") or text_post_app_info.get("reply_count")
print(f"[POST_METRICS] like_count={like_count} | reply_count={reply_count}")
print(f"[TEXT_POST_APP_INFO] {text_post_app_info}")
print(f"[TRIAGE_COMMENTS] {triage_comments}")
print(f"[TREND_METRICS] likes={like_count} replies={reply_count}")
# ---------------------
user = post.get("user") or {}
posts.append({
"username": user.get("username") or "(unknown)",
"post_id": post.get("pk") or post.get("id") or "?",
"caption": (post.get("caption") or {}).get("text", ""),
"like_count": like_count,
"reply_count": reply_count,
"raw_post": post
})
return posts
def calculate_trend_score(likes: int, replies: int) -> int:
like_score = 0
if likes >= 300:
like_score = 30
elif likes >= 100:
like_score = 20
elif likes >= 30:
like_score = 10
reply_score = 0
if replies >= 30:
reply_score = 30
elif replies >= 10:
reply_score = 20
elif replies >= 3:
reply_score = 10
return like_score + reply_score
def process_timeline(session):
query_config = load_latest_query_config()
tokens = get_fresh_tokens(session)
print(f"[DEBUG] process_timeline: tokens fetched -> lsd={bool(tokens.get('lsd'))}, fb_dtsg={bool(tokens.get('fb_dtsg'))}")
raw = fetch_timeline(session, tokens, query_config)
if not raw:
print("[DEBUG] process_timeline: raw data dari fetch_timeline bernilai None atau kosong!")
return False
posts = extract_posts(raw)
if not posts:
print("[DEBUG] process_timeline: Tidak ada postingan yang berhasil diekstrak!")
return False
app_mode = os.getenv("APP_MODE", "normal").lower()
is_trend_candidate = False
if app_mode == "trend_affiliate":
from services.affiliate_strategy import find_relevant_product_with_score
trend_candidates = []
stats_tracker.increment("trend_posts_found")
for p in posts:
likes = p.get("like_count") or 0
replies = p.get("reply_count") or 0
if likes >= 30 and replies >= 3:
trend_score = calculate_trend_score(likes, replies)
_, relevance_score = find_relevant_product_with_score(p.get("caption", ""))
final_score = trend_score + relevance_score
trend_candidates.append({
"post": p,
"trend_score": trend_score,
"relevance_score": relevance_score,
"final_score": final_score
})
if trend_candidates:
trend_candidates.sort(key=lambda x: (x["final_score"], x["relevance_score"], x["trend_score"]), reverse=True)
top_candidate = trend_candidates[0]
stats_tracker.increment("trend_posts_selected")
stats_tracker.log_error(f"highest_trend_score:{top_candidate['trend_score']}")
stats_tracker.log_error(f"highest_final_score:{top_candidate['final_score']}")
p = top_candidate["post"]
print(f"\n[TREND_SCORE] {top_candidate['trend_score']}")
print(f"[TREND_LIKE_COUNT] {p.get('like_count', 0)}")
print(f"[TREND_REPLY_COUNT] {p.get('reply_count', 0)}")
print(f"[TREND_RELEVANCE_SCORE] {top_candidate['relevance_score']}")
print(f"[TREND_FINAL_SCORE] {top_candidate['final_score']}")
posts = [p]
is_trend_candidate = True
else:
stats_tracker.increment("trend_fallbacks")
print("[TREND_FALLBACK] No candidate passed criteria. Falling back to affiliate mode.")
random.shuffle(posts)
else:
random.shuffle(posts)
for p in posts[:1]:
username = p.get("username", "?")
post_id = p.get("post_id", "?")
caption = p.get("caption", "")
print(f"\n[INFO] Menemukan target: @{username}")
if is_trend_candidate:
from services.affiliate_strategy import find_relevant_product, generate_trend_affiliate_comment
product = find_relevant_product(caption)
if product:
stats_tracker.increment("trend_relevant_products_used")
else:
stats_tracker.increment("trend_random_products_used")
product = get_random_affiliate_link()
print(f"[TREND_FALLBACK_PRODUCT] selected_product={product.get('nama', 'Unknown')}")
print(f"[TREND_PRODUCT] {product.get('nama', '')}")
reply_text = generate_trend_affiliate_comment(caption, product, _comment_history)
if reply_text == "SKIP" or "(Gagal)" in reply_text:
print("[TREND_FALLBACK] AI generation failed. Falling back to normal.")
stats_tracker.increment("trend_fallbacks")
reply_text = generate_reply(caption, None)
else:
stats_tracker.increment("trend_affiliate_comments")
print("[TREND_LINK_INSERTED] Mandatory link inserted.")
elif app_mode == "affiliate" or (app_mode == "trend_affiliate" and not is_trend_candidate):
from services.affiliate_strategy import find_relevant_product, generate_affiliate_comment
# 1. Match relevant product
product = find_relevant_product(caption)
if product:
print(f" >> Relevant product found: {product.get('nama', '')}")
# 2. Decision: 50% Affiliate / 50% Normal
try:
reply_text = generate_affiliate_comment(caption, product, _comment_history)
if reply_text == "SKIP" or "(Gagal)" in reply_text:
print(" >> Affiliate generation failed/skipped. Fallback to normal.")
reply_text = generate_reply(caption, None)
except Exception as e:
print(f" >> Affiliate failsafe triggered: {e}")
reply_text = generate_reply(caption, None)
else:
print(" >> No relevant product found. 100% Normal Reply.")
reply_text = generate_reply(caption, None)
else:
product = get_random_affiliate_link()
reply_text = generate_reply(caption, product)
if reply_text == "SKIP":
print(" >> AI memutuskan untuk SKIP thread ini (Tidak relevan/Banned Phrase/No Intent).")
time.sleep(15) # Jeda pendek agar tidak spam request API Threads
continue
print("--------------------------------------------------")
print(f" DRAF AI: {reply_text}")
print("--------------------------------------------------")
if AUTO_MODE:
# === HOOK LIKE POST ===
try:
pass
except Exception as e:
# Error handling tingkat akhir agar tidak menghentikan proses komentar
print(f"[LIKE] Exception tidak tertangani: {e}")
# ======================
# --- DEBUG LOGGING ---
print("\n=== DEBUG: RAW POST STRUCTURE SEBELUM REPLY ===")
import json
# Safety fallback so json.dumps doesn't fail on complex objects
try:
print(json.dumps(p.get("raw_post", {}), indent=2))
except Exception as err:
print(f"Failed to dump JSON: {err}")
print(p.get("raw_post", {}))
print("===============================================\n")
# ---------------------
success = post_reply(session, str(post_id), reply_text)
if success:
print(" >> Status Pengiriman: Berhasil!")
stats_tracker.increment("comments_sent")
else:
print(" >> Status Pengiriman: Gagal.")
stats_tracker.increment("comment_failures")
human_delay()
return True
def main():
# Load .env variables explicit check for user
_ = os.getenv("SHOPEE_RAW_COOKIE") # Explicit load as requested
sm = SessionManager()
if not sm.load_session() or sm.validate_session().status != SessionStatus.VALID:
print("[ERROR] Session invalid. Cek THREADS_SESSION_ID dll di .env")
sys.exit(1)
session = sm.inject_session()
while True:
try:
if not process_timeline(session):
time.sleep(60)
except Exception as e:
stats_tracker.log_error(str(e))
logger.error("Error: %s", e)
time.sleep(60)
if __name__ == "__main__":
main()