Support multiple images

This commit is contained in:
Zeva Rose 2022-03-18 15:01:44 -06:00
parent 05b15c0e29
commit 0cb744941c

View file

@ -2,10 +2,11 @@
import asyncio
from datetime import datetime, timedelta
from logging import Logger
from typing import List
import tweepy
from dis_snek import Snake
from dis_snek.models.discord.embed import EmbedAttachment
from dis_snek.models.discord.embed import Embed
from jarvis_core.db import q
from jarvis_core.db.models import TwitterAccount, TwitterFollow
from jarvis_core.util import build_embed
@ -15,6 +16,62 @@ from jarvis_tasks.config import TaskConfig
config = TaskConfig.from_yaml()
def tweet_embeds(tweet: tweepy.models.Status) -> List[Embed]:
"""
Build a tweet embeds.
Args:
tweet: Tweet to build embed
"""
url = f"https://twitter.com/{tweet.user.name}/status/{tweet.id}"
entities = tweet.__dict__.get("extended_entities", {})
media = entities.get("media", [])
photos = []
for item in media:
if item["type"] in ["photo", "animated_gif"]:
photos.append(item["media_url_https"])
text = tweet.text
if subtweet := tweet.__dict__.get("quoted_status", None):
subuser = subtweet.user
text += f"\n\n> [@{subuser.name}](https://twitter.com/{subuser.name})"
text += f"\n> {subtweet.text}"
if entites := subtweet.__dict__.get("extended_entities", {}):
submedia = entites.get("media", [])
for item in submedia:
if item["type"] in ["photo", "animated_gif"]:
photos.append(item["media_url_https"])
base_embed = build_embed(
title="",
description=(text + f"\n\n[View this tweet]({url})"),
fields=[],
color="#1DA1F2",
url=url,
)
base_embed.set_author(
name=tweet.user.name,
url=url,
icon_url=tweet.author.profile_image_url_https,
)
base_embed.set_footer(
text="Twitter",
icon_url="https://abs.twimg.com/icons/apple-touch-icon-192x192.png",
)
embeds = [base_embed]
if len(photos) > 0:
embeds[0].set_image(url=photos[0])
for photo in photos[1:4]:
embed = Embed(url=url)
embed.set_image(url=photo)
embeds.append(embed)
return embeds
async def twitter(bot: Snake, logger: Logger) -> None:
"""
Sync tweets in the background.
@ -64,32 +121,13 @@ async def twitter(bot: Snake, logger: Logger) -> None:
if retweet and not follow.retweets:
continue
timestamp = int(tweet.created_at.timestamp())
url = f"https://twitter.com/{account.handle}/status/{tweet.id}"
embeds = tweet_embeds(tweet)
mod = "re" if retweet else ""
media = tweet.entities.get("media", None)
photo = None
if media and media[0]["type"] in ["photo", "animated_gif"]:
photo = EmbedAttachment(url=media[0]["media_url_https"])
embed = build_embed(
title="",
description=(tweet.text + f"\n\n[View this tweet]({url})"),
fields=[],
color="#1DA1F2",
image=photo,
)
embed.set_author(
name=account.handle,
url=url,
icon_url=tweet.author.profile_image_url_https,
)
embed.set_footer(
text="Twitter",
icon_url="https://abs.twimg.com/icons/apple-touch-icon-192x192.png",
)
timestamp = int(tweet.created_at.timestamp())
await channel.send(
f"`@{account.handle}` {mod}tweeted this at <t:{timestamp}:f>"
f"`@{account.handle}` {mod}tweeted this at <t:{timestamp}:f>",
embeds=embeds,
)
# Delete invalid follows