2022-09-27 02:49:03 -07:00
|
|
|
import datetime
|
2022-09-27 15:09:09 -07:00
|
|
|
import traceback
|
2022-10-02 04:57:24 -07:00
|
|
|
import asyncio
|
2022-09-25 03:39:15 -07:00
|
|
|
|
2022-09-24 17:56:58 -07:00
|
|
|
import tweepy
|
|
|
|
|
|
|
|
|
|
import api_secrets
|
|
|
|
|
import talenttweet as tt
|
2023-01-13 02:55:25 -08:00
|
|
|
import talent_lists as tl
|
2023-01-14 01:18:15 -08:00
|
|
|
import ttweetqueue as ttq
|
2022-09-24 17:56:58 -07:00
|
|
|
import util
|
|
|
|
|
|
|
|
|
|
class TwAPI:
|
2022-09-27 02:49:03 -07:00
|
|
|
tweets_fetched = 0
|
2022-09-24 17:56:58 -07:00
|
|
|
instance = None
|
|
|
|
|
TWEET_MEDIA_FIELDS = ['url']
|
2022-09-27 22:04:26 -07:00
|
|
|
TWEET_FIELDS = ['created_at', 'in_reply_to_user_id', 'referenced_tweets']
|
2022-09-24 17:56:58 -07:00
|
|
|
TWEET_EXPANSIONS = ['entities.mentions.username', 'referenced_tweets.id.author_id']
|
2022-09-25 03:39:15 -07:00
|
|
|
|
|
|
|
|
# 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']
|
2023-01-13 02:55:25 -08:00
|
|
|
#
|
|
|
|
|
# VALUES IN TUPLE ARE NONE OR INT.
|
2022-09-24 17:56:58 -07:00
|
|
|
@staticmethod
|
2023-01-11 17:11:41 -08:00
|
|
|
def get_mrq(response):
|
|
|
|
|
tweet = response.data
|
|
|
|
|
|
2022-09-25 03:39:15 -07:00
|
|
|
mentions = set()
|
|
|
|
|
reply_to = None
|
|
|
|
|
qrt = None
|
|
|
|
|
|
2022-09-24 17:56:58 -07:00
|
|
|
# mentions
|
|
|
|
|
try:
|
|
|
|
|
mention_list = tweet.entities['mentions']
|
|
|
|
|
for mention in mention_list:
|
2022-09-25 03:39:15 -07: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:
|
2022-09-25 03:39:15 -07:00
|
|
|
reply_to = tweet.in_reply_to_user_id
|
2022-09-24 17:56:58 -07:00
|
|
|
# qrt
|
2022-09-25 03:39:15 -07:00
|
|
|
if tweet.referenced_tweets:
|
|
|
|
|
for ref_tweet in tweet.referenced_tweets:
|
2022-09-24 17:56:58 -07:00
|
|
|
if ref_tweet.type == 'quoted':
|
|
|
|
|
for incl_tweet in response.includes['tweets']:
|
|
|
|
|
if incl_tweet.id == ref_tweet.id:
|
2022-09-25 03:39:15 -07:00
|
|
|
qrt = incl_tweet.author_id
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
mentions.remove(reply_to)
|
2022-10-01 14:09:14 -07:00
|
|
|
except: pass
|
|
|
|
|
try:
|
2022-09-25 03:39:15 -07:00
|
|
|
mentions.remove(qrt)
|
|
|
|
|
except: pass
|
2023-01-13 02:55:25 -08:00
|
|
|
|
|
|
|
|
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-25 03:39:15 -07:00
|
|
|
|
|
|
|
|
return (mentions, reply_to, qrt)
|
2022-09-24 17:56:58 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
TwAPI.instance = self
|
|
|
|
|
self.client = tweepy.Client(
|
|
|
|
|
bearer_token=api_secrets.bearer_token(),
|
|
|
|
|
consumer_key=api_secrets.api_key(), consumer_secret=api_secrets.api_secret(),
|
|
|
|
|
access_token=api_secrets.access_token(), access_token_secret=api_secrets.access_secret()
|
|
|
|
|
)
|
2022-09-27 02:49:03 -07:00
|
|
|
self.api = tweepy.API(
|
|
|
|
|
auth=tweepy.OAuthHandler(
|
|
|
|
|
consumer_key=api_secrets.api_key(), consumer_secret=api_secrets.api_secret(),
|
|
|
|
|
access_token=api_secrets.access_token(), access_token_secret=api_secrets.access_secret()
|
|
|
|
|
)
|
|
|
|
|
)
|
2022-09-28 20:00:02 -07:00
|
|
|
try:
|
|
|
|
|
self.me = self.client.get_me().data
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print('Did you setup secrets.ini?')
|
|
|
|
|
raise e
|
2022-09-27 15:09:09 -07:00
|
|
|
print(f'Assuming the account of @{self.me.data["username"]} ({self.me["id"]})')
|
2022-09-24 17:56:58 -07:00
|
|
|
|
2022-09-27 15:09:09 -07:00
|
|
|
## ---[COMMENT OUT WHEN NOT IN USE]---
|
2023-01-11 17:11:41 -08:00
|
|
|
# async def nuke_tweets(self):
|
|
|
|
|
# async def delete_tweet(id):
|
|
|
|
|
# try:
|
|
|
|
|
# self.client.delete_tweet(id)
|
|
|
|
|
# except tweepy.TooManyRequests as e:
|
|
|
|
|
# wait_for = float(e.response.headers["x-rate-limit-reset"]) - datetime.datetime.now().timestamp() + 1
|
|
|
|
|
# print(f'\thit rate limit deleting {id}, retrying in {wait_for} seconds...')
|
|
|
|
|
# await asyncio.sleep(wait_for)
|
|
|
|
|
# print('continuing...')
|
|
|
|
|
# await delete_tweet(id)
|
2022-09-27 15:09:09 -07:00
|
|
|
|
2023-01-11 17:11:41 -08:00
|
|
|
# print(f'Retrieving all of {self.me["username"]}\'s tweets...')
|
|
|
|
|
# tweets = self.get_all_tweet_ids_from_user(self.me['id'])
|
2022-09-27 15:09:09 -07:00
|
|
|
|
2023-01-11 17:11:41 -08:00
|
|
|
# print(f'Retrieved {len(tweets)} tweets.')
|
|
|
|
|
# if not len(tweets) > 0:
|
|
|
|
|
# print('No tweets obtained. Make sure the profile is public.')
|
|
|
|
|
# return
|
2022-09-27 15:09:09 -07:00
|
|
|
|
2023-01-11 17:11:41 -08:00
|
|
|
# print(f'Deleting {len(tweets)} tweets...')
|
|
|
|
|
# deleted_count = 0
|
|
|
|
|
# try:
|
|
|
|
|
# for tweet in tweets:
|
|
|
|
|
# print(f'deleted {deleted_count}/{len(tweets)}')
|
|
|
|
|
# await delete_tweet(tweet.id)
|
|
|
|
|
# await asyncio.sleep(0.5)
|
|
|
|
|
# deleted_count += 1
|
|
|
|
|
# except:
|
|
|
|
|
# print('Unhandled error occurred while trying to delete tweets.')
|
|
|
|
|
# traceback.print_exc()
|
|
|
|
|
# print('Try running again.')
|
|
|
|
|
# else:
|
|
|
|
|
# print('Saul Gone')
|
2022-09-27 15:09:09 -07:00
|
|
|
|
|
|
|
|
def get_all_tweet_ids_from_user(self, user_id):
|
|
|
|
|
next_page_token = None
|
|
|
|
|
tokens_retrieved = 0
|
|
|
|
|
tweets_retrieved = 0
|
|
|
|
|
tweets = list()
|
|
|
|
|
while True:
|
|
|
|
|
print(f'Retrieved {tokens_retrieved} tokens so far...')
|
|
|
|
|
resp = self.client.get_users_tweets(
|
|
|
|
|
user_id, max_results=100, pagination_token=next_page_token,
|
|
|
|
|
media_fields=TwAPI.TWEET_MEDIA_FIELDS,
|
|
|
|
|
tweet_fields=TwAPI.TWEET_FIELDS,
|
|
|
|
|
expansions=TwAPI.TWEET_EXPANSIONS
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for tweet in resp.data:
|
|
|
|
|
tweets.append(tweet)
|
|
|
|
|
|
|
|
|
|
# update counters and pagination token
|
|
|
|
|
tweets_retrieved += resp.meta['result_count']
|
|
|
|
|
try:
|
|
|
|
|
next_page_token = resp.meta['next_token']
|
|
|
|
|
tokens_retrieved += 1
|
|
|
|
|
except KeyError:
|
|
|
|
|
print("next_token wasn't provided; we've reached the end!")
|
|
|
|
|
break # reached end of user's tweets
|
|
|
|
|
|
|
|
|
|
print(f'Retrieved {tweets_retrieved} tweets using {tokens_retrieved} tokens.')
|
|
|
|
|
return tweets
|
|
|
|
|
|
2022-09-26 02:44:26 -07:00
|
|
|
async def get_tweet_response(self, id, attempt = 0):
|
|
|
|
|
try:
|
2022-09-27 02:49:03 -07:00
|
|
|
twt = TwAPI.instance.client.get_tweet(
|
2022-09-26 02:44:26 -07:00
|
|
|
id,
|
|
|
|
|
media_fields=TwAPI.TWEET_MEDIA_FIELDS,
|
|
|
|
|
tweet_fields=TwAPI.TWEET_FIELDS,
|
|
|
|
|
expansions=TwAPI.TWEET_EXPANSIONS
|
|
|
|
|
)
|
2022-09-27 02:49:03 -07:00
|
|
|
TwAPI.tweets_fetched += 1
|
|
|
|
|
return twt
|
|
|
|
|
except tweepy.TooManyRequests as e:
|
|
|
|
|
wait_for = float(e.response.headers["x-rate-limit-reset"]) - datetime.datetime.now().timestamp() + 1
|
|
|
|
|
print(f'[{attempt}]\tget_tweet_response({id}):\n\thit rate limit after {TwAPI.tweets_fetched} fetches -- trying again in {wait_for} seconds...')
|
|
|
|
|
await asyncio.sleep(wait_for)
|
2022-09-26 02:44:26 -07:00
|
|
|
return await self.get_tweet_response(id, attempt=attempt+1)
|
2022-09-24 17:56:58 -07:00
|
|
|
|
2023-01-11 17:11:41 -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:
|
2023-01-14 01:18:15 -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:
|
|
|
|
|
wait_for = float(e.response.headers["x-rate-limit-reset"]) - datetime.datetime.now().timestamp() + 1
|
|
|
|
|
print(f'\thit rate limit -- attempting to create Tweet again in {wait_for} seconds...')
|
|
|
|
|
await asyncio.sleep(wait_for)
|
2022-09-28 13:33:31 -07:00
|
|
|
return await self.post_tweet(text=text, media_ids=media_ids, reply_to_tweet=reply_to_tweet)
|
2022-09-27 02:49:03 -07:00
|
|
|
|
|
|
|
|
async def get_ttweet_image_media_id(self, ttweet):
|
2022-09-25 03:39:15 -07:00
|
|
|
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
|
|
|
|
|
|
2022-09-28 20:00:02 -07:00
|
|
|
# return True = successfully posted a single ttweet
|
|
|
|
|
# return False = did not post ttweet (duplicate)
|
2023-01-11 17:11:41 -08:00
|
|
|
async def post_ttweet(self, ttweet: tt.TalentTweet, is_catchup=False, dry_run=False):
|
2022-09-27 15:09:09 -07:00
|
|
|
print(f'------{ttweet.tweet_id} ({util.get_username_local(ttweet.author_id)})------')
|
|
|
|
|
|
2023-01-11 22:50:47 -08:00
|
|
|
REPLY = '{0} replied to {1}!\n'
|
|
|
|
|
QUOTE_TWEET = '{0} quote tweeted {1}!\n'
|
|
|
|
|
TWEET = '{0} tweeted!\n'
|
|
|
|
|
RETWEET = '{0} retweeted {1}!\n'
|
2022-09-27 02:49:03 -07:00
|
|
|
|
|
|
|
|
def create_text():
|
2023-01-14 01:18:15 -08:00
|
|
|
author_username = f'@/{util.get_username_with_company(ttweet.author_id)}'
|
2023-01-11 17:11:41 -08:00
|
|
|
print_mention_ids = set(ttweet.mentions)
|
2022-09-27 15:09:09 -07:00
|
|
|
ret = str()
|
|
|
|
|
if is_catchup:
|
2022-09-27 17:40:48 -07:00
|
|
|
ret += f'{ttweet.get_datetime_str()}\n'
|
2022-09-27 15:09:09 -07:00
|
|
|
pass
|
2022-10-01 13:33:20 -07:00
|
|
|
|
2022-09-27 17:40:48 -07:00
|
|
|
# Tweet types
|
2023-01-11 17:11:41 -08:00
|
|
|
if ttweet.rt_target is not None: # retweet
|
2023-01-14 01:18:15 -08:00
|
|
|
ret += RETWEET.format(f'{author_username}', f'@/{util.get_username_with_company(ttweet.rt_author_id)}')
|
2023-01-11 17:11:41 -08:00
|
|
|
elif ttweet.reply_to is not None: # reply
|
2023-01-14 01:18:15 -08:00
|
|
|
reply_username = f'@/{util.get_username_with_company(ttweet.reply_to)}'
|
2023-01-11 17:11:41 -08:00
|
|
|
ret += REPLY.format(author_username, reply_username)
|
|
|
|
|
# if qrt, push id into mentions
|
|
|
|
|
print_mention_ids.add(ttweet.quote_retweeted)
|
|
|
|
|
elif ttweet.quote_retweeted is not None: # qrt
|
2023-01-14 01:18:15 -08:00
|
|
|
quoted_username = f'@/{util.get_username_with_company(ttweet.quote_retweeted)}'
|
2023-01-11 17:11:41 -08:00
|
|
|
ret += QUOTE_TWEET.format(author_username, quoted_username)
|
|
|
|
|
elif len(ttweet.mentions) > 0: # standalone tweet
|
|
|
|
|
ret += TWEET.format(author_username)
|
2022-09-27 15:09:09 -07:00
|
|
|
else:
|
|
|
|
|
raise ValueError(f'TalentTweet {ttweet.tweet_id} has insufficient other parties')
|
|
|
|
|
|
2023-01-11 17:11:41 -08:00
|
|
|
try: print_mention_ids.remove(None)
|
|
|
|
|
except: pass
|
|
|
|
|
|
2022-09-27 15:09:09 -07:00
|
|
|
# mention line
|
2023-01-11 17:11:41 -08:00
|
|
|
if len(print_mention_ids) > 0:
|
2023-01-14 01:18:15 -08:00
|
|
|
mention_usernames = [f'@/{util.get_username_with_company(x)}' for x in print_mention_ids]
|
2022-09-27 02:49:03 -07:00
|
|
|
ret += (
|
2022-09-27 17:40:48 -07:00
|
|
|
'mentioning '
|
2023-01-13 02:55:25 -08:00
|
|
|
f'{", ".join(mention_usernames)}\n'
|
2022-09-27 02:49:03 -07:00
|
|
|
)
|
2022-11-22 01:44:59 -08:00
|
|
|
ret += '\n'
|
2023-01-14 01:18:15 -08:00
|
|
|
# ret += '(this is a missed tweet)\n' if is_catchup else ''
|
2022-09-27 15:09:09 -07:00
|
|
|
return ret
|
2022-09-27 02:49:03 -07:00
|
|
|
|
|
|
|
|
text = create_text()
|
2023-01-11 17:11:41 -08:00
|
|
|
ttweet_url = util.ttweet_to_url(ttweet)
|
|
|
|
|
|
2023-01-14 01:18:15 -08:00
|
|
|
if dry_run: print('-------------------- DRY RUN --------------------')
|
|
|
|
|
print(text)
|
|
|
|
|
if dry_run: return
|
|
|
|
|
|
|
|
|
|
# NO DRY-RUN: actually post tweet
|
|
|
|
|
# main tweet: text + screenshot
|
|
|
|
|
try:
|
|
|
|
|
print('creating main QRT w/ screenshot...', end='')
|
|
|
|
|
media_ids = [await self.get_ttweet_image_media_id(ttweet)]
|
|
|
|
|
twt_resp = await self.post_tweet(text, media_ids=media_ids, quote_tweet_id=ttweet.tweet_id)
|
|
|
|
|
print('done')
|
|
|
|
|
except:
|
|
|
|
|
print('error occurred trying to create main tweet, falling back to URL-main + reply screencap format')
|
|
|
|
|
text += f"\n{ttweet_url}"
|
2022-10-06 14:18:25 -07:00
|
|
|
try:
|
2023-01-14 01:18:15 -08:00
|
|
|
print('posting main tweet...', end='')
|
|
|
|
|
twt_resp = await self.post_tweet(text)
|
2022-10-06 14:18:25 -07:00
|
|
|
print('done')
|
2023-01-14 01:18:15 -08:00
|
|
|
twt_id = twt_resp.data['id']
|
|
|
|
|
# if ttweet.reply_to is not None:
|
|
|
|
|
# re_ttweet = tt.TalentTweet(tweet_id=ttweet.reply_to, author_id=)
|
|
|
|
|
# media_ids.insert(0, await self.get_ttweet_image_media_id())
|
|
|
|
|
|
2023-01-11 17:11:41 -08:00
|
|
|
try:
|
2023-01-14 01:18:15 -08:00
|
|
|
print('creating reply img...', end='')
|
|
|
|
|
media_ids = [await self.get_ttweet_image_media_id(ttweet)]
|
|
|
|
|
print('posting reply tweet...', end='')
|
|
|
|
|
await self.post_tweet(reply_to_tweet=twt_id, media_ids=media_ids)
|
2023-01-11 17:11:41 -08:00
|
|
|
print('done')
|
2023-01-14 01:18:15 -08:00
|
|
|
except:
|
|
|
|
|
print('Had trouble posting reply image tweet.')
|
|
|
|
|
print('successfully posted ttweet!')
|
|
|
|
|
except tweepy.Forbidden as e:
|
|
|
|
|
if 'duplicate content' in e.api_messages[0]:
|
|
|
|
|
print('Twitter says the TalentTweet is a duplicate; skipping error-free...')
|
|
|
|
|
return False
|
|
|
|
|
else:
|
|
|
|
|
raise e
|
2023-01-14 02:13:03 -08:00
|
|
|
return True
|
2023-01-14 01:18:15 -08:00
|
|
|
|
|
|
|
|
def post_ttweet_by_id(self, tweet_id, is_catchup=False, dry_run=False):
|
|
|
|
|
ttweet = asyncio.run(tt.TalentTweet.create_from_id(tweet_id))
|
|
|
|
|
print(f'm({ttweet.mentions}), r({ttweet.reply_to}), q({ttweet.quote_retweeted})')
|
|
|
|
|
if ttweet.is_cross_company():
|
|
|
|
|
print(f'Tweet {ttweet.tweet_id} is cross-company! Creating post...')
|
|
|
|
|
asyncio.run(self.post_ttweet(ttweet, is_catchup=is_catchup, dry_run=dry_run))
|
|
|
|
|
ttq.TalentTweetQueue.instance.add_finished_tweet(ttweet.tweet_id)
|
|
|
|
|
else:
|
|
|
|
|
print(f'Tweet {tweet_id} is not cross-company.')
|
2022-09-27 02:49:03 -07:00
|
|
|
|
2022-09-24 17:56:58 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-09-29 07:44:21 +01:00
|
|
|
|