all repos — gif-escarbot @ 35dbea7b1ab1a87b2054c51d6c268aef6629cf31

Earthbound Café's custom delivery bot with other cool utilities built-in.

add timestamp to replacer
Marco Andronaco andronacomarco@gmail.com
Fri, 25 Aug 2023 23:44:01 +0200
commit

35dbea7b1ab1a87b2054c51d6c268aef6629cf31

parent

f8e2e98680716dabf1b2999c63a8cfd85c9d8de1

1 files changed, 39 insertions(+), 4 deletions(-)

jump to
M Replacer.pyReplacer.py

@@ -7,10 +7,37 @@ logger = logging.getLogger(__name__)

re_flags = re.I | re.M +def get_human_readable(input_str: str, indicator: str, offset: int = 0) -> (int, int): + try: + result = int(input_str[offset:].split(indicator, maxsplit=1)[0]) + return result, len(str(result)) + len(indicator) + except ValueError: + return 0, 0 + +youtube_timestamp_regex = re.compile(r"(?:&|\?)t=(\d*h?\d*m?\d*s?)", re_flags) +def youtube_timestamp(input_str: str) -> str: + result = youtube_timestamp_regex.findall(input_str) + try: + input_string = result[0] + seconds = int(input_string) + hours, remainder = divmod(seconds, 3600) + minutes, seconds = divmod(remainder, 60) + except IndexError: # nothing to parse + return "" + except ValueError: # human-readable number + hours, offset = get_human_readable(input_string, "h") + minutes, offset = get_human_readable(input_string, "m", offset) + seconds, offset = get_human_readable(input_string, "s", offset) + + if hours == 0: + return '{}:{:02}'.format(minutes, seconds) + return '{}:{:02}:{:02}'.format(hours, minutes, seconds) + replacers = [ { - "regex": re.compile(r"(?:https?:\/\/)?(?:www\.)?youtu\.?be(?:\.com)?\/?.*(?:watch|embed)?(?:.*v=|v\/|\/)([\w\-_]+)\&?", re_flags), + "regex": re.compile(r"(?:(?:https?:)?\/\/)?(?:(?:www|m)\.)?(?:(?:youtube(?:-nocookie)?\.com|youtu.be))(?:\/(?:[\w\-]+\?v=|embed\/|live\/|v\/)?)([\w\-]+)(\S+)?", re_flags), "becomes": "https://y.outube.duckdns.org/{}", + "timestamp": youtube_timestamp }, { "regex": re.compile(r"(?:https?:\/\/)?(?:www\.)?twitter\.com\/(?:#!\/)?(.*)\/status(?:es)?\/([^\/\?\s]+)", re_flags),

@@ -26,7 +53,7 @@ "becomes": "https://ddinstagram.com/{}/{}",

}, ] -link_message = "Da {}[\.]({})" +link_message = "Da {}[\.]({}) {}" def get_callback_data(feedback: bool) -> str: payload = { "feedback": feedback }

@@ -59,7 +86,13 @@ regex = site["regex"]

res = regex.findall(message) for r in res: link = format_template(site["becomes"], r) - output.append(link) + + try: + timestamp = site["timestamp"](r[-1]) + except KeyError: + timestamp = None + + output.append([link, timestamp]) return output async def replace(update: Update, _) -> None:

@@ -70,7 +103,9 @@ links = parse_text(update.message.caption)

for link in links: logger.info(link) - text = link_message.format(update.effective_user.mention_markdown_v2(update.effective_user.name), link) + + user = update.effective_user.mention_markdown_v2(update.effective_user.name) + text = link_message.format(user, link[0], link[1]) message = await update.effective_chat.send_message(text, parse_mode=ParseMode.MARKDOWN_V2) await sleep(FEEDBACK_TIMEOUT) await message.edit_reply_markup(reply_markup=get_message_markup())