Skip to content

Commit

Permalink
reddit: handle post and comment links in one regex
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dgw committed Sep 10, 2021
1 parent f8db436 commit f869d64
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions sopel/modules/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<submission>[\w-]+)'
r'(?:/?(?:[\w%]+/(?P<comment>[\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-]+)'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f869d64

Please sign in to comment.