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.
|
|
|
|
|
|
2022-09-26 02:44:26 -07:00
|
|
|
import traceback
|
2022-09-26 14:44:46 -07:00
|
|
|
import datetime
|
2022-09-28 11:51:30 -07:00
|
|
|
import asyncio
|
|
|
|
|
import shutil
|
2023-08-17 02:28:29 -07:00
|
|
|
from datetime import datetime
|
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
|
2022-09-28 20:00:02 -07:00
|
|
|
import ttweetqueue as ttq
|
2022-09-24 17:56:58 -07:00
|
|
|
|
2023-08-18 01:34:25 -07:00
|
|
|
safe_to_post_tweets = True
|
2022-09-28 20:00:02 -07:00
|
|
|
errored = False
|
2022-09-24 17:56:58 -07:00
|
|
|
|
2022-09-26 02:44:26 -07:00
|
|
|
# Returns a list of sorted and filtered TalentTweets (should
|
|
|
|
|
# be equivalent to queue.txt)
|
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
|
2022-09-24 17:56:58 -07:00
|
|
|
|
2023-08-17 02:28:29 -07:00
|
|
|
scraper = Scraper()
|
2022-09-28 20:00:02 -07:00
|
|
|
queue = ttq.TalentTweetQueue.instance
|
2022-09-26 02:44:26 -07:00
|
|
|
|
2022-09-28 02:20:06 -07:00
|
|
|
# Begin getting tweets from online
|
2022-09-28 20:00:02 -07:00
|
|
|
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:
|
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
|
2022-09-28 20:00:02 -07:00
|
|
|
except:
|
|
|
|
|
print('Error occurred processing tweet data.')
|
|
|
|
|
safe_to_post_tweets = False
|
2023-08-17 02:28:29 -07:00
|
|
|
traceback.print_exc()
|
2022-09-28 20:00:02 -07:00
|
|
|
else:
|
2023-01-11 22:50:47 -08:00
|
|
|
queue.finished_user_dates[talent_id] = util.get_current_date()
|
2023-08-17 02:28:29 -07:00
|
|
|
queue.save_file()
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
print('Interrupting tweet pulling... NOTE: remaining dates in queue file will not be updated!')
|
|
|
|
|
queue.save_file()
|
2022-09-28 20:00:02 -07:00
|
|
|
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-26 02:44:26 -07:00
|
|
|
|
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
|
2022-09-28 20:00:02 -07:00
|
|
|
async def process_queue() -> bool:
|
|
|
|
|
global errored
|
2023-08-18 01:34:25 -07:00
|
|
|
|
|
|
|
|
WAIT_TIME = 60*15
|
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
|
|
|
|
2022-09-28 20:00:02 -07:00
|
|
|
queue = ttq.TalentTweetQueue.instance
|
2022-10-02 04:57:24 -07:00
|
|
|
queued_ttweets_count = queue.get_count()
|
2022-09-28 20:00:02 -07:00
|
|
|
|
2022-10-02 04:57:24 -07:00
|
|
|
if queued_ttweets_count == 0:
|
2022-10-04 13:43:05 -07:00
|
|
|
print('Posting queue is empty!')
|
2022-10-02 04:57:24 -07:00
|
|
|
return True
|
2022-09-27 02:49:03 -07:00
|
|
|
|
|
|
|
|
try:
|
2022-10-02 04:57:24 -07:00
|
|
|
while not queue.is_empty():
|
|
|
|
|
ttweet = queue.get_next_ttweet()
|
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
|
|
|
|
2022-10-02 04:57:24 -07:00
|
|
|
print('running queue.good()...')
|
|
|
|
|
queue.good()
|
2022-09-28 20:00:02 -07:00
|
|
|
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')
|
2022-10-02 04:57:24 -07:00
|
|
|
if not queue.is_empty():
|
2022-09-28 20:00:02 -07:00
|
|
|
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()
|
2022-10-02 04:57:24 -07:00
|
|
|
|
2022-09-28 13:33:31 -07:00
|
|
|
if errored or ttweets_posted > 0:
|
|
|
|
|
return False
|
|
|
|
|
return True
|
2022-09-25 03:39:15 -07: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 20:00:02 -07:00
|
|
|
global errored
|
2022-09-28 02:20:06 -07:00
|
|
|
global safe_to_post_tweets
|
2022-09-28 11:51:30 -07:00
|
|
|
|
2022-10-02 04:57:24 -07:00
|
|
|
queue = ttq.TalentTweetQueue.instance
|
2023-08-18 01:34:25 -07:00
|
|
|
|
|
|
|
|
async def queue_loop():
|
|
|
|
|
while True:
|
|
|
|
|
print(f'{queue.get_count()} cross-company tweets to attempt sharing.')
|
|
|
|
|
try:
|
|
|
|
|
if safe_to_post_tweets:
|
|
|
|
|
if await process_queue():
|
|
|
|
|
print('Posted no new tweets; we\'re caught up!')
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
print('Tweets were not retrieved cleanly.')
|
|
|
|
|
return False
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
print('Interrupting queue processing...')
|
|
|
|
|
return False
|
|
|
|
|
except:
|
|
|
|
|
print('Unhandled error occurred while running catch up in posting phase.')
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
if errored:
|
2022-09-28 13:33:31 -07:00
|
|
|
return False
|
2023-08-18 01:34:25 -07:00
|
|
|
|
|
|
|
|
await get_cross_tweets_online()
|
|
|
|
|
|
|
|
|
|
if PROGRAM_ARGS.straight_to_queue:
|
|
|
|
|
print('Processing queue first before pulling tweets...')
|
|
|
|
|
return await queue_loop()
|
|
|
|
|
else:
|
|
|
|
|
await get_cross_tweets_online()
|
|
|
|
|
return await queue_loop()
|