Skip to content

Commit f869d64

Browse files
committed
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.
1 parent f8db436 commit f869d64

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

sopel/modules/reddit.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727

2828
domain = r'https?://(?:www\.|old\.|pay\.|ssl\.|[a-z]{2}\.)?reddit\.com'
2929
subreddit_url = r'%s/r/([\w-]+)/?$' % domain
30-
post_url = r'%s/r/\S+?/comments/([\w-]+)(?:/[\w%%]+)?/?' % domain
30+
post_or_comment_url = (
31+
domain +
32+
r'/r/\S+?/comments/(?P<submission>[\w-]+)'
33+
r'(?:/?(?:[\w%]+/(?P<comment>[\w-]+))?)'
34+
)
3135
short_post_url = r'https?://redd\.it/([\w-]+)'
3236
user_url = r'%s/u(?:ser)?/([\w-]+)' % domain
33-
comment_url = r'%s/r/\S+?/comments/\S+?/\S+?/([\w-]+)' % domain
3437
image_url = r'https?://i\.redd\.it/\S+'
3538
video_url = r'https?://v\.redd\.it/([\w-]+)'
3639
gallery_url = r'https?://(?:www\.)?reddit\.com/gallery/([\w-]+)'
@@ -107,18 +110,25 @@ def video_info(bot, trigger, match):
107110
timeout=(10.0, 4.0)).headers['Location']
108111
try:
109112
return say_post_info(
110-
bot, trigger, re.match(post_url, url).group(1), False, True)
113+
bot, trigger, re.match(post_or_comment_url, url).group('submission'), False, True
114+
)
111115
except AttributeError:
112116
# Fail silently if we can't map the video link to a submission
113117
return plugin.NOLIMIT
114118

115119

116-
@plugin.url(post_url)
120+
@plugin.url(post_or_comment_url)
117121
@plugin.url(short_post_url)
118122
@plugin.output_prefix(PLUGIN_OUTPUT_PREFIX)
119-
def rpost_info(bot, trigger, match):
123+
def post_or_comment_info(bot, trigger, match):
120124
match = match or trigger
121-
return say_post_info(bot, trigger, match.group(1))
125+
comment = match.group('comment')
126+
127+
if comment:
128+
say_comment_info(bot, trigger, comment)
129+
return
130+
131+
say_post_info(bot, trigger, match.group('submission'))
122132

123133

124134
@plugin.url(gallery_url)
@@ -205,12 +215,9 @@ def say_post_info(bot, trigger, id_, show_link=True, show_comments_link=False):
205215
return plugin.NOLIMIT
206216

207217

208-
@plugin.url(comment_url)
209-
@plugin.output_prefix(PLUGIN_OUTPUT_PREFIX)
210-
def comment_info(bot, trigger, match):
211-
"""Shows information about the linked comment"""
218+
def say_comment_info(bot, trigger, id_):
212219
try:
213-
c = bot.memory['reddit_praw'].comment(match.group(1))
220+
c = bot.memory['reddit_praw'].comment(id_)
214221
except prawcore.exceptions.NotFound:
215222
bot.reply('No such comment.')
216223
return plugin.NOLIMIT

0 commit comments

Comments
 (0)