Files
NijiHolo_EN_ID_Bot/src/catchup.py
T

162 lines
5.5 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-26 14:44:46 -07:00
import datetime
2022-09-28 11:51:30 -07:00
import asyncio
import shutil
2022-09-24 17:56:58 -07:00
import twint
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
2022-09-27 02:49:03 -07:00
PROGRAM_ARGS = None
2022-09-28 02:20:06 -07:00
safe_to_post_tweets = True
errored = False
2022-09-24 17:56:58 -07:00
## Returns the ID of all tweets (up to limit) from a user ID.
def get_user_tweets(id, since_date=None, limit=None):
global safe_to_post_tweets
2022-09-27 02:49:03 -07:00
qrt_count = 0
2022-09-24 17:56:58 -07:00
tweets = list()
c = twint.Config()
c.User_id = id
c.Limit = limit
c.Store_object = True
c.Store_object_tweets_list = tweets
c.Hide_output = True
c.Since = '' if since_date == None else f'{since_date} 00:00:00'
2022-09-24 17:56:58 -07:00
2022-09-28 02:20:06 -07:00
user_str = f'@{util.get_username_local(id)}'
2022-09-27 02:49:03 -07:00
print(f'Scraping tweets from {user_str} since {"forever ago" if c.Since == "" else c.Since}...')
2022-09-25 18:31:50 -07:00
try:
twint.run.Search(c)
except:
print(f'Had trouble getting tweets from {user_str}')
safe_to_post_tweets = False
2022-09-28 02:20:06 -07:00
traceback.print_exc()
2022-09-27 02:49:03 -07:00
for twt in tweets:
2022-09-28 02:20:06 -07:00
if type(twt.quote_url) is str and twt.quote_url != '':
2022-09-27 02:49:03 -07:00
qrt_count += 1
2022-09-27 02:49:03 -07:00
print(f'Scraped {len(tweets)} tweets, {qrt_count} of which are quote tweets.')
return tweets
# Returns a list of sorted and filtered TalentTweets (should
# be equivalent to queue.txt)
async def get_cross_talent_tweets():
2022-09-28 02:20:06 -07:00
global safe_to_post_tweets
2022-09-24 17:56:58 -07:00
queue = ttq.TalentTweetQueue.instance
2022-09-28 02:20:06 -07:00
# Begin getting tweets from online
print('Pulling tweets from online!')
try:
for i, (talent_id, talent_username) in enumerate(talent_lists.talents.items()):
print(f'[{i+1}/{len(talent_lists.talents)}] {talent_username}-----------------------------------')
try:
tweets = get_user_tweets(talent_id, since_date=queue.finished_user_dates.get(talent_id, None))
for tweet in tweets:
if tweet.id not in queue.ttweets_dict and tweet.id not in queue.finished_ttweets:
ttweet = await tt.TalentTweet.create_from_twint_tweet(tweet)
if ttweet.is_cross_company():
queue.add_ttweet(ttweet)
except:
print('Error occurred processing tweet data.')
safe_to_post_tweets = False
print(traceback.format_exc())
queue.finished_user_dates[talent_id] = '2000-01-01'
else:
queue.finished_user_dates[talent_id] = util.get_current_date()
except:
print('Unhandled error occurred while pulling tweets.')
traceback.print_exc()
safe_to_post_tweets = False
else:
print('Successfully saved all tweets from online!')
queue.save_file()
2022-09-28 13:33:31 -07:00
# return False = errored or we posted at least one ttweet
# return True = we didn't post a single ttweet
async def process_queue() -> bool:
2022-09-27 02:49:03 -07:00
global PROGRAM_ARGS
global errored
2022-11-22 01:44:59 -08:00
WAIT_TIME = 60*3
2022-09-27 15:09:09 -07:00
ttweets_posted = 0
2022-09-28 13:33:31 -07:00
errored = False
2022-09-27 02:49:03 -07:00
queue = ttq.TalentTweetQueue.instance
queued_ttweets_count = queue.get_count()
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
if PROGRAM_ARGS.announce_catchup:
2022-09-29 01:20:42 -07:00
TwAPI.instance.post_tweet(text=f'Starting to catch up through {queued_ttweets_count} logged tweets.')
2022-09-27 02:49:03 -07:00
try:
while not queue.is_empty():
ttweet = queue.get_next_ttweet()
tweet_was_successful = await TwAPI.instance.post_ttweet(ttweet, is_catchup=True)
2022-10-01 13:33:20 -07:00
print('running queue.good()...')
queue.good()
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)
2022-09-27 02:49:03 -07:00
except:
print('Unhandled error occurred while posting tweets from queue.')
2022-09-28 13:33:31 -07:00
errored = True
2022-09-27 02:49:03 -07:00
traceback.print_exc()
else:
if PROGRAM_ARGS.announce_catchup:
await TwAPI.instance.post_tweet('Finished with catch-up tweets!')
2022-09-28 13:33:31 -07:00
if errored or ttweets_posted > 0:
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
2022-09-27 02:49:03 -07:00
async def run(program_args):
global PROGRAM_ARGS
global errored
2022-09-28 02:20:06 -07:00
global safe_to_post_tweets
2022-09-27 02:49:03 -07:00
PROGRAM_ARGS = program_args
2022-09-28 11:51:30 -07:00
ret = None
queue = ttq.TalentTweetQueue.instance
2022-09-27 15:09:09 -07:00
while True:
await get_cross_talent_tweets()
print(f'{queue.get_count()} cross-company tweets to attempt sharing.')
2022-09-28 13:33:31 -07:00
try:
if safe_to_post_tweets:
if await process_queue():
2022-09-28 13:33:31 -07:00
print('Posted no new tweets; we\'re caught up!')
return True
else:
print('Tweets were not retrieved cleanly.')
return False
except:
print('Unhandled error occurred while running catch up in posting phase.')
traceback.print_exc()
return False
if errored:
2022-09-29 07:44:21 +01:00
return False