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-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-27 02:49:03 -07:00
|
|
|
PROGRAM_ARGS = None
|
2022-09-26 14:44:46 -07:00
|
|
|
|
2022-09-27 02:49:03 -07:00
|
|
|
def write_user_timestamp(user_id, file, timestamp = None, error = False):
|
|
|
|
|
if timestamp is None:
|
|
|
|
|
timestamp = datetime.datetime.now().timestamp()
|
|
|
|
|
|
|
|
|
|
file.write(f'# {user_id} {timestamp if not error else "-1"}\n')
|
2022-09-26 14:44:46 -07:00
|
|
|
pass
|
|
|
|
|
|
2022-09-27 02:49:03 -07:00
|
|
|
def get_queue_path():
|
2022-09-25 18:31:50 -07:00
|
|
|
return f'{util.get_project_dir()}/queue.txt'
|
|
|
|
|
|
2022-09-24 17:56:58 -07:00
|
|
|
def get_local_queue():
|
|
|
|
|
# f = open(os.path.join(get_project_dir(), 'queue.txt'))
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
## 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):
|
|
|
|
|
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-27 02:49:03 -07:00
|
|
|
user_str = f'{util.get_username_local(id)}'
|
|
|
|
|
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-27 02:49:03 -07:00
|
|
|
|
|
|
|
|
for twt in tweets:
|
|
|
|
|
if twt.quote_url != '':
|
|
|
|
|
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
|
|
|
|
2022-09-27 02:49:03 -07:00
|
|
|
# Returns dict of accounts that successfully caught up.
|
|
|
|
|
# LINE FORMAT: "# {user_id} {status_num} {UNIX_timestamp}
|
|
|
|
|
def get_finished_user_timestamps(queue_file):
|
|
|
|
|
results = dict()
|
|
|
|
|
for line in queue_file:
|
|
|
|
|
tokens = line.split()
|
2022-09-27 15:09:09 -07:00
|
|
|
if len(tokens) == 0: continue
|
|
|
|
|
|
|
|
|
|
if tokens[0][0] != '#':
|
|
|
|
|
print(f'{line} is our stopper!')
|
2022-09-27 02:49:03 -07:00
|
|
|
# reached end of accounts list
|
|
|
|
|
break
|
|
|
|
|
if tokens[2] != '-1':
|
|
|
|
|
results[int(tokens[1])] = float(tokens[2])
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
def get_user_timestamps_str(queue_file):
|
|
|
|
|
results = str()
|
|
|
|
|
for line in queue_file:
|
|
|
|
|
tokens = line.split()
|
|
|
|
|
if len(tokens) != 3 or tokens[0][0] != '#':
|
|
|
|
|
# reached end of accounts list
|
|
|
|
|
break
|
|
|
|
|
results += f'{line}\n'
|
|
|
|
|
return results[:-1]
|
|
|
|
|
|
2022-09-26 02:44:26 -07:00
|
|
|
# If queue.txt doesn't exist, creates and populates it.
|
|
|
|
|
# Returns a list of sorted and filtered TalentTweets (should
|
|
|
|
|
# be equivalent to queue.txt)
|
2022-09-26 14:44:46 -07:00
|
|
|
async def get_cross_talent_tweets(queue_path):
|
2022-09-27 02:49:03 -07:00
|
|
|
finished_user_timestamps = dict()
|
2022-09-26 02:44:26 -07:00
|
|
|
ttweets_dict = dict()
|
2022-09-27 15:09:09 -07:00
|
|
|
posted_ttweets = set() # TODO: don't add TTweet to ttweets_dict if its id exists in posted_ttweets
|
2022-09-24 17:56:58 -07:00
|
|
|
|
2022-09-26 02:44:26 -07:00
|
|
|
# Populate structures with existing data from queue.txt
|
|
|
|
|
try:
|
2022-09-26 14:44:46 -07:00
|
|
|
with open(queue_path, 'r') as f:
|
2022-09-27 15:09:09 -07:00
|
|
|
finished_user_timestamps = get_finished_user_timestamps(f)
|
|
|
|
|
print(finished_user_timestamps)
|
2022-09-26 02:44:26 -07:00
|
|
|
|
2022-09-27 02:49:03 -07:00
|
|
|
# Get existing queued TalentTweets
|
2022-09-26 02:44:26 -07:00
|
|
|
for line in f:
|
|
|
|
|
tokens = line.split()
|
|
|
|
|
if len(tokens) == 0 or tokens[0][0] == '#':
|
|
|
|
|
continue
|
|
|
|
|
ttweet = tt.TalentTweet.deserialize(line)
|
|
|
|
|
ttweets_dict[ttweet.tweet_id] = ttweet
|
2022-09-27 02:49:03 -07:00
|
|
|
print(f'Found {len(finished_user_timestamps)} scraped accounts and {len(ttweets_dict)} tweets.')
|
2022-09-26 02:44:26 -07:00
|
|
|
except FileNotFoundError:
|
2022-09-27 02:49:03 -07:00
|
|
|
print('queue.txt not found.')
|
2022-09-26 02:44:26 -07:00
|
|
|
|
2022-09-26 14:44:46 -07:00
|
|
|
# Pull tweets from twint
|
|
|
|
|
with open(queue_path, 'w') as f:
|
2022-09-27 02:49:03 -07:00
|
|
|
print('Pulling tweets from online!')
|
|
|
|
|
try:
|
|
|
|
|
print('TODO: using test_talents')
|
|
|
|
|
for talent_id in talent_lists.test_talents:
|
|
|
|
|
# for talent_id in talent_lists.talents:
|
|
|
|
|
if talent_id not in finished_user_timestamps or \
|
|
|
|
|
finished_user_timestamps[talent_id] < datetime.datetime.now().timestamp():
|
|
|
|
|
try:
|
|
|
|
|
# tweets = get_user_tweets(talent_id, since_timestamp=1663698621) # shorten test runs
|
|
|
|
|
tweets = get_user_tweets(talent_id, since_timestamp=finished_user_timestamps.get(talent_id, None))
|
|
|
|
|
for tweet in tweets:
|
|
|
|
|
if tweet.id not in ttweets_dict:
|
|
|
|
|
ttweet = await tt.TalentTweet.create_from_twint_tweet(tweet)
|
|
|
|
|
if ttweet.is_cross_company():
|
|
|
|
|
ttweets_dict[ttweet.tweet_id] = ttweet
|
|
|
|
|
except:
|
|
|
|
|
print('Error occurred processing tweet data.')
|
|
|
|
|
print(traceback.format_exc())
|
|
|
|
|
write_user_timestamp(user_id=talent_id, file=f, error=True)
|
|
|
|
|
else:
|
|
|
|
|
write_user_timestamp(user_id=talent_id, file=f)
|
2022-09-26 14:44:46 -07:00
|
|
|
else:
|
2022-09-27 02:49:03 -07:00
|
|
|
print(f'Skipping already completed {util.get_username_local(talent_id)}')
|
|
|
|
|
write_user_timestamp(user_id=talent_id, file=f, timestamp=finished_user_timestamps[talent_id])
|
|
|
|
|
f.write('\n')
|
|
|
|
|
ttweets_dict = dict(sorted(ttweets_dict.items()))
|
|
|
|
|
for ttweet in ttweets_dict.values():
|
|
|
|
|
f.write(f'{ttweet.serialize()}\n')
|
|
|
|
|
except:
|
|
|
|
|
print('Unhandled error occurred while pulling tweets.')
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
print('Saving queue.txt and exiting.')
|
|
|
|
|
exit(1)
|
2022-09-26 14:44:46 -07:00
|
|
|
|
|
|
|
|
return ttweets_dict
|
2022-09-26 02:44:26 -07:00
|
|
|
|
2022-09-27 15:09:09 -07:00
|
|
|
async def process_queue(ttweets_dict: dict) -> int:
|
2022-09-27 02:49:03 -07:00
|
|
|
global PROGRAM_ARGS
|
2022-09-27 15:09:09 -07:00
|
|
|
ttweets_posted = 0
|
2022-09-27 02:49:03 -07:00
|
|
|
|
2022-09-27 15:09:09 -07:00
|
|
|
if len(ttweets_dict) == 0: return ttweets_posted
|
2022-09-27 02:49:03 -07:00
|
|
|
|
|
|
|
|
if PROGRAM_ARGS.announce_catchup:
|
2022-09-27 15:09:09 -07:00
|
|
|
TwAPI.instance.post_tweet(text=f'Starting to catch up through {len(ttweets_dict)} logged tweets.')
|
2022-09-27 02:49:03 -07:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
while len(ttweets_dict) > 0:
|
|
|
|
|
key = list(ttweets_dict.keys())[0]
|
|
|
|
|
ttweet = ttweets_dict[key]
|
2022-09-27 15:09:09 -07:00
|
|
|
if await TwAPI.instance.post_ttweet(ttweet, is_catchup=True):
|
|
|
|
|
ttweets_posted += 1
|
2022-09-27 02:49:03 -07:00
|
|
|
ttweets_dict.pop(key)
|
2022-09-27 15:09:09 -07:00
|
|
|
# TODO: add ttweet.tweet_id to some success list
|
2022-09-27 02:49:03 -07:00
|
|
|
except:
|
|
|
|
|
print('Unhandled error occurred while posting tweets from queue.')
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
else:
|
|
|
|
|
if PROGRAM_ARGS.announce_catchup:
|
|
|
|
|
await TwAPI.instance.post_tweet('Finished with catch-up tweets!')
|
|
|
|
|
|
|
|
|
|
print('Updating what\'s left in ttweet_dict to queue.txt.')
|
|
|
|
|
with open(get_queue_path(), 'r') as f:
|
|
|
|
|
user_timestamps_str = get_user_timestamps_str(f)
|
|
|
|
|
with open(get_queue_path(), 'w') as f:
|
|
|
|
|
f.write(user_timestamps_str + '\n\n')
|
|
|
|
|
for ttweet in ttweets_dict.values():
|
|
|
|
|
f.write(f'{ttweet.serialize()}\n')
|
2022-09-27 15:09:09 -07:00
|
|
|
|
|
|
|
|
return ttweets_posted
|
2022-09-25 03:39:15 -07:00
|
|
|
|
2022-09-27 02:49:03 -07:00
|
|
|
async def run(program_args):
|
|
|
|
|
global PROGRAM_ARGS
|
|
|
|
|
PROGRAM_ARGS = program_args
|
|
|
|
|
queue_path = get_queue_path()
|
2022-09-27 15:09:09 -07:00
|
|
|
while True:
|
|
|
|
|
ttweets_dict = await get_cross_talent_tweets(queue_path)
|
|
|
|
|
print(f'found {len(ttweets_dict)} cross-company tweets')
|
|
|
|
|
if await process_queue(ttweets_dict) == 0:
|
|
|
|
|
print('Posted no new tweets; we\'re caught up!')
|
|
|
|
|
break
|
|
|
|
|
# TODO: go to listen mode
|