diff --git a/src/catchup.py b/src/catchup.py index c7d6e0a..748ef82 100644 --- a/src/catchup.py +++ b/src/catchup.py @@ -14,6 +14,8 @@ from twapi import TwAPI import talenttweet as tt import ttweetqueue as ttq +PROGRAM_ARGS = None + safe_to_post_tweets = True errored = False @@ -61,7 +63,7 @@ async def get_cross_tweets_online(): # return False = errored or we posted at least one ttweet # return True = we didn't post a single ttweet -async def process_queue() -> bool: +async def process_queue(priority_tweet_ids: list[str]=None) -> bool: global errored global scraper global queue @@ -119,12 +121,37 @@ async def run(PROGRAM_ARGS): queue.ttweets_dict[id] = tt.TalentTweet.create_from_tweety(t) queue.save_file() + # post tweets given in command line first + if PROGRAM_ARGS.post_id is not None and len(PROGRAM_ARGS.post_id) > 0: + PROGRAM_ARGS.post_id.sort() + print('Posting specified tweets first.') + for id in PROGRAM_ARGS.post_id: + try: + i = int(id) + except ValueError: + print(f'Invalid tweet {id}!') + continue + + if i not in queue.finished_ttweets: + posted = await TwAPI.instance.post_ttweet_by_id(i) + if posted: + queue.add_finished_tweet(i) + print('Successfully posted tweet. Sleeping for 5 minutes') + await asyncio.sleep(60*5) + else: + print('Did not post tweet') + else: + print('Tweet was already finished') + + print('Done processing specified tweets') + PROGRAM_ARGS.post_id = None + async def queue_loop(): while True: - print(f'{queue.get_count()} cross-company tweets to attempt sharing.') + print(f'{queue.get_count()} cross-company tweets to announce.') try: if safe_to_post_tweets: - if await process_queue(): + if await process_queue(PROGRAM_ARGS.post_id): print('Posted no new tweets; we\'re caught up!') return True else: diff --git a/src/main.py b/src/main.py index 74c219d..b996d38 100644 --- a/src/main.py +++ b/src/main.py @@ -24,6 +24,7 @@ def init_argparse(): p.add_argument('--no-listen', action='store_true', help='Run one scraping-posting cycle without waiting to run again.') p.add_argument('--refresh-queue', action='store_true', help='Refresh the details on each tweet currently in queue.') p.add_argument('--straight-to-queue', action='store_true', help='Go through queue first before attempting to pull tweets.') + p.add_argument('--post-id', action='append', help='ID of a tweet to try and post right away. Specify multiple to post multiple tweets in a row.') return p def command_line(): diff --git a/src/talenttweet.py b/src/talenttweet.py index 91f1b39..c9cb097 100644 --- a/src/talenttweet.py +++ b/src/talenttweet.py @@ -111,6 +111,7 @@ class TalentTweet: # filter users to only be talents self.mentions = {x for x in mrq[0] if x in tl.talents} self.rt_mentions = {x for x in rt_mentions if x in tl.talents} + self.mentions.difference_update(self.rt_mentions) self.reply_to = mrq[1] self.quote_tweeted = mrq[2] @@ -171,7 +172,7 @@ class TalentTweet: def get_datetime_str(self): unpad = '#' if platform.system() == 'Windows' else '-' - return self.date_time.strftime(f'%{unpad}I:%M%p (%Z) · %b %{unpad}d, %Y') + return self.date_time.strftime(f'%b %{unpad}d, %Y · %{unpad}I:%M%p (%Z)') def announce_text(self): # templates @@ -207,6 +208,7 @@ class TalentTweet: rtm_msg(RETWEET_MENTIONS_B, rt_username) else: ret += RETWEET.format(author_username, rt_username) + print_mention_ids.clear() elif self.reply_to is not None: # reply reply_username = f'@/{util.get_username_with_company(self.reply_to)}' if self.reply_to != -1 else None if len(self.rt_mentions) > 0: @@ -221,8 +223,6 @@ class TalentTweet: ret += QUOTE_TWEET.format(author_username, quoted_username) elif len(self.mentions) > 0: # standalone tweet ret += TWEET.format(author_username, ", ".join(mention_usernames)) - f'[{self.get_datetime_str()}]\n' - return ret else: raise ValueError(f'TalentTweet {self.tweet_id} has insufficient other parties') @@ -233,5 +233,6 @@ class TalentTweet: f'{", ".join(mention_usernames)}' ) + # date ret += f'\n\n{self.get_datetime_str()}' return ret diff --git a/src/twapi.py b/src/twapi.py index ee4f82b..5491d64 100644 --- a/src/twapi.py +++ b/src/twapi.py @@ -147,3 +147,21 @@ class TwAPI: else: raise e return True + + async def post_ttweet_by_id(self, id: int): + from scraper import Scraper + + print(f'Manually posting tweet {id}') + s = Scraper() + t = s.get_tweet(id, True) + if not t: + print('Tweet could not be retrieved') + return False + + ttweet = tt.TalentTweet.create_from_tweety(t) + if not ttweet.is_cross_company(): + print(f'{ttweet.username}/{ttweet.tweet_id} is not cross-company!') + return False + + print(f'Posting {ttweet.username}/{ttweet.tweet_id}...') + return await self.post_ttweet(ttweet) \ No newline at end of file