fix tweets mentioning cross-company, chromedriver

This commit is contained in:
muskit
2023-01-11 17:11:41 -08:00
parent 4726a20dd1
commit d183d1720c
4 changed files with 103 additions and 83 deletions
+2 -1
View File
@@ -80,7 +80,8 @@ class TalentTweet:
@staticmethod @staticmethod
def create_from_v2api_response(resp): def create_from_v2api_response(resp):
tweet = resp.data tweet = resp.data
mrq = twapi.TwAPI.get_mrq(tweet, resp)
mrq = twapi.TwAPI.get_mrq(resp)
rt_target = None rt_target = None
rt_author_id = None rt_author_id = None
+1 -1
View File
@@ -6,7 +6,7 @@ import util
import talenttweet as tt import talenttweet as tt
# User timestamps line format: # User timestamps line format:
# # {user_id} {status_num} {UNIX_timestamp} # {user_id} {status_num} {UNIX_timestamp}
class TalentTweetQueue: class TalentTweetQueue:
instance = None instance = None
+99 -81
View File
@@ -23,7 +23,9 @@ class TwAPI:
# tweet_fields=['created_at', 'in_reply_to_user_id'], # tweet_fields=['created_at', 'in_reply_to_user_id'],
# expansions=['entities.mentions.username', 'referenced_tweets.id.author_id'] # expansions=['entities.mentions.username', 'referenced_tweets.id.author_id']
@staticmethod @staticmethod
def get_mrq(tweet: tweepy.Tweet, response): def get_mrq(response):
tweet = response.data
mentions = set() mentions = set()
reply_to = None reply_to = None
qrt = None qrt = None
@@ -77,39 +79,39 @@ class TwAPI:
print(f'Assuming the account of @{self.me.data["username"]} ({self.me["id"]})') print(f'Assuming the account of @{self.me.data["username"]} ({self.me["id"]})')
## ---[COMMENT OUT WHEN NOT IN USE]--- ## ---[COMMENT OUT WHEN NOT IN USE]---
async def nuke_tweets(self): # async def nuke_tweets(self):
async def delete_tweet(id): # async def delete_tweet(id):
try: # try:
self.client.delete_tweet(id) # self.client.delete_tweet(id)
except tweepy.TooManyRequests as e: # except tweepy.TooManyRequests as e:
wait_for = float(e.response.headers["x-rate-limit-reset"]) - datetime.datetime.now().timestamp() + 1 # 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...') # print(f'\thit rate limit deleting {id}, retrying in {wait_for} seconds...')
await asyncio.sleep(wait_for) # await asyncio.sleep(wait_for)
print('continuing...') # print('continuing...')
await delete_tweet(id) # await delete_tweet(id)
print(f'Retrieving all of {self.me["username"]}\'s tweets...') # print(f'Retrieving all of {self.me["username"]}\'s tweets...')
tweets = self.get_all_tweet_ids_from_user(self.me['id']) # tweets = self.get_all_tweet_ids_from_user(self.me['id'])
print(f'Retrieved {len(tweets)} tweets.') # print(f'Retrieved {len(tweets)} tweets.')
if not len(tweets) > 0: # if not len(tweets) > 0:
print('No tweets obtained. Make sure the profile is public.') # print('No tweets obtained. Make sure the profile is public.')
return # return
print(f'Deleting {len(tweets)} tweets...') # print(f'Deleting {len(tweets)} tweets...')
deleted_count = 0 # deleted_count = 0
try: # try:
for tweet in tweets: # for tweet in tweets:
print(f'deleted {deleted_count}/{len(tweets)}') # print(f'deleted {deleted_count}/{len(tweets)}')
await delete_tweet(tweet.id) # await delete_tweet(tweet.id)
await asyncio.sleep(0.5) # await asyncio.sleep(0.5)
deleted_count += 1 # deleted_count += 1
except: # except:
print('Unhandled error occurred while trying to delete tweets.') # print('Unhandled error occurred while trying to delete tweets.')
traceback.print_exc() # traceback.print_exc()
print('Try running again.') # print('Try running again.')
else: # else:
print('Saul Gone') # print('Saul Gone')
def get_all_tweet_ids_from_user(self, user_id): def get_all_tweet_ids_from_user(self, user_id):
next_page_token = None next_page_token = None
@@ -156,9 +158,9 @@ class TwAPI:
await asyncio.sleep(wait_for) await asyncio.sleep(wait_for)
return await self.get_tweet_response(id, attempt=attempt+1) return await self.get_tweet_response(id, attempt=attempt+1)
async def post_tweet(self, text='', media_ids: list=None, reply_to_tweet: int=None): async def post_tweet(self, text='', media_ids: list=None, reply_to_tweet: int=None, quote_tweet_id: int=None):
try: try:
tweet = self.client.create_tweet(text=text, media_ids=None if media_ids == None else media_ids, in_reply_to_tweet_id=reply_to_tweet) tweet = self.client.create_tweet(text=text, media_ids=media_ids, in_reply_to_tweet_id=reply_to_tweet, quote_tweet_id=str(quote_tweet_id))
return tweet return tweet
except tweepy.TooManyRequests as e: except tweepy.TooManyRequests as e:
wait_for = float(e.response.headers["x-rate-limit-reset"]) - datetime.datetime.now().timestamp() + 1 wait_for = float(e.response.headers["x-rate-limit-reset"]) - datetime.datetime.now().timestamp() + 1
@@ -173,84 +175,100 @@ class TwAPI:
# return True = successfully posted a single ttweet # return True = successfully posted a single ttweet
# return False = did not post ttweet (duplicate) # return False = did not post ttweet (duplicate)
async def post_ttweet(self, ttweet: tt.TalentTweet, is_catchup=False): async def post_ttweet(self, ttweet: tt.TalentTweet, is_catchup=False, dry_run=False):
print(f'------{ttweet.tweet_id} ({util.get_username_local(ttweet.author_id)})------') print(f'------{ttweet.tweet_id} ({util.get_username_local(ttweet.author_id)})------')
REPLY = '{0} {1}replied to {2}!\n' REPLY = '{0} replied to {1}\n'
QUOTE_TWEET = '{0} {1}quote tweeted {2}!\n' QUOTE_TWEET = '{0} quote tweeted {1}\n'
TWEET = '{0} {1}tweeted!\n' TWEET = '{0} tweeted\n'
RETWEET = '{0} {1}retweeted {2}!\n' RETWEET = '{0} retweeted {1}\n'
def create_text(): def create_text():
author_username = f'@/{util.get_username_local(ttweet.author_id)}' author_username = f'@/{util.get_username_local(ttweet.author_id)}'
mention_ids = set() print_mention_ids = set(ttweet.mentions)
ret = str() ret = str()
just = ''
if is_catchup: if is_catchup:
ret += f'{ttweet.get_datetime_str()}\n' ret += f'{ttweet.get_datetime_str()}\n'
pass pass
else:
just = 'just '
# Tweet types # Tweet types
if ttweet.rt_target is not None: # standalone tweet if ttweet.rt_target is not None: # retweet
ret += RETWEET.format(author_username, just, f'@/{util.get_username(ttweet.rt_author_id)}') ret += RETWEET.format(author_username, f'@/{util.get_username(ttweet.rt_author_id)}')
mention_ids.clear() elif ttweet.reply_to is not None: # reply
elif ttweet.reply_to is not None: # reply (w/ qrt; push it into mentions)
reply_username = f'@/{util.get_username(ttweet.reply_to)}' reply_username = f'@/{util.get_username(ttweet.reply_to)}'
ret += REPLY.format(author_username, just, reply_username) ret += REPLY.format(author_username, reply_username)
# if qrt, push id into mentions
mention_ids = set(ttweet.mentions) print_mention_ids.add(ttweet.quote_retweeted)
mention_ids.add(ttweet.quote_retweeted) elif ttweet.quote_retweeted is not None: # qrt
try: mention_ids.remove(None)
except: pass
elif ttweet.quote_retweeted is not None: # standalone qrt
quoted_username = f'@/{util.get_username(ttweet.quote_retweeted)}' quoted_username = f'@/{util.get_username(ttweet.quote_retweeted)}'
ret += QUOTE_TWEET.format(author_username, just, quoted_username) ret += QUOTE_TWEET.format(author_username, quoted_username)
elif len(ttweet.mentions) > 0: # standalone tweet w/ mentions elif len(ttweet.mentions) > 0: # standalone tweet
ret += TWEET.format(author_username, just) ret += TWEET.format(author_username)
else: else:
raise ValueError(f'TalentTweet {ttweet.tweet_id} has insufficient other parties') raise ValueError(f'TalentTweet {ttweet.tweet_id} has insufficient other parties')
try: print_mention_ids.remove(None)
except: pass
# mention line # mention line
if len(mention_ids) > 0: if len(print_mention_ids) > 0:
mention_usernames = [f'@/{util.get_username(x)}' for x in mention_ids] mention_usernames = [f'@/{util.get_username(x)}' for x in print_mention_ids]
ret += ( ret += (
'mentioning ' 'mentioning '
f'{" ".join(mention_usernames)}\n' f'{" ".join(mention_usernames)}\n'
) )
ret += '\n' ret += '\n'
ret += '(this is a missed tweet)\n' if is_catchup else '' ret += '(this is a missed tweet)\n' if is_catchup else ''
ret += f'{util.ttweet_to_url(ttweet)}'
return ret return ret
text = create_text() text = create_text()
try: ttweet_url = util.ttweet_to_url(ttweet)
# media_ids = [await self.get_ttweet_image_media_id(ttweet)]
print('posting main tweet...', end='') if dry_run: # DRY-RUN: only print tweet
twt_resp = await self.post_tweet(text) print(text)
print('done') print(f'QRT: {ttweet_url}')
twt_id = twt_resp.data['id'] else: # NO DRY-RUN: post actual tweet
# if ttweet.reply_to is not None: # main tweet: text + screenshot
# re_ttweet = tt.TalentTweet(tweet_id=ttweet.reply_to, author_id=)
# media_ids.insert(0, await self.get_ttweet_image_media_id())
try: try:
print('creating reply img...', end='') print('creating main QRT w/ screenshot...', end='')
media_ids = [await self.get_ttweet_image_media_id(ttweet)] media_ids = [await self.get_ttweet_image_media_id(ttweet)]
print('posting reply tweet...', end='') twt_resp = await self.post_tweet(text, media_ids=media_ids, quote_tweet_id=ttweet.tweet_id)
await self.post_tweet(reply_to_tweet=twt_id, media_ids=media_ids,)
print('done') print('done')
# twt_id = twt_resp.data['id']
# try:
# print('posting reply tweet...', end='')
# await self.post_tweet(text=ttweet_url, reply_to_tweet=twt_id)
# print('done')
# except:
# print('Had trouble posting reply tweet.')
except: except:
print('Had trouble posting reply image tweet.') print('error occurred trying to create main tweet, falling back to URL-main + reply format')
print('successfully posted ttweet!') text += f"\n{ttweet_url}"
return True try:
except tweepy.Forbidden as e: print('posting main tweet...', end='')
if 'duplicate content' in e.api_messages[0]: twt_resp = await self.post_tweet(text)
print('Twitter says the TalentTweet is a duplicate; skipping error-free...') print('done')
return False twt_id = twt_resp.data['id']
else: # if ttweet.reply_to is not None:
raise e # re_ttweet = tt.TalentTweet(tweet_id=ttweet.reply_to, author_id=)
# media_ids.insert(0, await self.get_ttweet_image_media_id())
try:
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,)
print('done')
except:
print('Had trouble posting reply image tweet.')
print('successfully posted ttweet!')
return True
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
+1
View File
@@ -49,6 +49,7 @@ def get_key_from_value(d, val):
async def create_ttweet_image(ttweet): async def create_ttweet_image(ttweet):
tc = TweetCapture() tc = TweetCapture()
tc.driver_path = '/usr/bin/chromedriver' # Linux chromedriver path
filename = f'{get_project_dir()}/img.png' filename = f'{get_project_dir()}/img.png'
url = ttweet_to_url(ttweet) url = ttweet_to_url(ttweet)
img = None img = None