konversi status
This commit is contained in:
parent
4232d84949
commit
2bad6764d1
3 changed files with 47 additions and 16 deletions
|
@ -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 `<p>` untuk memisahkan baris atau membuat new line, ada pula yang pakai `<br>
|
||||
2. Sedikit lebih lambat dibandingkan Deno TS?
|
||||
|
|
15
main.py
15
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"<p.*?>", "\n\n", str(notif.status))
|
||||
raw = html_p.replace("<p>", "\n")
|
||||
status_text = re.sub(r"<.*?>", "", raw)
|
||||
raw = str(notif.status)
|
||||
|
||||
# raw = raw.strip()
|
||||
raw = raw.replace("<p>", "", 1)
|
||||
raw = raw.rsplit("</p>", 1)[0]
|
||||
|
||||
rubah_p = re.sub(r"</p>", "\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()
|
||||
|
||||
|
||||
#
|
||||
|
|
45
main_test.py
45
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"<p.*?>", "\n\n", str(notif.status))
|
||||
raw = html_p.replace("<p>", "\n")
|
||||
html_content = re.sub(r"<.*?>", "", raw)
|
||||
print(html_content)
|
||||
|
||||
def buang_href(x):
|
||||
return re.sub(r'<a\s+(?:[^>]*?\s+)?href="([^"]*)">(.*?)</a>', 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())
|
||||
|
|
Loading…
Reference in a new issue