Files
NijiHolo_EN_ID_Bot/src/catchup.py
T

181 lines
6.4 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
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
2022-09-28 02:20:06 -07:00
# Begin getting tweets from online
print('Pulling tweets from online!')
try:
2023-08-18 18:20:53 -07:00
for i, (talent_id, talent_username) in enumerate(talents.items()):
print(f'[{i+1}/{len(talents)}] {talent_username}-----------------------------------')
try:
2023-08-17 02:28:29 -07:00
since_date = queue.finished_user_dates.get(talent_id, None)
ttweets = scraper.get_cross_ttweets_from_user(talent_username, since_date=since_date)
2023-08-18 01:34:25 -07:00
print(f'got {len(ttweets)} TalentTweets')
2023-08-17 02:28:29 -07:00
for ttweet in ttweets:
2023-08-18 01:34:25 -07:00
if ttweet.tweet_id not in queue.finished_ttweets \
2023-08-17 02:28:29 -07:00
and ttweet.is_cross_company():
queue.add_ttweet(ttweet)
except KeyboardInterrupt as e:
raise e
2023-08-27 02:55:18 -07:00
except Exception as e:
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:
2023-08-17 02:28:29 -07:00
print('Interrupting tweet pulling... NOTE: remaining dates in queue file will not be updated!')
queue.save_file()
2023-08-21 03:05:22 -07:00
raise e
except:
print('Unhandled error occurred while pulling tweets.')
traceback.print_exc()
2023-08-27 02:55:18 -07:00
with open("error_catchup.txt", "a") as f:
traceback.print_exc(file=f)
safe_to_post_tweets = False
else:
print('Successfully saved all tweets from online!')
queue.save_file()
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:
2023-08-27 02:55:18 -07:00
'''
Go through the queue and post stored TalentTweets.
'''
2023-08-18 18:20:53 -07:00
global scraper
global queue
queued_ttweets_count = queue.get_count()
2023-08-18 01:34:25 -07:00
WAIT_TIME = 60*15
2022-09-27 15:09:09 -07:00
ttweets_posted = 0
if queued_ttweets_count == 0:
2022-10-04 13:43:05 -07:00
print('Posting queue is empty!')
return True
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:
print('skipping finished tweet...')
queue.good(ttweet.tweet_id)
continue
2023-08-18 01:34:25 -07:00
tweet_was_successful = await TwAPI.instance.post_ttweet(ttweet)
2022-10-01 13:33:20 -07: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
2022-10-01 13:33:20 -07:00
print(f'({ttweets_posted}/{queued_ttweets_count}) done')
if not queue.is_empty():
print(f'resting for {WAIT_TIME}s...')
2022-10-01 13:33:20 -07:00
await asyncio.sleep(WAIT_TIME-5)
print('5 second warning!')
await asyncio.sleep(5)
2023-08-27 02:55:18 -07:00
except Exception as e:
2022-09-27 02:49:03 -07:00
print('Unhandled error occurred while posting tweets from queue.')
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
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()
print('Posting specified tweets first.')
for id in PROGRAM_ARGS.post_id:
try:
i = int(id)
except ValueError:
print(f'Invalid tweet {id}!')
continue
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')
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
print('Refreshing queue tweets...')
for id in queue.ttweets_dict:
t = scraper.get_tweet(id, queue.ttweets_dict[id].author_id in privated_accounts)
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:
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")
else:
2023-08-18 01:34:25 -07: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:
2023-08-21 02:43:18 -07: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:
2023-08-18 01:34:25 -07:00
print('Interrupting queue processing...')
2023-08-21 03:05:22 -07:00
raise e
2023-08-18 01:34:25 -07:00
except:
print('Unhandled error occurred while running catch up in posting phase.')
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
2023-08-27 02:55:18 -07:00
print('Processing queue first before fetching tweets...')
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:
print('Interrupt received. Ending catchup mode...')
return False