From 2bad6764d10e9345712b214b26d5ac9569432934 Mon Sep 17 00:00:00 2001
From: Kusaeni
Date: Wed, 2 Oct 2024 12:39:37 +0700
Subject: [PATCH] konversi status
---
README.md | 3 ++-
main.py | 15 ++++++++++-----
main_test.py | 45 +++++++++++++++++++++++++++++++++++----------
3 files changed, 47 insertions(+), 16 deletions(-)
diff --git a/README.md b/README.md
index a39ef2b..de37483 100644
--- a/README.md
+++ b/README.md
@@ -40,5 +40,6 @@ Saat ini ada 4 environment variable yaitu:
4. TELE_CHATID : Chat Id dari channel telegram.
## Kekurangan
-1. Konversi dari html ke text/markdown masih buruk, perlu perbaikan disini
+1. ~Konversi dari html ke text/markdown masih buruk~, Sulit pakai Markdown karena bermasalah dengan entities, jadi pakai html (perlu perbaikan disini)
+2. Setiap instance punya cara sendiri - sendiri untuk menyimpan data status, ada yang pakai `` untuk memisahkan baris atau membuat new line, ada pula yang pakai `
2. Sedikit lebih lambat dibandingkan Deno TS?
diff --git a/main.py b/main.py
index 007e34c..409788d 100644
--- a/main.py
+++ b/main.py
@@ -126,9 +126,14 @@ async def kirim_ke_tele():
# html_strip = re.compile("<.*?>")
# status = str(notif.status)
# status_text = re.sub(html_strip, "", status)
- html_p = re.sub(r"
", "\n\n", str(notif.status))
- raw = html_p.replace("", "\n")
- status_text = re.sub(r"<.*?>", "", raw)
+ raw = str(notif.status)
+
+ # raw = raw.strip()
+ raw = raw.replace("
", "", 1)
+ raw = raw.rsplit("
", 1)[0]
+
+ rubah_p = re.sub(r"
", "\n\n", raw)
+ status_text = re.sub(r"<.*?>", "", rubah_p)
try:
await bot.send_message(
@@ -162,8 +167,8 @@ async def jalan():
await getNotif()
await asyncio.sleep(5)
await kirim_ke_tele()
- await asyncio.sleep(5)
- await tandai_notif()
+ # await asyncio.sleep(5)
+ # await tandai_notif()
#
diff --git a/main_test.py b/main_test.py
index d0ea597..97cbebf 100644
--- a/main_test.py
+++ b/main_test.py
@@ -1,22 +1,47 @@
+import asyncio
+import os
import re
-from sqlalchemy import desc
+import telegram
+from dotenv import load_dotenv
+from markdownify import markdownify
+from telegram.helpers import escape_markdown
from models import Notifikasi, Session
+load_dotenv()
+TELE_BOT = os.getenv("TELE_BOT")
+TELE_CHATID = os.getenv("TELE_CHATID")
+
session = Session()
+bot = telegram.Bot(token=f"{TELE_BOT}")
-def main_test():
- cek_data = (
- session.query(Notifikasi).order_by(desc(Notifikasi.created_at)).limit(10).all()
- )
+
+async def test():
+ cek_data = session.query(Notifikasi).filter(Notifikasi.id > 18).limit(2).all()
for notif in cek_data:
- html_p = re.sub(r"", "\n\n", str(notif.status))
- raw = html_p.replace("", "\n")
- html_content = re.sub(r"<.*?>", "", raw)
- print(html_content)
+
+ def buang_href(x):
+ return re.sub(r']*?\s+)?href="([^"]*)">(.*?)', r"\2", x)
+
+ y = buang_href(str(notif.status))
+ n = markdownify(y)
+ t = escape_markdown(n, version=2)
+
+ # print(t)
+
+ try:
+ await bot.send_message(
+ parse_mode="MarkdownV2",
+ chat_id=str(TELE_CHATID),
+ text=f"""
+ {t}
+ """,
+ )
+ except Exception as e:
+ print("Mentions: Ada masalah saat kirim ke Telebot", e)
-main_test()
+asyncio.run(test())