Skip to content

Commit

Permalink
remove regex extras, fix http share links
Browse files Browse the repository at this point in the history
  • Loading branch information
half-duplex committed Sep 27, 2024
1 parent 79365e1 commit 4ee43c4
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions sopel_reddit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
r'(?:/r/\S+?)?/comments/(?P<submission>[\w-]+)'
r'(?:/?(?:[\w%]+/(?P<comment>[\w-]+))?)'
)
share_url = r'https?://(?:www\.)?reddit\.com/r/\S+?/s/(?P<share>[\w-]+)'
share_url = r'https?://(?:www\.)?reddit\.com/r/\S+?/s/\w+'
short_post_url = r'https?://(redd\.it|reddit\.com)/(?P<submission>[\w-]+)/?$'
user_url = r'%s/u(?:ser)?/([\w-]+)' % domain
image_url = r'https?://(?P<subdomain>i|preview)\.redd\.it/(?:[\w%]+-)*(?P<image>[^-?\s]+)'
Expand Down Expand Up @@ -161,14 +161,18 @@ def video_info(bot, trigger, match):
@plugin.url(share_url)
@plugin.output_prefix(PLUGIN_OUTPUT_PREFIX)
def share_info(bot, trigger):
url = str(trigger)
if url.startswith("http:"):
url = "https:" + url[5:]
redirect_response = requests.get(
trigger, allow_redirects=False, headers={"User-Agent": USER_AGENT}
url, allow_redirects=False, headers={"User-Agent": USER_AGENT}
)
if redirect_response.status_code != 301:
return
real_url = redirect_response.headers["location"]
match = re.match(post_or_comment_url, real_url)
post_or_comment_info(bot, trigger, match, force_link=True)
if match:
post_or_comment_info(bot, trigger, match, force_link=True)


@plugin.url(post_or_comment_url)
Expand Down Expand Up @@ -296,7 +300,7 @@ def say_comment_info(bot, trigger, id_, show_link=False):
# DIY shortish-link, since c.permalink is both long and not a link
link = ""
if show_link:
link = "https://reddit.com/comments/{}/c/{}/ | ".format(
link = "https://reddit.com/comments/{}/c/{} | ".format(
c.link_id[3:], # strip t3_ prefix
c.id
)
Expand Down

0 comments on commit 4ee43c4

Please sign in to comment.