masih mencari skema yang benar untuk html ke markdown

This commit is contained in:
Kusaeni 2024-10-01 15:05:12 +07:00
parent 2f29314faa
commit 4232d84949
3 changed files with 42 additions and 13 deletions

View file

@ -18,7 +18,8 @@ App ini masih dalam pengembangan dan banyak hal yang belum bisa dilakukan, berik
- [x] Tandai data di database jika sudah terkirim - [x] Tandai data di database jika sudah terkirim
- [x] Delay antar fungsi - [x] Delay antar fungsi
- [ ] Cron internal - [ ] Cron internal
- [ ] Build - [ ] ~Build~
- [ ] Benahi html ke markdown, karena pakai parse `MarkdownV2`
## Install ## Install
Clone repositori ini dan install di lokal. Clone repositori ini dan install di lokal.
@ -37,3 +38,7 @@ Saat ini ada 4 environment variable yaitu:
2. GTS_TOKEN : Token dengan scope minimal "Read" untuk akses API gotosocial 2. GTS_TOKEN : Token dengan scope minimal "Read" untuk akses API gotosocial
3. TELE_BOT : Token Bot Telegram, hubungi Bot Father untuk minta 3. TELE_BOT : Token Bot Telegram, hubungi Bot Father untuk minta
4. TELE_CHATID : Chat Id dari channel telegram. 4. TELE_CHATID : Chat Id dari channel telegram.
## Kekurangan
1. Konversi dari html ke text/markdown masih buruk, perlu perbaikan disini
2. Sedikit lebih lambat dibandingkan Deno TS?

26
main.py
View file

@ -16,24 +16,23 @@ from models import Notifikasi, Session
# Request ke kauaku # Request ke kauaku
load_dotenv() load_dotenv()
gts_api = os.getenv("GTS_API") GTS_API = os.getenv("GTS_API")
gts_token = os.getenv("GTS_TOKEN") GTS_TOKEN = os.getenv("GTS_TOKEN")
TELE_BOT = os.getenv("TELE_BOT")
TELE_CHATID = os.getenv("TELE_CHATID")
headers = {"Authorization": f"Bearer {gts_token}"} headers = {"Authorization": f"Bearer {GTS_TOKEN}"}
session = Session() session = Session()
TELE_BOT = os.getenv("TELE_BOT")
TELE_CHATID = os.getenv("TELE_CHATID")
bot = telegram.Bot(token=f"{TELE_BOT}") bot = telegram.Bot(token=f"{TELE_BOT}")
async def getNotif(): async def getNotif():
req = requests.get(f"{gts_api}?limit=5", headers=headers) req = requests.get(f"{GTS_API}?limit=5", headers=headers)
if req.status_code == 200: if req.status_code == 200:
data = req.json() data = req.json()
print(data.__class__)
try: try:
for notif in data: for notif in data:
type = notif["type"] type = notif["type"]
@ -118,22 +117,25 @@ async def kirim_ke_tele():
parse_mode="html", parse_mode="html",
chat_id=str(TELE_CHATID), chat_id=str(TELE_CHATID),
text=f""" text=f"""
<b>{notif.display_name}</b>\n<i>{notif.handler}</i>\n{flag}\n from python <b>{notif.display_name}</b>\n<i>{notif.handler}</i>\n{flag}
""", """,
) )
except Exception as e: except Exception as e:
print("Follow: Ada masalah saat kirim ke Telebot", e) print("Follow: Ada masalah saat kirim ke Telebot", e)
else: else:
html_strip = re.compile("<.*?>") # html_strip = re.compile("<.*?>")
status = str(notif.status) # status = str(notif.status)
status_text = re.sub(html_strip, "", 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)
try: try:
await bot.send_message( await bot.send_message(
parse_mode="html", parse_mode="html",
chat_id=str(TELE_CHATID), chat_id=str(TELE_CHATID),
text=f""" text=f"""
<b>{notif.display_name}</b>\n<i>{notif.handler}</i>\n{flag}\n\n{status_text}\nfrom python\n{notif.url} <b>{notif.display_name}</b>\n<i>{notif.handler}</i>\n{flag}\n\n{status_text}\n\n<a href="{notif.url}">source</a>
""", """,
) )
except Exception as e: except Exception as e:

22
main_test.py Normal file
View file

@ -0,0 +1,22 @@
import re
from sqlalchemy import desc
from models import Notifikasi, Session
session = Session()
def main_test():
cek_data = (
session.query(Notifikasi).order_by(desc(Notifikasi.created_at)).limit(10).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)
main_test()