Skip to content

Commit

Permalink
Support reddit post content and messages longer than 2000 chars
Browse files Browse the repository at this point in the history
  • Loading branch information
amadejkastelic committed Nov 23, 2023
1 parent f5b416f commit a603ed4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
11 changes: 10 additions & 1 deletion downloader/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,18 @@ async def _hydrate_post(self, post: models.Post) -> bool:
self.url = requests.get(self.url).url.split('?')[0]
submission = await self.client.submission(url=self.url)

content = ''
if submission.selftext:
content = f'\n\n{submission.selftext}'
if submission.url:
if content:
content += f'\n{submission.url}'
else:
content = f'\n\n{submission.url}'

post.url = self.url
post.author = submission.author
post.description = submission.title
post.description = f'{submission.title}{content}'
post.likes = submission.score
post.spoiler = submission.over_18 or submission.spoiler
post.created = datetime.datetime.fromtimestamp(submission.created_utc).astimezone()
Expand Down
14 changes: 13 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import datetime
import io
import logging
import os
import random
Expand Down Expand Up @@ -68,8 +69,19 @@ async def _send_post(
)

try:
content = f'Here you go {author.mention} {random.choice(emoji)}.\n{str(post)}'
if len(content) > 2000:
if file:
content = content[:1997] + '...'
else:
content, _, fp = str(post).partition('📕 Description: ')
file = discord.File(
fp=io.BytesIO(fp.encode()),
filename='message.txt',
)

return await channel.send(
content=f'Here you go {author.mention} {random.choice(emoji)}.\n{str(post)}',
content=content,
file=file,
suppress_embeds=True,
)
Expand Down
2 changes: 1 addition & 1 deletion models/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class Post:
def __str__(self) -> str:
return (
'🔗 URL: {url}\n'
'📕 Description: {description}\n'
'🧑🏻‍🎨 Author: {author}\n'
'📅 Created: {created}\n'
'👀 Views: {views}\n'
'👍🏻 Likes: {likes}\n'
'📕 Description: {description}\n'
).format(
url=self.url,
author=self.author or '❌',
Expand Down

0 comments on commit a603ed4

Please sign in to comment.