Files
NijiHolo_EN_ID_Bot/src/twapi.py
T

208 lines
6.9 KiB
Python
Raw Normal View History

2022-09-27 02:49:03 -07:00
import datetime
2022-09-27 15:09:09 -07:00
import traceback
import asyncio
2023-08-17 02:28:29 -07:00
from dotenv import dotenv_values
2022-09-24 17:56:58 -07:00
import tweepy
import talenttweet as tt
import talent_lists as tl
2022-09-24 17:56:58 -07:00
import util
2024-01-25 16:29:01 -08:00
2022-09-24 17:56:58 -07:00
class TwAPI:
2022-09-27 02:49:03 -07:00
tweets_fetched = 0
2022-09-24 17:56:58 -07:00
instance = None
2024-01-25 16:29:01 -08:00
TWEET_MEDIA_FIELDS = ["url"]
TWEET_FIELDS = ["created_at", "in_reply_to_user_id", "referenced_tweets"]
TWEET_EXPANSIONS = ["entities.mentions.username", "referenced_tweets.id.author_id"]
# Returns a tuple of user IDs:(reply_to, qrt, {mentions})
# for a single tweet.
2022-09-24 17:56:58 -07:00
#
# Tweet must have been queried with these parameters:
# media_fields=['url'],
# tweet_fields=['created_at', 'in_reply_to_user_id'],
# expansions=['entities.mentions.username', 'referenced_tweets.id.author_id']
#
# VALUES IN TUPLE ARE NONE OR INT.
2022-09-24 17:56:58 -07:00
@staticmethod
def get_mrq(response):
tweet = response.data
mentions = set()
reply_to = None
qrt = None
2022-09-24 17:56:58 -07:00
# mentions
try:
2024-01-25 16:29:01 -08:00
mention_list = tweet.entities["mentions"]
2022-09-24 17:56:58 -07:00
for mention in mention_list:
2024-01-25 16:29:01 -08:00
mentions.add(int(mention["id"]))
except:
pass
2022-09-24 17:56:58 -07:00
# reply-to
if tweet.in_reply_to_user_id != None:
reply_to = tweet.in_reply_to_user_id
2022-09-24 17:56:58 -07:00
# qrt
if tweet.referenced_tweets:
for ref_tweet in tweet.referenced_tweets:
2024-01-25 16:29:01 -08:00
if ref_tweet.type == "quoted":
for incl_tweet in response.includes["tweets"]:
2022-09-24 17:56:58 -07:00
if incl_tweet.id == ref_tweet.id:
qrt = incl_tweet.author_id
try:
mentions.remove(reply_to)
2024-01-25 16:29:01 -08:00
except:
pass
2022-10-01 14:09:14 -07:00
try:
mentions.remove(qrt)
2024-01-25 16:29:01 -08:00
except:
pass
mention_list = list(mentions)
for uid in mention_list:
if uid not in tl.talents.keys():
mentions.remove(uid)
if reply_to not in tl.talents.keys():
reply_to = None
2022-09-24 17:56:58 -07:00
2024-01-25 16:29:01 -08:00
return (mentions, reply_to, qrt)
2022-09-24 17:56:58 -07:00
def __init__(self):
2024-01-25 16:29:01 -08:00
creds = dotenv_values(util.working_path(file=".env"))
2022-09-24 17:56:58 -07:00
TwAPI.instance = self
self.client = tweepy.Client(
2024-01-25 16:29:01 -08:00
consumer_key=creds["app_key"],
consumer_secret=creds["app_secret"],
access_token=creds["user_token"],
access_token_secret=creds["user_secret"],
2022-09-24 17:56:58 -07:00
)
2023-08-18 01:34:25 -07:00
self.api = tweepy.API(
auth=tweepy.OAuthHandler(
2024-01-25 16:29:01 -08:00
consumer_key=creds["app_key"],
consumer_secret=creds["app_secret"],
access_token=creds["user_token"],
access_token_secret=creds["user_secret"],
2023-08-18 01:34:25 -07:00
)
)
2024-01-25 16:29:01 -08:00
2023-08-18 01:34:25 -07:00
try:
self.me = self.client.get_me().data
2024-01-25 16:29:01 -08:00
print(
f'Assuming the account of @{self.me.data["username"]} ({self.me["id"]})'
)
2023-08-18 01:34:25 -07:00
except:
pass
2022-09-27 15:09:09 -07:00
2024-01-25 16:29:01 -08:00
async def post_tweet(
self,
text="",
media_ids: list = None,
reply_to_tweet: int = None,
quote_tweet_id: int = None,
):
2022-09-27 02:49:03 -07:00
try:
2024-01-25 16:29:01 -08:00
tweet = self.client.create_tweet(
text=text,
media_ids=media_ids,
in_reply_to_tweet_id=reply_to_tweet,
quote_tweet_id=quote_tweet_id,
)
2022-09-27 02:49:03 -07:00
return tweet
except tweepy.TooManyRequests as e:
2024-01-25 16:29:01 -08:00
wait_for = (
abs(
float(e.response.headers["x-rate-limit-reset"])
- datetime.datetime.now().timestamp()
)
+ 1
)
print(f"\thit rate limit: trying again in {wait_for}s...")
2022-09-27 02:49:03 -07:00
await asyncio.sleep(wait_for)
2024-01-25 16:29:01 -08:00
return await self.post_tweet(
text=text,
media_ids=media_ids,
reply_to_tweet=reply_to_tweet,
quote_tweet_id=quote_tweet_id,
)
2022-09-27 02:49:03 -07:00
async def get_ttweet_image_media_id(self, ttweet):
img = await util.create_ttweet_image(ttweet)
2022-09-27 02:49:03 -07:00
media = self.api.media_upload(img)
return media.media_id
# return True = successfully posted a single ttweet
# return False = did not post ttweet (duplicate)
2023-08-18 01:34:25 -07:00
async def post_ttweet(self, ttweet: tt.TalentTweet, dry_run=False):
2024-01-25 16:29:01 -08:00
print(
f"------{ttweet.tweet_id} ({util.get_username_local(ttweet.author_id)})------"
)
2023-08-16 18:48:13 -07:00
text = ttweet.announce_text()
ttweet_url = ttweet.url()
2024-01-25 16:29:01 -08:00
if dry_run:
print("-------------------- DRY RUN --------------------")
2023-08-16 18:48:13 -07:00
print(ttweet)
2024-01-25 16:29:01 -08:00
if dry_run:
return False
2023-01-14 01:18:15 -08:00
# NO DRY-RUN: actually post tweet
# main tweet: text + screenshot
try:
2024-01-25 16:29:01 -08:00
print("creating main QRT w/ screenshot...")
2023-01-14 01:18:15 -08:00
media_ids = [await self.get_ttweet_image_media_id(ttweet)]
2024-01-25 16:29:01 -08:00
twt_resp = await self.post_tweet(
text, media_ids=media_ids, quote_tweet_id=ttweet.tweet_id
)
print("done")
2023-01-14 01:18:15 -08:00
except:
2024-01-25 16:29:01 -08:00
print(
"error occurred trying to create main tweet, falling back to URL-main + reply screencap format"
)
2023-01-23 22:37:21 -08:00
traceback.print_exc()
try:
2024-01-25 16:29:01 -08:00
print("posting main tweet...")
2023-08-17 02:28:29 -07:00
twt_resp = await self.post_tweet(text, quote_tweet_id=ttweet.tweet_id)
2024-01-25 16:29:01 -08:00
print("done")
twt_id = twt_resp.data["id"]
2023-01-14 01:18:15 -08:00
try:
2024-01-25 16:29:01 -08:00
print("creating reply img...", end="")
2023-01-14 01:18:15 -08:00
media_ids = [await self.get_ttweet_image_media_id(ttweet)]
2024-01-25 16:29:01 -08:00
print("posting reply tweet...", end="")
2023-01-14 01:18:15 -08:00
await self.post_tweet(reply_to_tweet=twt_id, media_ids=media_ids)
2024-01-25 16:29:01 -08:00
print("done")
2023-01-14 01:18:15 -08:00
except:
2024-01-25 16:29:01 -08:00
print("Had trouble posting reply image tweet.")
print("successfully posted ttweet!")
2023-01-14 01:18:15 -08:00
except tweepy.Forbidden as e:
2024-01-25 16:29:01 -08:00
if "duplicate content" in e.api_messages[0]:
print(
"Twitter says the TalentTweet is a duplicate; skipping error-free..."
)
2023-01-14 01:18:15 -08:00
return False
else:
raise e
2023-01-14 02:13:03 -08:00
return True
async def post_ttweet_by_id(self, id: int):
from scraper import Scraper
2024-01-25 16:29:01 -08:00
print(f"Manually posting tweet {id}")
s = Scraper()
t = s.get_tweet(id, True)
if not t:
2024-01-25 16:29:01 -08:00
print("Tweet could not be retrieved")
return False
2024-01-25 16:29:01 -08:00
ttweet = tt.TalentTweet.create_from_tweety(t)
if not ttweet.is_cross_company():
2024-01-25 16:29:01 -08:00
print(f"{ttweet.username}/{ttweet.tweet_id} is not cross-company!")
return False
2024-01-25 16:29:01 -08:00
print(f"Posting {ttweet.username}/{ttweet.tweet_id}...")
return await self.post_ttweet(ttweet)