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
|
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
|
2022-09-28 20:00:02 -07:00
|
|
|
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
|
2022-09-28 20:00:02 -07:00
|
|
|
errored = False
|
2022-09-24 17:56:58 -07:00
|
|
|
|
|
|
|
|
## Returns the ID of all tweets (up to limit) from a user ID.
|
2022-09-27 02:49:03 -07:00
|
|
|
def get_user_tweets(id, since_timestamp=None, limit=None):
|
2022-10-02 04:57:24 -07:00
|
|
|
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
|
2022-09-25 03:39:15 -07:00
|
|
|
c.Hide_output = True
|
2022-09-27 02:49:03 -07:00
|
|
|
c.Since = '' if since_timestamp == None else util.timestamp_to_tdate(since_timestamp)
|
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}')
|
2022-09-28 20:00:02 -07:00
|
|
|
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-26 02:44:26 -07:00
|
|
|
|
2022-09-27 02:49:03 -07:00
|
|
|
print(f'Scraped {len(tweets)} tweets, {qrt_count} of which are quote tweets.')
|
2022-09-26 03:50:11 -07:00
|
|
|
return tweets
|
2022-09-26 02:44:26 -07:00
|
|
|
|
|
|
|
|
# Returns a list of sorted and filtered TalentTweets (should
|
|
|
|
|
# be equivalent to queue.txt)
|
2022-09-28 20:00:02 -07:00
|
|
|
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
|
|
|
|
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:
|
|
|
|
|
tweets = get_user_tweets(talent_id, since_timestamp=queue.finished_user_timestamps.get(talent_id, None))
|
|
|
|
|
for tweet in tweets:
|
2022-10-02 04:57:24 -07:00
|
|
|
if tweet.id not in queue.ttweets_dict and tweet.id not in queue.finished_ttweets:
|
2022-09-28 20:00:02 -07:00
|
|
|
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_timestamps[talent_id] = -1
|
|
|
|
|
else:
|
|
|
|
|
queue.finished_user_timestamps[talent_id] = util.get_current_timestamp()
|
|
|
|
|
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:
|
2022-09-27 02:49:03 -07:00
|
|
|
global PROGRAM_ARGS
|
2022-09-28 20:00:02 -07:00
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
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:
|
2022-10-02 04:57:24 -07:00
|
|
|
while not queue.is_empty():
|
|
|
|
|
ttweet = queue.get_next_ttweet()
|
2022-09-28 20:00:02 -07:00
|
|
|
tweet_was_successful = await TwAPI.instance.post_ttweet(ttweet, is_catchup=True)
|
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()
|
|
|
|
|
else:
|
|
|
|
|
if PROGRAM_ARGS.announce_catchup:
|
|
|
|
|
await TwAPI.instance.post_tweet('Finished with catch-up tweets!')
|
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
|
2022-09-27 02:49:03 -07:00
|
|
|
async def run(program_args):
|
|
|
|
|
global 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-27 02:49:03 -07:00
|
|
|
PROGRAM_ARGS = program_args
|
2022-09-28 11:51:30 -07:00
|
|
|
|
|
|
|
|
ret = None
|
2022-10-02 04:57:24 -07:00
|
|
|
queue = ttq.TalentTweetQueue.instance
|
2022-09-27 15:09:09 -07:00
|
|
|
while True:
|
2022-10-02 04:57:24 -07:00
|
|
|
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:
|
2022-09-28 20:00:02 -07:00
|
|
|
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()
|
2022-09-28 20:00:02 -07:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
if errored:
|
2022-09-29 07:44:21 +01:00
|
|
|
return False
|