Add crosspost support

This commit is contained in:
Zeva Rose 2022-05-05 00:29:58 -06:00
parent 91eb089189
commit 1b05cc3b13

View file

@ -12,7 +12,7 @@ from jarvis_core.db import q
from jarvis_core.db.models import Subreddit, SubredditFollow from jarvis_core.db.models import Subreddit, SubredditFollow
from naff import Client from naff import Client
from naff.client.errors import NotFound as DNotFound from naff.client.errors import NotFound as DNotFound
from naff.models.discord.embed import Embed from naff.models.discord.embed import Embed, EmbedField
from jarvis_tasks import const from jarvis_tasks import const
from jarvis_tasks.config import TaskConfig from jarvis_tasks.config import TaskConfig
@ -26,7 +26,7 @@ running = []
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
async def post_embeds(sub: Sub, post: Submission) -> Optional[List[Embed]]: async def post_embeds(sub: Sub, post: Submission, reddit: Reddit) -> Optional[List[Embed]]:
""" """
Build a post embeds. Build a post embeds.
@ -36,8 +36,18 @@ async def post_embeds(sub: Sub, post: Submission) -> Optional[List[Embed]]:
url = "https://reddit.com" + post.permalink url = "https://reddit.com" + post.permalink
await post.author.load() await post.author.load()
author_url = f"https://reddit.com/u/{post.author.name}" author_url = f"https://reddit.com/u/{post.author.name}"
author_icon = post.author.icon_img
images = [] images = []
content = f"**{post.title}**" title = f"{post.title}"
fields = []
content = ""
og_post = None
if not post.is_self:
og_post = post # noqa: F841
post = await reddit.submission(post.crosspost_parent_list[0]["id"])
await post.load()
fields.append(EmbedField(name="Crossposted From", value=post.subreddit_name_prefixed))
content = f"> **{post.title}**"
if "url" in vars(post): if "url" in vars(post):
if any(post.url.endswith(x) for x in ["jpeg", "jpg", "png", "gif"]): if any(post.url.endswith(x) for x in ["jpeg", "jpg", "png", "gif"]):
images = [post.url] images = [post.url]
@ -53,23 +63,26 @@ async def post_embeds(sub: Sub, post: Submission) -> Optional[List[Embed]]:
if "selftext" in vars(post) and post.selftext: if "selftext" in vars(post) and post.selftext:
content += "\n\n" + post.selftext content += "\n\n" + post.selftext
if len(content) > 600: if len(content) > 900:
content = content[:600] + "..." content = content[:900] + "..."
content += f"\n\n[View this post]({url})" content += f"\n\n[View this post]({url})"
if not images and not content: if not images and not content:
logging.debug(f"Post {post.id} had neither content nor images?") logger.debug(f"Post {post.id} had neither content nor images?")
return None return None
color = "#FF4500" color = "#FF4500"
if "primary_color" in vars(sub): if "primary_color" in vars(sub):
color = sub.primary_color color = sub.primary_color
base_embed = build_embed( base_embed = build_embed(
title="", description=content, fields=[], timestamp=post.created_utc, url=url, color=color title=title,
) description=content,
base_embed.set_author( fields=fields,
name="u/" + post.author.name, url=author_url, icon_url=post.author.icon_img timestamp=post.created_utc,
url=url,
color=color,
) )
base_embed.set_author(name="u/" + post.author.name, url=author_url, icon_url=author_icon)
base_embed.set_footer( base_embed.set_footer(
text="Reddit", icon_url="https://www.redditinc.com/assets/images/site/reddit-logo.png" text="Reddit", icon_url="https://www.redditinc.com/assets/images/site/reddit-logo.png"
) )
@ -86,7 +99,7 @@ async def post_embeds(sub: Sub, post: Submission) -> Optional[List[Embed]]:
return embeds return embeds
async def _stream(sub: Sub, bot: Client) -> None: async def _stream(sub: Sub, bot: Client, reddit: Reddit) -> None:
""" """
Stream a subreddit Stream a subreddit
@ -125,7 +138,7 @@ async def _stream(sub: Sub, bot: Client) -> None:
num_follows -= 1 num_follows -= 1
continue continue
embeds = await post_embeds(sub, post) embeds = await post_embeds(sub, post, reddit)
timestamp = int(post.created_utc) timestamp = int(post.created_utc)
try: try:
@ -214,7 +227,7 @@ async def reddit(bot: Client) -> None:
continue continue
# Create and run stream # Create and run stream
coro = _stream(sub, bot) coro = _stream(sub, bot, red)
asyncio.create_task(coro) asyncio.create_task(coro)
count += 1 count += 1