From ac23c63f78440508cefbe2b58a1f8ac960a6d884 Mon Sep 17 00:00:00 2001 From: msk <15199219+muskit@users.noreply.github.com> Date: Sat, 1 Oct 2022 13:33:20 -0700 Subject: [PATCH] fix listen mode, various logic --- src/catchup.py | 13 ++++++++----- src/listen.py | 14 ++++++++++---- src/talenttweet.py | 12 +++++++----- src/ttweetqueue.py | 4 +++- src/twapi.py | 36 ++++++++++++++++++++---------------- src/util.py | 2 +- 6 files changed, 49 insertions(+), 32 deletions(-) diff --git a/src/catchup.py b/src/catchup.py index 745a6ac..e38a779 100644 --- a/src/catchup.py +++ b/src/catchup.py @@ -105,20 +105,23 @@ async def process_queue() -> bool: try: while len(queue.ttweets_dict) > 0: - print(f'({ttweets_posted+1}/{queued_ttweets_count})') key = list(queue.ttweets_dict.keys())[0] ttweet = queue.ttweets_dict[key] queue.good = False tweet_was_successful = await TwAPI.instance.post_ttweet(ttweet, is_catchup=True) queue.ttweets_dict.pop(key) + + print('saving new queue...') + queue.good = True + queue.save_file() if tweet_was_successful: - print('saving new queue...') - queue.good = True - queue.save_file() ttweets_posted += 1 + print(f'({ttweets_posted}/{queued_ttweets_count}) done') if len(queue.ttweets_dict) > 0: print(f'resting for {WAIT_TIME}s...') - await asyncio.sleep(WAIT_TIME) + await asyncio.sleep(WAIT_TIME-5) + print('5 second warning!') + await asyncio.sleep(5) except: print('Unhandled error occurred while posting tweets from queue.') errored = True diff --git a/src/listen.py b/src/listen.py index fcec95f..e0b64da 100644 --- a/src/listen.py +++ b/src/listen.py @@ -10,10 +10,16 @@ import api_secrets import talent_lists as tl def on_response(resp): - print(resp) - print(resp.data) - ttweet = TalentTweet.create_from_v2api_response(resp) - asyncio.run(TwAPI.instance.post_ttweet(ttweet)) + try: + ttweet = TalentTweet.create_from_v2api_response(resp) + except ValueError as e: + if "insufficient other parties" in str(e): + print('Tweet has no other parties, probably not a cross-company interaction.') + return + + if ttweet.is_cross_company(): + print('Tweet is cross-company! Creating post...') + asyncio.run(TwAPI.instance.post_ttweet(ttweet)) def run(): sc = tweepy.StreamingClient(api_secrets.bearer_token()) diff --git a/src/talenttweet.py b/src/talenttweet.py index 6db1765..9faac63 100644 --- a/src/talenttweet.py +++ b/src/talenttweet.py @@ -3,7 +3,7 @@ import platform import pytz -from twapi import * +import twapi import talent_lists import util @@ -69,13 +69,14 @@ class TalentTweet: if quoted_id == -1: quoted_id = util.get_user_id_online(quoted_username) + # FIXME: resultant tweets don't show timezone properly date_time = datetime.datetime.strptime(tweet.datetime, '%Y-%m-%d %H:%M:%S %Z') return TalentTweet(tweet_id=tweet.id, author_id=tweet.user_id, date_time=date_time, mrq=(mentions, reply_to, quoted_id)) @staticmethod def create_from_v2api_response(resp): tweet = resp.data - mrq = TwAPI.get_mrq(tweet, resp) + mrq = twapi.TwAPI.get_mrq(tweet, resp) rt_target = None rt_author_id = None @@ -99,7 +100,7 @@ class TalentTweet: @staticmethod async def create_from_id(id): - resp = await TwAPI.instance.get_tweet_response(id) + resp = await twapi.TwAPI.instance.get_tweet_response(id) return TalentTweet.create_from_v2api_response(resp) def __init__(self, tweet_id: int, author_id: int, date_time: datetime.datetime, mrq: tuple, rt_target: int=None, rt_author_id: int=None): @@ -115,9 +116,10 @@ class TalentTweet: self.all_parties.update(self.mentions) try: self.all_parties.remove(None) + except: pass + try: self.all_parties.remove(self.author_id) - except: - pass + except: pass def __repr__(self) -> str: diff --git a/src/ttweetqueue.py b/src/ttweetqueue.py index 0e3c33c..3c804f1 100644 --- a/src/ttweetqueue.py +++ b/src/ttweetqueue.py @@ -84,4 +84,6 @@ class TalentTweetQueue: def __del__(self): if self.good: print('Ended in good state, deleting backup queue...') - os.remove(self.queue_backup_path) \ No newline at end of file + os.remove(self.queue_backup_path) + else: + print('Ended in bad state, keeping backup queue.') \ No newline at end of file diff --git a/src/twapi.py b/src/twapi.py index 43b5a6b..ca4aa3a 100644 --- a/src/twapi.py +++ b/src/twapi.py @@ -174,43 +174,45 @@ class TwAPI: async def post_ttweet(self, ttweet: tt.TalentTweet, is_catchup=False): print(f'------{ttweet.tweet_id} ({util.get_username_local(ttweet.author_id)})------') - REPLY = '{0} replied to {1}!\n' - QUOTE_TWEET = '{0} quote tweeted {1}!\n' - TWEET = '{0} tweeted!\n' - RETWEET = '{0} retweeted {1}!\n' + REPLY = '{0} {1}replied to {2}!\n' + QUOTE_TWEET = '{0} {1}quote tweeted {2}!\n' + TWEET = '{0} {1}tweeted!\n' + RETWEET = '{0} {1}retweeted {2}!\n' def create_text(): author_username = f'@/{util.get_username_local(ttweet.author_id)}' mention_ids = set() ret = str() + just = '' if is_catchup: - # ret += '[catch-up tweet]\n' ret += f'{ttweet.get_datetime_str()}\n' pass - + else: + just = 'just ' + # Tweet types if ttweet.rt_target is not None: # standalone tweet - ret += RETWEET.format(author_username, f'@/{util.get_username(ttweet.rt_author_id)}') + ret += RETWEET.format(author_username, just, f'@/{util.get_username(ttweet.rt_author_id)}') mention_ids.clear() elif ttweet.reply_to is not None: # reply (w/ qrt; push it into mentions) - reply_username = f'@/{util.get_username_local(ttweet.reply_to)}' - ret += REPLY.format(author_username, reply_username) + reply_username = f'@/{util.get_username(ttweet.reply_to)}' + ret += REPLY.format(author_username, just, reply_username) mention_ids = set(ttweet.mentions) mention_ids.add(ttweet.quote_retweeted) try: mention_ids.remove(None) except: pass elif ttweet.quote_retweeted is not None: # standalone qrt - quoted_username = f'@/{util.get_username_local(ttweet.quote_retweeted)}' - ret += QUOTE_TWEET.format(author_username, quoted_username) + quoted_username = f'@/{util.get_username(ttweet.quote_retweeted)}' + ret += QUOTE_TWEET.format(author_username, just, quoted_username) elif len(ttweet.mentions) > 0: # standalone tweet w/ mentions - ret += TWEET.format(author_username) + ret += TWEET.format(author_username, just) else: raise ValueError(f'TalentTweet {ttweet.tweet_id} has insufficient other parties') # mention line if len(mention_ids) > 0: - mention_usernames = [f'@/{util.get_username_local(x)}' for x in mention_ids] + mention_usernames = [f'@/{util.get_username(x)}' for x in mention_ids] ret += ( 'mentioning ' f'{" ".join(mention_usernames)}\n' @@ -222,16 +224,18 @@ class TwAPI: try: # print('creating reply img') # media_ids = [await self.get_ttweet_image_media_id(ttweet)] - print('posting main tweet') + print('posting main tweet...', end='') twt_resp = await self.post_tweet(text) + print('done') twt_id = twt_resp.data['id'] - print('creating reply img') + print('creating reply img...', end='') 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...', end='') await self.post_tweet(reply_to_tweet=twt_id, media_ids=media_ids,) + print('done') print('successfully posted ttweet!') return True except tweepy.Forbidden as e: diff --git a/src/util.py b/src/util.py index 817efae..fb82570 100644 --- a/src/util.py +++ b/src/util.py @@ -51,7 +51,7 @@ async def create_ttweet_image(ttweet): filename = f'{get_project_dir()}/img.png' url = ttweet_to_url(ttweet) img = None - + print(url) try: os.remove(filename) except: pass try: