Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle share links #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions sopel_reddit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
r'(?:/r/\S+?)?/comments/(?P<submission>[\w-]+)'
r'(?:/?(?:[\w%]+/(?P<comment>[\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 @@ -157,18 +158,35 @@ def video_info(bot, trigger, match):
return say_post_info(bot, trigger, submission_id, False, True)


@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(
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)
if match:
post_or_comment_info(bot, trigger, match, force_link=True)


@plugin.url(post_or_comment_url)
@plugin.url(short_post_url)
@plugin.output_prefix(PLUGIN_OUTPUT_PREFIX)
def post_or_comment_info(bot, trigger, match):
def post_or_comment_info(bot, trigger, match, force_link=False):
match = match or trigger
groups = match.groupdict()

if groups.get("comment"):
say_comment_info(bot, trigger, groups["comment"])
say_comment_info(bot, trigger, groups["comment"], show_link=force_link)
return

say_post_info(bot, trigger, groups["submission"])
say_post_info(bot, trigger, groups["submission"], show_comments_link=force_link)


@plugin.url(gallery_url)
Expand Down Expand Up @@ -260,15 +278,15 @@ def say_post_info(bot, trigger, id_, show_link=True, show_comments_link=False):
return plugin.NOLIMIT


def say_comment_info(bot, trigger, id_):
def say_comment_info(bot, trigger, id_, show_link=False):
try:
c = bot.memory['reddit_praw'].comment(id_)
except prawcore.exceptions.NotFound:
bot.reply('No such comment.')
return plugin.NOLIMIT

message = ('Comment by {author} | {points} {points_text} | '
'Posted at {posted} | {comment}')
'Posted at {posted} | {link}{comment}')

if c.author:
author = c.author.name
Expand All @@ -279,12 +297,20 @@ def say_comment_info(bot, trigger, id_):

posted = get_time_created(bot, trigger, c.created_utc)

# DIY shortish-link, since c.permalink is both long and not a link
link = ""
if show_link:
link = "https://reddit.com/comments/{}/c/{} | ".format(
c.link_id[3:], # strip t3_ prefix
c.id
)

# stolen from the function I (dgw) wrote for our github plugin
lines = [line for line in c.body.splitlines() if line and line[0] != '>']

message = message.format(
author=author, points=c.score, points_text=points_text,
posted=posted, comment=' '.join(lines))
posted=posted, link=link, comment=' '.join(lines))

bot.say(message, truncation=' […]')

Expand Down