From 0203578987d152cd1646cc8f3465828e11fcf2c9 Mon Sep 17 00:00:00 2001 From: muskit <15199219+muskit@users.noreply.github.com> Date: Sun, 21 Apr 2024 16:04:33 -0700 Subject: [PATCH] add preempt tweet list handling --- src/catchup.py | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/catchup.py b/src/catchup.py index 77c2c5b..45f5467 100644 --- a/src/catchup.py +++ b/src/catchup.py @@ -16,8 +16,9 @@ import ttweetqueue as ttq PROGRAM_ARGS = None +preempt_done = False safe_to_post_tweets = True -scraper: Scraper +scraper = Scraper() # Updates TTweetQueue @@ -127,13 +128,13 @@ async def process_queue() -> bool: # return False = issue occurred where we couldn't post all past tweets properly async def run(PROGRAM_ARGS): global safe_to_post_tweets + global preempt_done global scraper global queue - scraper = Scraper() queue = ttq.TalentTweetQueue.instance - # post tweets given in command line first + # OPTION: 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.") @@ -150,11 +151,38 @@ async def run(PROGRAM_ARGS): print("Successfully posted tweet. Sleeping for 5 minutes") await asyncio.sleep(60 * 5) else: - print("Did not post tweet") + print("Did not post tweet\n") print("Done processing specified tweets") PROGRAM_ARGS.post_id = None - # refresh stored queue first + # PREEMPT: post tweet IDs in preempt.txt if exists and not empty + if not preempt_done: + try: + with open(working_path(file="preempt.txt"), "r") as preempt_file: + print("Found preempt.txt! Posting stored IDs unconditionally...") + + for l in preempt_file: + if len(l) == 0: continue + try: + id = int(l.strip().split()[0]) + except: + print(f"Error occurred processing {l}, skipping...") + continue + + posted = await TwAPI.instance.post_ttweet_by_id(id, PROGRAM_ARGS.dry_run) + if posted: + queue.add_finished_tweet(id) + print("Successfully posted tweet. Sleeping for 5 minutes") + await asyncio.sleep(60 * 5) + else: + print("Could not post tweet\n") + + print("Finished processing preempt.txt") + preempt_done = True + except FileNotFoundError: + print("preempt.txt wasn't found") + + # OPTION: refresh stored queue first if PROGRAM_ARGS.refresh_queue: PROGRAM_ARGS.refresh_queue = False print("Refreshing queue tweets...")