fixups and refinements

This commit is contained in:
msk
2022-09-28 13:33:31 -07:00
parent c155edd260
commit d2fc4a4d44
4 changed files with 44 additions and 23 deletions
+21 -8
View File
@@ -145,10 +145,13 @@ async def get_cross_talent_tweets(queue_path):
return ttweets_dict return ttweets_dict
# Return number of TalentTweets successfully posted # return False = errored or we posted at least one ttweet
async def process_queue(ttweets_dict: dict) -> int: # return True = we didn't post a single ttweet
async def process_queue(ttweets_dict: dict) -> bool:
global PROGRAM_ARGS global PROGRAM_ARGS
WAIT_TIME = 30
ttweets_posted = 0 ttweets_posted = 0
errored = False
if len(ttweets_dict) == 0: return ttweets_posted if len(ttweets_dict) == 0: return ttweets_posted
@@ -161,12 +164,13 @@ async def process_queue(ttweets_dict: dict) -> int:
ttweet = ttweets_dict[key] ttweet = ttweets_dict[key]
if await TwAPI.instance.post_ttweet(ttweet, is_catchup=True): if await TwAPI.instance.post_ttweet(ttweet, is_catchup=True):
ttweets_posted += 1 ttweets_posted += 1
print('resting for 60s...') print(f'resting for {WAIT_TIME}s...')
await asyncio.sleep(60) await asyncio.sleep(WAIT_TIME)
ttweets_dict.pop(key) ttweets_dict.pop(key)
# TODO: add ttweet.tweet_id to some success list # TODO: add ttweet.tweet_id to some success list
except: except:
print('Unhandled error occurred while posting tweets from queue.') print('Unhandled error occurred while posting tweets from queue.')
errored = True
traceback.print_exc() traceback.print_exc()
else: else:
if PROGRAM_ARGS.announce_catchup: if PROGRAM_ARGS.announce_catchup:
@@ -180,7 +184,9 @@ async def process_queue(ttweets_dict: dict) -> int:
for ttweet in ttweets_dict.values(): for ttweet in ttweets_dict.values():
f.write(f'{ttweet.serialize()}\n') f.write(f'{ttweet.serialize()}\n')
return ttweets_posted if errored or ttweets_posted > 0:
return False
return True
# return True = no problems # return True = no problems
# return False = issue occurred where we couldn't post all past tweets properly # return False = issue occurred where we couldn't post all past tweets properly
@@ -195,8 +201,10 @@ async def run(program_args):
queue_path = get_queue_path() queue_path = get_queue_path()
if os.path.exists(queue_backup): if os.path.exists(queue_backup):
print('Found old backup queue! We errored in the previous run.')
shutil.copyfile(queue_backup, queue_path) shutil.copyfile(queue_backup, queue_path)
else: else:
print('Creating backup queue...')
shutil.copyfile(queue_path, queue_backup) shutil.copyfile(queue_path, queue_backup)
ret = None ret = None
@@ -204,12 +212,17 @@ async def run(program_args):
while True: while True:
ttweets_dict = await get_cross_talent_tweets(queue_path) ttweets_dict = await get_cross_talent_tweets(queue_path)
print(f'found {len(ttweets_dict)} cross-company tweets') print(f'found {len(ttweets_dict)} cross-company tweets')
try:
if safe_to_post_tweets: if safe_to_post_tweets:
if await process_queue(ttweets_dict) == 0:
print('Posted no new tweets; we\'re caught up!')
os.remove(queue_backup) # keep updated queue os.remove(queue_backup) # keep updated queue
if await process_queue(ttweets_dict):
print('Posted no new tweets; we\'re caught up!')
return True return True
else: else:
print('Tweets were not retrieved cleanly.') print('Tweets were not retrieved cleanly.')
os.remove(queue_path) # keep backup queue # os.remove(queue_path) # keep backup queue
return False
except:
print('Unhandled error occurred while running catch up in posting phase.')
traceback.print_exc()
return False return False
+3 -2
View File
@@ -1,6 +1,7 @@
## The bot's listen mode ## The bot's listen mode
# Continuously listen for cross-company interactions. # Continuously listen for cross-company interactions.
import asyncio
import tweepy import tweepy
from talenttweet import TalentTweet from talenttweet import TalentTweet
@@ -8,11 +9,11 @@ from twapi import TwAPI
import api_secrets import api_secrets
import talent_lists as tl import talent_lists as tl
async def on_response(resp): def on_response(resp):
print(resp) print(resp)
print(resp.data) print(resp.data)
ttweet = TalentTweet.create_from_v2api_response(resp) ttweet = TalentTweet.create_from_v2api_response(resp)
await TwAPI.instance.post_ttweet(ttweet) asyncio.run(TwAPI.instance.post_ttweet(ttweet))
def run(): def run():
sc = tweepy.StreamingClient(api_secrets.bearer_token()) sc = tweepy.StreamingClient(api_secrets.bearer_token())
+8 -5
View File
@@ -150,15 +150,15 @@ 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_id=None, reply_to_tweet: int=None): async def post_tweet(self, text='', media_ids: list=None, reply_to_tweet: int=None):
try: try:
tweet = self.client.create_tweet(text=text, media_ids=None if media_id == None else [media_id], in_reply_to_tweet_id=reply_to_tweet) 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)
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
print(f'\thit rate limit -- attempting to create Tweet again in {wait_for} seconds...') print(f'\thit rate limit -- attempting to create Tweet again in {wait_for} seconds...')
await asyncio.sleep(wait_for) await asyncio.sleep(wait_for)
return await self.post_tweet(text=text, media_id=media_id, reply_to_tweet=reply_to_tweet) return await self.post_tweet(text=text, media_ids=media_ids, reply_to_tweet=reply_to_tweet)
async def get_ttweet_image_media_id(self, ttweet): async def get_ttweet_image_media_id(self, ttweet):
img = await util.create_ttweet_image(ttweet) img = await util.create_ttweet_image(ttweet)
@@ -218,9 +218,12 @@ class TwAPI:
twt_resp = await self.post_tweet(text) twt_resp = await self.post_tweet(text)
twt_id = twt_resp.data['id'] twt_id = twt_resp.data['id']
print('creating reply img') print('creating reply img')
media_id = await self.get_ttweet_image_media_id(ttweet) media_ids = [await self.get_ttweet_image_media_id(ttweet)]
# 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())
print('posting reply tweet') print('posting reply tweet')
await self.post_tweet(reply_to_tweet=twt_id, media_id=media_id,) await self.post_tweet(reply_to_tweet=twt_id, media_ids=media_ids,)
print('successfully posted ttweet!') print('successfully posted ttweet!')
return True return True
except tweepy.Forbidden as e: except tweepy.Forbidden as e:
+7 -3
View File
@@ -59,9 +59,12 @@ async def create_ttweet_image(ttweet):
print(f'successfully saved {img}') print(f'successfully saved {img}')
return img return img
def get_tweet_url(id, username):
return f'https://twitter.com/{username}/status/{id}'
def ttweet_to_url(ttweet): def ttweet_to_url(ttweet):
username = get_username_online(ttweet.author_id) username = get_username(ttweet.author_id)
return f'https://twitter.com/{username}/status/{ttweet.tweet_id}' return get_tweet_url(ttweet.tweet_id, username)
def get_username_local(id): def get_username_local(id):
return talent_lists.talents.get(id, f'{id}') return talent_lists.talents.get(id, f'{id}')
@@ -90,13 +93,14 @@ def get_username_online(id, default=None):
except: except:
print(f'Unhandled error retrieving username for {id}!') print(f'Unhandled error retrieving username for {id}!')
traceback.print_exc() traceback.print_exc()
return str(default) if default is not None else f'{id}' return str(default) if default is not None else f'id:{id}'
## Attempt to pull username from local; pull from online if doesn't exist. ## Attempt to pull username from local; pull from online if doesn't exist.
def get_username(id): def get_username(id):
ret = talent_lists.talents.get(id, None) ret = talent_lists.talents.get(id, None)
if ret == None: if ret == None:
return get_username_online(id) return get_username_online(id)
return ret
def get_user_id_local(username) -> int: def get_user_id_local(username) -> int:
talent_usernames = list(talent_lists.talents.values()) talent_usernames = list(talent_lists.talents.values())