Skip to content

Commit

Permalink
title: support reddit by redirecting to redlib instance
Browse files Browse the repository at this point in the history
  • Loading branch information
pbui committed Nov 7, 2024
1 parent ef93482 commit c044f74
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/bobbit/modules/title.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,19 @@ async def youtube_title(bot, url, text):

# Reddit Command

REDDIT_PATTERN = r'.*(?P<url>https?://[^\s]+reddit.com/[^\s]+).*'
REDDIT_PATTERN = r'.*(?P<url>https?://[^\s]*reddit.com/[^\s]+).*'
REDLIB_HOST = 'redlib.h4x0r.space'

async def reddit_title(bot, message, url):
# XXX: Hack is to redirect to redlib instance
url = re.sub(r'://([^/]+)/', '://{}/'.format(REDLIB_HOST), url)

async with bot.http_client.get(url) as response:
text = await response.text()

try:
post_title = re.findall(r'<meta property="og:title" content="([^"]+)"', text)[0]
subreddit, post_title = post_title.split(' - ', 1)
post_title, subreddit = post_title.split(' - ', 1)
return message.with_body(bot.client.format_text(
'{color}{green}{}{color}: {bold}{}{bold}',
subreddit, html.unescape(post_title)
Expand Down Expand Up @@ -160,7 +164,7 @@ def register(bot):

return (
('command', PATTERN , title),
#('command', REDDIT_PATTERN, reddit_title),
('command', REDDIT_PATTERN, reddit_title),
)

# vim: set sts=4 sw=4 ts=8 expandtab ft=python:

0 comments on commit c044f74

Please sign in to comment.