From f869d64ba2e7f828eed181c59ea4b57e28ffb5e2 Mon Sep 17 00:00:00 2001 From: dgw Date: Wed, 18 Aug 2021 01:05:38 -0700 Subject: [PATCH] reddit: handle post and comment links in one regex It's entirely impractical to try to handle post and comment links in separate callables, because comment links contain an entire post link. Before, this was handled by anchoring the post URL pattern at the end, but that broke Sopel handling e.g. `?sort=confidence` if someone sorted the comments before copying the link into IRC. Lookaround assertions didn't appear to prevent matching the post link contained in every comment link, so this solution seemed like the only way forward. It's a nice tidy dispatcher pattern, really. --- sopel/modules/reddit.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/sopel/modules/reddit.py b/sopel/modules/reddit.py index 5bd8ee1b0e..9c0fea840c 100644 --- a/sopel/modules/reddit.py +++ b/sopel/modules/reddit.py @@ -27,10 +27,13 @@ domain = r'https?://(?:www\.|old\.|pay\.|ssl\.|[a-z]{2}\.)?reddit\.com' subreddit_url = r'%s/r/([\w-]+)/?$' % domain -post_url = r'%s/r/\S+?/comments/([\w-]+)(?:/[\w%%]+)?/?' % domain +post_or_comment_url = ( + domain + + r'/r/\S+?/comments/(?P[\w-]+)' + r'(?:/?(?:[\w%]+/(?P[\w-]+))?)' +) short_post_url = r'https?://redd\.it/([\w-]+)' user_url = r'%s/u(?:ser)?/([\w-]+)' % domain -comment_url = r'%s/r/\S+?/comments/\S+?/\S+?/([\w-]+)' % domain image_url = r'https?://i\.redd\.it/\S+' video_url = r'https?://v\.redd\.it/([\w-]+)' gallery_url = r'https?://(?:www\.)?reddit\.com/gallery/([\w-]+)' @@ -107,18 +110,25 @@ def video_info(bot, trigger, match): timeout=(10.0, 4.0)).headers['Location'] try: return say_post_info( - bot, trigger, re.match(post_url, url).group(1), False, True) + bot, trigger, re.match(post_or_comment_url, url).group('submission'), False, True + ) except AttributeError: # Fail silently if we can't map the video link to a submission return plugin.NOLIMIT -@plugin.url(post_url) +@plugin.url(post_or_comment_url) @plugin.url(short_post_url) @plugin.output_prefix(PLUGIN_OUTPUT_PREFIX) -def rpost_info(bot, trigger, match): +def post_or_comment_info(bot, trigger, match): match = match or trigger - return say_post_info(bot, trigger, match.group(1)) + comment = match.group('comment') + + if comment: + say_comment_info(bot, trigger, comment) + return + + say_post_info(bot, trigger, match.group('submission')) @plugin.url(gallery_url) @@ -205,12 +215,9 @@ def say_post_info(bot, trigger, id_, show_link=True, show_comments_link=False): return plugin.NOLIMIT -@plugin.url(comment_url) -@plugin.output_prefix(PLUGIN_OUTPUT_PREFIX) -def comment_info(bot, trigger, match): - """Shows information about the linked comment""" +def say_comment_info(bot, trigger, id_): try: - c = bot.memory['reddit_praw'].comment(match.group(1)) + c = bot.memory['reddit_praw'].comment(id_) except prawcore.exceptions.NotFound: bot.reply('No such comment.') return plugin.NOLIMIT