bot_v4 / utils /markdown.py
qqqsfasdf's picture
Upload 70 files
036b534 verified
# ============================================
# utils/markdown.py — MarkdownV2 helpers
# ============================================
import re
def escape(text: str) -> str:
if not text:
return ""
return re.sub(r'([_*\[\]()~`>#+\-=|{}.!\\])', r'\\\1', str(text))
def bold(text: str) -> str:
return f"*{escape(text)}*"
def italic(text: str) -> str:
return f"_{escape(text)}_"
def code(text: str) -> str:
return f"`{escape(text)}`"
def link(label: str, url: str) -> str:
return f"[{escape(label)}]({url})"
def build_game_info_message(details: dict, game_link_in_lib: str | None = None) -> str:
"""
بناء رسالة تفاصيل اللعبة الكاملة بتنسيق MarkdownV2.
الترتيب المطلوب:
[رابط صورة اللعبة كرابط في أول السطر]
اسم اللعبة
المعلومات الأساسية
التصنيفات والخصائص
ملخص القصة (3 أسطر)
لماذا نضع رابط الصورة أولاً كنص عادي؟
Telegram يُظهر preview للرابط الأول في الرسالة.
عندما يكون رابط صورة IGDB، يظهر كبطاقة مرئية أعلى النص.
"""
parts = []
# ── رابط الصورة في أول السطر (يولّد preview تلقائياً) ──
cover_url = details.get("cover_url", "")
if cover_url:
parts.append(cover_url)
parts.append("")
# ── اسم اللعبة ──
parts.append(f"🎮 *{escape(details.get('name', ''))}*")
parts.append("")
# ── المعلومات الأساسية ──
if details.get("release_date"):
parts.append(f"📅 *تاريخ الإصدار:* {escape(details['release_date'])}")
if details.get("developer"):
parts.append(f"🏢 *المطور:* {escape(details['developer'])}")
if details.get("publisher") and details["publisher"] != details.get("developer"):
parts.append(f"📦 *الناشر:* {escape(details['publisher'])}")
if details.get("platforms_str"):
parts.append(f"🖥 *المنصات:* {escape(details['platforms_str'])}")
# التقييمات
ur = details.get("user_rating", "")
uc = details.get("user_rating_count", 0)
cr = details.get("crit_rating", "")
cc = details.get("crit_rating_count", 0)
if ur or cr:
rating_parts = []
if ur:
rating_parts.append(f"{escape(ur)} \\(بناء على {escape(str(uc))} مستخدم\\)")
if cr:
rating_parts.append(f"{escape(cr)} \\(بناء على {escape(str(cc))} نقد\\)")
parts.append(f"⭐ *التقييمات:* {' — '.join(rating_parts)}")
if details.get("age_rating"):
parts.append(f"🔞 *التصنيف العمري:* {escape(details['age_rating'])}")
if details.get("languages"):
parts.append(f"🌐 *اللغات المدعومة:* {escape(details['languages'])}")
parts.append("")
# ── التصنيفات والخصائص ──
if details.get("genres_ar"):
parts.append(f"🎭 *الأنواع:* {escape(details['genres_ar'])}")
if details.get("themes_ar"):
parts.append(f"🌊 *الثيمات:* {escape(details['themes_ar'])}")
if details.get("game_modes_ar"):
parts.append(f"🕹 *أنماط اللعب:* {escape(details['game_modes_ar'])}")
if details.get("perspectives_ar"):
parts.append(f"👁 *منظور اللاعب:* {escape(details['perspectives_ar'])}")
if details.get("engine"):
parts.append(f"⚙️ *المحرك:* {escape(details['engine'])}")
# ── ملخص القصة ──
if details.get("summary"):
parts.append("")
parts.append(f"📖 *نبذة عن القصة:*")
parts.append(escape(details["summary"]))
# ── رابط التعريب في المكتبة ──
if game_link_in_lib:
parts.append("")
parts.append(f"🎌 {link('شاهد التعريب في مكتبتنا', game_link_in_lib)}")
return "\n".join(parts)
def build_pc_specs_message(game_name: str, pc_specs: dict) -> str:
"""رسالة متطلبات التشغيل (PC) فقط"""
min_specs = pc_specs.get("minimum", {})
rec_specs = pc_specs.get("recommended", {})
parts = []
parts.append(f"🖥 *متطلبات التشغيل — {escape(game_name)}*")
parts.append("")
if min_specs:
parts.append(f"*\\[ الحد الأدنى — Minimum \\]*")
for k, v in min_specs.items():
if v:
parts.append(f"• {escape(k)}: {escape(str(v))}")
parts.append("")
if rec_specs:
parts.append(f"*\\[ الموصى به — Recommended \\]*")
for k, v in rec_specs.items():
if v:
parts.append(f"• {escape(k)}: {escape(str(v))}")
if not min_specs and not rec_specs:
parts.append(f"{escape('لم يتم العثور على متطلبات رسمية لهذه اللعبة.')}")
return "\n".join(parts)
def build_new_post_notification(
game_title: str, platform: str,
game_link: str, channel_username: str
) -> str:
emoji = "🖥" if platform == "pc" else "🎮" if platform == "ps" else "📁"
plat_ar = "PC" if platform == "pc" else "PlayStation" if platform == "ps" else "عام"
ch_part = f"\n📢 @{escape(channel_username)}" if channel_username else ""
return (
f"🔔 *تعريب جديد\\!*\n\n"
f"🎮 *{escape(game_title)}*\n"
f"{emoji} المنصة\\: {escape(plat_ar)}\n"
f"🔗 {link('شاهد التعريب', game_link)}"
f"{ch_part}"
)
def build_update_notification(
game_title: str, update_note: str,
game_link: str, channel_username: str
) -> str:
ch_part = f"\n📢 @{escape(channel_username)}" if channel_username else ""
note_part = f"\n✏️ {escape(update_note)}" if update_note else ""
return (
f"🔄 *تم تحديث تعريب\\!*\n\n"
f"🎮 *{escape(game_title)}*"
f"{note_part}\n"
f"🔗 {link('شاهد التحديث', game_link)}"
f"{ch_part}"
)