some MD stripping fixed

This commit is contained in:
abslimit 2020-09-20 14:59:11 +03:00
parent 2331ac05e3
commit 33c05d14e0

View File

@ -19,11 +19,8 @@ class BridgeBot:
def __init__(self, cfg: dict):
# RegExps
self.re_md_links = re.compile(r'\[(.+)\]\((https?:\/\/[\w\d./?=#&-]+)\)')
self.re_md_bold = re.compile(r'\*\*(.+)\*\*')
self.re_md_italic = re.compile(r'__(.+)__')
self.re_md_strike = re.compile(r'~~(.+)~~')
self.re_md_mono = re.compile(r'`(.+)`')
self.re_md_links = re.compile(r'\[([\w\d\s\-,?!.]+)\]\((https?:\/\/[\w\d./?=#&-]+)\)')
self.re_md_media_link = re.compile(r'^\[\]\(https?:\/\/telegra.ph[\w\d./?=#&-]+\)')
# Config init
self.config = cfg
self.mastodon_clients = {}
@ -74,11 +71,12 @@ class BridgeBot:
mstdn_post_limit = 500
#full_text = event.message.raw_text
full_text = event.message.text
full_text = re.sub(self.re_md_media_link, '', full_text)
full_text = re.sub(self.re_md_links, r'\g<1> \g<2>', full_text)
full_text = re.sub(self.re_md_bold, r'\g<1>', full_text)
full_text = re.sub(self.re_md_italic, r'\g<1>', full_text)
full_text = re.sub(self.re_md_strike, r'\g<1>', full_text)
full_text = re.sub(self.re_md_mono, r'\g<1>', full_text)
full_text = full_text.replace('**', '')
full_text = full_text.replace('__', '')
full_text = full_text.replace('~~', '')
full_text = full_text.replace('`', '')
# URL of Telegram message
tg_message_url = f"[https://t.me/{channel.username}/" + str(event.message.id) + "]\n\n"
if event.message.file and not (event.message.photo or event.message.video or event.message.gif):
@ -123,11 +121,15 @@ class BridgeBot:
reply_to = current_mastodon_client.status_post(post_text, media_ids=[mstdn_media_meta], visibility=current_mstdn_acc_visibility)
tg_message_url = f"[Продолжение https://t.me/{channel.username}/" + str(event.message.id) + "]\n\n"
# Chunking post into mstdn limit chunks and reply to root post
i = 0
while reply_start + mstdn_post_limit < full_text_size:
reply_end = full_text.rfind(' ', reply_start, reply_start + mstdn_post_limit - len(tg_message_url))
post_text: str = tg_message_url + full_text[reply_start+1:reply_end]
reply_to = current_mastodon_client.status_post(post_text, in_reply_to_id=reply_to, visibility=current_mstdn_acc_visibility)
reply_start = reply_end
i = i + 1
if i > 15:
break
# Final chunk to reply to root post
if reply_start > 0:
post_text: str = tg_message_url + full_text[reply_start+1:full_text_size]