Files
NijiHolo_EN_ID_Bot/src/catchup.py
T

202 lines
6.8 KiB
Python
Raw Normal View History

2022-09-24 17:56:58 -07:00
## The bot's catch-up mode
# Scan all accounts for cross-company interactions.
# Terminates when finished scanning and posting.
#
# We should post, at the fastest, one tweet per minute.
import traceback
2022-09-28 11:51:30 -07:00
import asyncio
2022-09-24 17:56:58 -07:00
2023-08-17 02:28:29 -07:00
from scraper import Scraper
2022-09-24 17:56:58 -07:00
from util import *
from talent_lists import *
2022-09-25 18:31:50 -07:00
from twapi import TwAPI
2022-09-24 17:56:58 -07:00
import talenttweet as tt
import ttweetqueue as ttq
2022-09-24 17:56:58 -07:00
PROGRAM_ARGS = None
2023-08-18 01:34:25 -07:00
safe_to_post_tweets = True
2023-08-18 18:20:53 -07:00
scraper: Scraper
2024-01-25 16:29:01 -08:00
2023-08-21 03:05:22 -07:00
# Updates TTweetQueue
2023-08-18 01:34:25 -07:00
async def get_cross_tweets_online():
2022-09-28 02:20:06 -07:00
global safe_to_post_tweets
2023-08-18 18:20:53 -07:00
global queue
global scraper
2023-08-30 02:26:45 -07:00
safe_to_post_tweets = True
2024-01-25 16:29:01 -08:00
dbg_curr_user = ""
2022-09-28 02:20:06 -07:00
# Begin getting tweets from online
2024-01-25 16:29:01 -08:00
print("Pulling tweets from online!")
try:
2023-08-18 18:20:53 -07:00
for i, (talent_id, talent_username) in enumerate(talents.items()):
2024-01-25 16:29:01 -08:00
print(
f"[{i+1}/{len(talents)}] {talent_username}-----------------------------------"
)
dbg_curr_user = f"{talent_id}: {talent_username}"
try:
2023-08-17 02:28:29 -07:00
since_date = queue.finished_user_dates.get(talent_id, None)
2024-01-25 16:29:01 -08:00
ttweets = scraper.get_cross_ttweets_from_user(
talent_username, since_date=since_date
)
print(f"got {len(ttweets)} TalentTweets")
2023-08-17 02:28:29 -07:00
for ttweet in ttweets:
2024-01-25 16:29:01 -08:00
if (
ttweet.tweet_id not in queue.finished_ttweets
and ttweet.is_cross_company()
):
2023-08-17 02:28:29 -07:00
queue.add_ttweet(ttweet)
except KeyboardInterrupt as e:
raise e
2023-08-27 02:55:18 -07:00
except Exception as e:
2024-01-25 16:29:01 -08:00
print("Unhandled error occurred processing tweet data.")
safe_to_post_tweets = False
2023-08-27 02:55:18 -07:00
raise e
else:
2023-08-18 18:20:53 -07:00
queue.finished_user_dates[talent_id] = get_current_date()
2023-08-17 02:28:29 -07:00
queue.save_file()
2023-08-21 03:05:22 -07:00
except KeyboardInterrupt as e:
2024-01-25 16:29:01 -08:00
print(
"Interrupting tweet pulling... NOTE: remaining dates in queue file will not be updated!"
)
2023-08-17 02:28:29 -07:00
queue.save_file()
2023-08-21 03:05:22 -07:00
raise e
except:
2024-01-25 16:29:01 -08:00
print("Unhandled error occurred while pulling tweets.")
traceback.print_exc()
2024-01-25 16:29:01 -08:00
with open(working_path(file="error_catchup.txt"), "a") as f:
f.write(f"Error getting tweets from user {dbg_curr_user}\n")
2023-08-27 02:55:18 -07:00
traceback.print_exc(file=f)
safe_to_post_tweets = False
else:
2024-01-25 16:29:01 -08:00
print("Successfully saved all tweets from online!")
queue.save_file()
2024-01-25 16:29:01 -08:00
2023-08-27 02:55:18 -07:00
# return False = we posted at least one ttweet
2022-09-28 13:33:31 -07:00
# return True = we didn't post a single ttweet
async def process_queue() -> bool:
2024-01-25 16:29:01 -08:00
"""
2023-08-27 02:55:18 -07:00
Go through the queue and post stored TalentTweets.
2024-01-25 16:29:01 -08:00
"""
2023-08-18 18:20:53 -07:00
global scraper
global queue
queued_ttweets_count = queue.get_count()
2024-01-25 16:29:01 -08:00
WAIT_TIME = 60 * 15
2022-09-27 15:09:09 -07:00
ttweets_posted = 0
if queued_ttweets_count == 0:
2024-01-25 16:29:01 -08:00
print("Posting queue is empty!")
return True
2024-01-25 16:29:01 -08:00
2022-09-27 02:49:03 -07:00
try:
while not queue.is_empty():
ttweet = queue.get_next_ttweet()
2023-08-18 22:57:24 -07:00
if ttweet.tweet_id in queue.finished_ttweets:
2024-01-25 16:29:01 -08:00
print("skipping finished tweet...")
2023-08-18 22:57:24 -07:00
queue.good(ttweet.tweet_id)
continue
2023-08-18 01:34:25 -07:00
tweet_was_successful = await TwAPI.instance.post_ttweet(ttweet)
2024-01-25 16:29:01 -08:00
print("running queue.good()...")
2023-08-18 22:57:24 -07:00
queue.good(ttweet.tweet_id)
if tweet_was_successful:
2022-09-27 15:09:09 -07:00
ttweets_posted += 1
2024-01-25 16:29:01 -08:00
print(f"({ttweets_posted}/{queued_ttweets_count}) done")
if not queue.is_empty():
2024-01-25 16:29:01 -08:00
print(f"resting for {WAIT_TIME}s...")
await asyncio.sleep(WAIT_TIME - 5)
print("5 second warning!")
2022-10-01 13:33:20 -07:00
await asyncio.sleep(5)
2023-08-27 02:55:18 -07:00
except Exception as e:
2024-01-25 16:29:01 -08:00
print("Unhandled error occurred while posting tweets from queue.")
2022-09-27 02:49:03 -07:00
traceback.print_exc()
2023-08-27 02:55:18 -07:00
if ttweets_posted > 0:
2022-09-28 13:33:31 -07:00
return False
return True
2024-01-25 16:29:01 -08:00
2022-09-28 02:20:06 -07:00
# return True = no problems
# return False = issue occurred where we couldn't post all past tweets properly
2023-08-18 01:34:25 -07:00
async def run(PROGRAM_ARGS):
2022-09-28 02:20:06 -07:00
global safe_to_post_tweets
2023-08-18 18:20:53 -07:00
global scraper
global queue
2022-09-28 11:51:30 -07:00
2023-08-18 18:20:53 -07:00
scraper = Scraper()
queue = ttq.TalentTweetQueue.instance
2023-08-18 01:34:25 -07:00
2023-08-27 02:55:18 -07:00
# 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()
2024-01-25 16:29:01 -08:00
print("Posting specified tweets first.")
for id in PROGRAM_ARGS.post_id:
try:
i = int(id)
except ValueError:
2024-01-25 16:29:01 -08:00
print(f"Invalid tweet {id}!")
continue
2024-01-25 16:29:01 -08:00
posted = await TwAPI.instance.post_ttweet_by_id(i)
if posted:
queue.add_finished_tweet(i)
2024-01-25 16:29:01 -08:00
print("Successfully posted tweet. Sleeping for 5 minutes")
await asyncio.sleep(60 * 5)
else:
2024-01-25 16:29:01 -08:00
print("Did not post tweet")
print("Done processing specified tweets")
PROGRAM_ARGS.post_id = None
2023-08-27 02:55:18 -07:00
# refresh stored queue first
if PROGRAM_ARGS.refresh_queue:
PROGRAM_ARGS.refresh_queue = False
2024-01-25 16:29:01 -08:00
print("Refreshing queue tweets...")
2023-08-27 02:55:18 -07:00
for id in queue.ttweets_dict:
2024-01-25 16:29:01 -08:00
t = scraper.get_tweet(
id, queue.ttweets_dict[id].author_id in privated_accounts
)
2023-08-27 02:55:18 -07:00
queue.ttweets_dict[id] = tt.TalentTweet.create_from_tweety(t)
queue.save_file()
2023-08-18 01:34:25 -07:00
async def queue_loop():
while True:
2024-01-25 16:29:01 -08:00
print(f"{queue.get_count()} cross-company tweets to announce.")
2023-08-18 01:34:25 -07:00
try:
if safe_to_post_tweets:
if await process_queue():
2023-08-27 02:55:18 -07:00
print("Finished processing queue")
2023-08-28 23:29:27 -07:00
return
2023-08-27 02:55:18 -07:00
else:
2024-01-25 16:29:01 -08:00
print("Posted no new tweets; we're caught up!")
2023-08-27 02:55:18 -07:00
return
2023-08-18 01:34:25 -07:00
else:
2024-01-25 16:29:01 -08:00
print("Tweets were not retrieved cleanly. Not processing queue.")
2023-08-27 02:55:18 -07:00
return
2023-08-21 03:05:22 -07:00
except KeyboardInterrupt as e:
2024-01-25 16:29:01 -08:00
print("Interrupting queue processing...")
2023-08-21 03:05:22 -07:00
raise e
2023-08-18 01:34:25 -07:00
except:
2024-01-25 16:29:01 -08:00
print(
"Unhandled error occurred while running catch up in posting phase."
)
2023-08-18 01:34:25 -07:00
traceback.print_exc()
await get_cross_tweets_online()
2023-08-21 03:05:22 -07:00
try:
if PROGRAM_ARGS.straight_to_queue:
PROGRAM_ARGS.straight_to_queue = False
2024-01-25 16:29:01 -08:00
print("Processing queue first before fetching tweets...")
2023-08-27 02:55:18 -07:00
await queue_loop()
2023-08-21 03:05:22 -07:00
else:
await get_cross_tweets_online()
2023-08-27 02:55:18 -07:00
await queue_loop()
2023-08-21 03:05:22 -07:00
except KeyboardInterrupt:
2024-01-25 16:29:01 -08:00
print("Interrupt received. Ending catchup mode...")
2023-08-28 23:29:27 -07:00
return False