Skip to content

Commit

Permalink
Fix twitter embedder
Browse files Browse the repository at this point in the history
  • Loading branch information
joinemm committed Jun 10, 2023
1 parent 395d789 commit 5167192
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 58 deletions.
4 changes: 2 additions & 2 deletions dev-requirements.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-c requirements.txt
pre-commit
flake8
black
pylint
isort
isort
ruff
16 changes: 5 additions & 11 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ dill==0.3.6
# via pylint
distlib==0.3.6
# via virtualenv
filelock==3.12.0
filelock==3.12.1
# via virtualenv
flake8==6.0.0
# via -r dev-requirements.in
identify==2.5.24
# via pre-commit
isort==5.12.0
Expand All @@ -31,9 +29,7 @@ isort==5.12.0
lazy-object-proxy==1.9.0
# via astroid
mccabe==0.7.0
# via
# flake8
# pylint
# via pylint
mypy-extensions==1.0.0
# via black
nodeenv==1.8.0
Expand All @@ -44,21 +40,19 @@ packaging==23.1
# black
pathspec==0.11.1
# via black
platformdirs==3.5.1
platformdirs==3.5.3
# via
# black
# pylint
# virtualenv
pre-commit==3.3.2
# via -r dev-requirements.in
pycodestyle==2.10.0
# via flake8
pyflakes==3.0.1
# via flake8
pylint==2.17.4
# via -r dev-requirements.in
pyyaml==6.0
# via pre-commit
ruff==0.0.272
# via -r dev-requirements.in
tomlkit==0.11.8
# via pylint
virtualenv==20.23.0
Expand Down
65 changes: 25 additions & 40 deletions modules/media_embedders.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import arrow
import discord
import regex
import tweepy
import tweepy.asynchronous as aiotweepy
import yarl
from aiohttp import ClientConnectorError
from attr import dataclass
Expand Down Expand Up @@ -311,13 +309,6 @@ class TwitterEmbedder(BaseEmbedder):
EMOJI = "<:twitter:937425165241946162>"
NO_RESULTS_ERROR = "Found no Twitter links to embed!"

def __init__(self, bot: "MisoBot"):
self.tweepy = aiotweepy.AsyncClient(
bot.keychain.TWITTER_BEARER_TOKEN,
wait_on_rate_limit=True,
)
super().__init__(bot)

@staticmethod
def extract_links(text: str, include_id_only=True):
text = "\n".join(text.split())
Expand All @@ -343,45 +334,39 @@ async def create_message(
tweet_id: int,
options: Options | None = None,
):
response = await self.tweepy.get_tweet(
tweet_id,
tweet_fields=["attachments", "created_at"],
expansions=["attachments.media_keys", "author_id"],
media_fields=["variants", "url", "alt_text"],
user_fields=["profile_image_url"],
)

if response.errors: # type: ignore
raise exceptions.CommandWarning(response.errors[0]["detail"]) # type: ignore

tweet: tweepy.Tweet = response.data # type: ignore
media_urls = []
async with self.bot.session.get(
f"https://api.fxtwitter.com/i/status/{tweet_id}"
) as response:
data = await response.json()
if data["code"] != 200:
raise exceptions.CommandError(
f"Error from API: {data['message']}",
)

user = response.includes["users"][0] # type: ignore
screen_name = user.username
tweet_url = f"https://twitter.com/{screen_name}/status/{tweet.id}"
tweet = data["tweet"]
screen_name = tweet["author"]["screen_name"]

media: tweepy.Media
for media in response.includes.get("media", []): # type: ignore
if media.type == "photo":
base, extension = media.url.rsplit(".", 1)
media_urls.append(("jpg", f"{base}?format={extension}&name=orig"))
else:
variants = sorted(
filter(lambda x: x["content_type"] == "video/mp4", media.data["variants"]),
key=lambda y: y["bit_rate"],
reverse=True,
for media in data["tweet"]["media"]["all"]:
if media["type"] == "photo":
base, extension = media["url"].rsplit(".", 1)
media_urls.append(
("jpg", f"{base}?format={extension}&name=orig"),
)
media_urls.append(("mp4", variants[0]["url"]))
else:
media_urls.append(("mp4", media["url"]))

if not media_urls:
raise exceptions.CommandWarning(f"Tweet `{tweet_url}` does not include any media.")
raise exceptions.CommandWarning(
f"Tweet `{tweet['url']}` does not include any media.",
)

timestamp = arrow.get(tweet.created_at)
timestamp = arrow.get(tweet["created_timestamp"])

tasks = []
for n, (extension, media_url) in enumerate(media_urls, start=1):
filename = f"{timestamp.format('YYMMDD')}-@{screen_name}-{tweet.id}-{n}.{extension}"
ts_format = timestamp.format("YYMMDD")
filename = f"{ts_format}-@{screen_name}-{tweet_id}-{n}.{extension}"
tasks.append(
self.download_media(
media_url,
Expand All @@ -395,7 +380,7 @@ async def create_message(
username = discord.utils.escape_markdown(screen_name)
caption = f"{self.EMOJI} **@{username}** <t:{int(timestamp.timestamp())}:d>"
if options and options.captions:
caption += f"\n>>> {tweet.text.rsplit(maxsplit=1)[0]}"
caption += f"\n>>> {tweet['text']}"

files = []
too_big_files = []
Expand All @@ -410,7 +395,7 @@ async def create_message(
return {
"content": caption,
"files": files,
"view": MediaUI("View on Twitter", tweet_url),
"view": MediaUI("View on Twitter", tweet["url"]),
}


Expand Down
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ oauthlib==3.2.2
# via
# requests-oauthlib
# tweepy
orjson==3.8.14
orjson==3.9.1
# via discord-py
packaging==23.1
# via
Expand All @@ -143,7 +143,7 @@ pycares==4.3.0
# via aiodns
pycparser==2.21
# via cffi
pydantic==1.10.8
pydantic==1.10.9
# via shazamio
pydub==0.25.1
# via shazamio
Expand All @@ -167,7 +167,7 @@ random-user-agent==1.0.1
# via -r requirements.in
redis==4.5.5
# via -r requirements.in
regex==2023.5.5
regex==2023.6.3
# via -r requirements.in
requests==2.31.0
# via
Expand All @@ -191,11 +191,11 @@ soupsieve==2.4.1
# via beautifulsoup4
tweepy[async]==4.14.0
# via -r requirements.in
typing-extensions==4.6.2
typing-extensions==4.6.3
# via
# async-lru
# pydantic
urllib3==2.0.2
urllib3==2.0.3
# via requests
uvloop==0.17.0
# via -r requirements.in
Expand Down

0 comments on commit 5167192

Please sign in to comment.