fix listen mode, various logic

This commit is contained in:
msk
2022-10-01 13:33:20 -07:00
parent 595906d15f
commit ac23c63f78
6 changed files with 49 additions and 32 deletions
+8 -5
View File
@@ -105,20 +105,23 @@ async def process_queue() -> bool:
try: try:
while len(queue.ttweets_dict) > 0: while len(queue.ttweets_dict) > 0:
print(f'({ttweets_posted+1}/{queued_ttweets_count})')
key = list(queue.ttweets_dict.keys())[0] key = list(queue.ttweets_dict.keys())[0]
ttweet = queue.ttweets_dict[key] ttweet = queue.ttweets_dict[key]
queue.good = False queue.good = False
tweet_was_successful = await TwAPI.instance.post_ttweet(ttweet, is_catchup=True) tweet_was_successful = await TwAPI.instance.post_ttweet(ttweet, is_catchup=True)
queue.ttweets_dict.pop(key) queue.ttweets_dict.pop(key)
print('saving new queue...')
queue.good = True
queue.save_file()
if tweet_was_successful: if tweet_was_successful:
print('saving new queue...')
queue.good = True
queue.save_file()
ttweets_posted += 1 ttweets_posted += 1
print(f'({ttweets_posted}/{queued_ttweets_count}) done')
if len(queue.ttweets_dict) > 0: if len(queue.ttweets_dict) > 0:
print(f'resting for {WAIT_TIME}s...') print(f'resting for {WAIT_TIME}s...')
await asyncio.sleep(WAIT_TIME) await asyncio.sleep(WAIT_TIME-5)
print('5 second warning!')
await asyncio.sleep(5)
except: except:
print('Unhandled error occurred while posting tweets from queue.') print('Unhandled error occurred while posting tweets from queue.')
errored = True errored = True
+10 -4
View File
@@ -10,10 +10,16 @@ import api_secrets
import talent_lists as tl import talent_lists as tl
def on_response(resp): def on_response(resp):
print(resp) try:
print(resp.data) ttweet = TalentTweet.create_from_v2api_response(resp)
ttweet = TalentTweet.create_from_v2api_response(resp) except ValueError as e:
asyncio.run(TwAPI.instance.post_ttweet(ttweet)) if "insufficient other parties" in str(e):
print('Tweet has no other parties, probably not a cross-company interaction.')
return
if ttweet.is_cross_company():
print('Tweet is cross-company! Creating post...')
asyncio.run(TwAPI.instance.post_ttweet(ttweet))
def run(): def run():
sc = tweepy.StreamingClient(api_secrets.bearer_token()) sc = tweepy.StreamingClient(api_secrets.bearer_token())
+7 -5
View File
@@ -3,7 +3,7 @@ import platform
import pytz import pytz
from twapi import * import twapi
import talent_lists import talent_lists
import util import util
@@ -69,13 +69,14 @@ class TalentTweet:
if quoted_id == -1: if quoted_id == -1:
quoted_id = util.get_user_id_online(quoted_username) quoted_id = util.get_user_id_online(quoted_username)
# FIXME: resultant tweets don't show timezone properly
date_time = datetime.datetime.strptime(tweet.datetime, '%Y-%m-%d %H:%M:%S %Z') date_time = datetime.datetime.strptime(tweet.datetime, '%Y-%m-%d %H:%M:%S %Z')
return TalentTweet(tweet_id=tweet.id, author_id=tweet.user_id, date_time=date_time, mrq=(mentions, reply_to, quoted_id)) return TalentTweet(tweet_id=tweet.id, author_id=tweet.user_id, date_time=date_time, mrq=(mentions, reply_to, quoted_id))
@staticmethod @staticmethod
def create_from_v2api_response(resp): def create_from_v2api_response(resp):
tweet = resp.data tweet = resp.data
mrq = TwAPI.get_mrq(tweet, resp) mrq = twapi.TwAPI.get_mrq(tweet, resp)
rt_target = None rt_target = None
rt_author_id = None rt_author_id = None
@@ -99,7 +100,7 @@ class TalentTweet:
@staticmethod @staticmethod
async def create_from_id(id): async def create_from_id(id):
resp = await TwAPI.instance.get_tweet_response(id) resp = await twapi.TwAPI.instance.get_tweet_response(id)
return TalentTweet.create_from_v2api_response(resp) return TalentTweet.create_from_v2api_response(resp)
def __init__(self, tweet_id: int, author_id: int, date_time: datetime.datetime, mrq: tuple, rt_target: int=None, rt_author_id: int=None): def __init__(self, tweet_id: int, author_id: int, date_time: datetime.datetime, mrq: tuple, rt_target: int=None, rt_author_id: int=None):
@@ -115,9 +116,10 @@ class TalentTweet:
self.all_parties.update(self.mentions) self.all_parties.update(self.mentions)
try: try:
self.all_parties.remove(None) self.all_parties.remove(None)
except: pass
try:
self.all_parties.remove(self.author_id) self.all_parties.remove(self.author_id)
except: except: pass
pass
def __repr__(self) -> str: def __repr__(self) -> str:
+2
View File
@@ -85,3 +85,5 @@ class TalentTweetQueue:
if self.good: if self.good:
print('Ended in good state, deleting backup queue...') print('Ended in good state, deleting backup queue...')
os.remove(self.queue_backup_path) os.remove(self.queue_backup_path)
else:
print('Ended in bad state, keeping backup queue.')
+19 -15
View File
@@ -174,43 +174,45 @@ class TwAPI:
async def post_ttweet(self, ttweet: tt.TalentTweet, is_catchup=False): async def post_ttweet(self, ttweet: tt.TalentTweet, is_catchup=False):
print(f'------{ttweet.tweet_id} ({util.get_username_local(ttweet.author_id)})------') print(f'------{ttweet.tweet_id} ({util.get_username_local(ttweet.author_id)})------')
REPLY = '{0} replied to {1}!\n' REPLY = '{0} {1}replied to {2}!\n'
QUOTE_TWEET = '{0} quote tweeted {1}!\n' QUOTE_TWEET = '{0} {1}quote tweeted {2}!\n'
TWEET = '{0} tweeted!\n' TWEET = '{0} {1}tweeted!\n'
RETWEET = '{0} retweeted {1}!\n' RETWEET = '{0} {1}retweeted {2}!\n'
def create_text(): def create_text():
author_username = f'@/{util.get_username_local(ttweet.author_id)}' author_username = f'@/{util.get_username_local(ttweet.author_id)}'
mention_ids = set() mention_ids = set()
ret = str() ret = str()
just = ''
if is_catchup: if is_catchup:
# ret += '[catch-up tweet]\n'
ret += f'{ttweet.get_datetime_str()}\n' ret += f'{ttweet.get_datetime_str()}\n'
pass pass
else:
just = 'just '
# Tweet types # Tweet types
if ttweet.rt_target is not None: # standalone tweet if ttweet.rt_target is not None: # standalone tweet
ret += RETWEET.format(author_username, f'@/{util.get_username(ttweet.rt_author_id)}') ret += RETWEET.format(author_username, just, f'@/{util.get_username(ttweet.rt_author_id)}')
mention_ids.clear() mention_ids.clear()
elif ttweet.reply_to is not None: # reply (w/ qrt; push it into mentions) elif ttweet.reply_to is not None: # reply (w/ qrt; push it into mentions)
reply_username = f'@/{util.get_username_local(ttweet.reply_to)}' reply_username = f'@/{util.get_username(ttweet.reply_to)}'
ret += REPLY.format(author_username, reply_username) ret += REPLY.format(author_username, just, reply_username)
mention_ids = set(ttweet.mentions) mention_ids = set(ttweet.mentions)
mention_ids.add(ttweet.quote_retweeted) mention_ids.add(ttweet.quote_retweeted)
try: mention_ids.remove(None) try: mention_ids.remove(None)
except: pass except: pass
elif ttweet.quote_retweeted is not None: # standalone qrt elif ttweet.quote_retweeted is not None: # standalone qrt
quoted_username = f'@/{util.get_username_local(ttweet.quote_retweeted)}' quoted_username = f'@/{util.get_username(ttweet.quote_retweeted)}'
ret += QUOTE_TWEET.format(author_username, quoted_username) ret += QUOTE_TWEET.format(author_username, just, quoted_username)
elif len(ttweet.mentions) > 0: # standalone tweet w/ mentions elif len(ttweet.mentions) > 0: # standalone tweet w/ mentions
ret += TWEET.format(author_username) ret += TWEET.format(author_username, just)
else: else:
raise ValueError(f'TalentTweet {ttweet.tweet_id} has insufficient other parties') raise ValueError(f'TalentTweet {ttweet.tweet_id} has insufficient other parties')
# mention line # mention line
if len(mention_ids) > 0: if len(mention_ids) > 0:
mention_usernames = [f'@/{util.get_username_local(x)}' for x in mention_ids] mention_usernames = [f'@/{util.get_username(x)}' for x in mention_ids]
ret += ( ret += (
'mentioning ' 'mentioning '
f'{" ".join(mention_usernames)}\n' f'{" ".join(mention_usernames)}\n'
@@ -222,16 +224,18 @@ class TwAPI:
try: try:
# print('creating reply img') # print('creating reply img')
# media_ids = [await self.get_ttweet_image_media_id(ttweet)] # media_ids = [await self.get_ttweet_image_media_id(ttweet)]
print('posting main tweet') print('posting main tweet...', end='')
twt_resp = await self.post_tweet(text) twt_resp = await self.post_tweet(text)
print('done')
twt_id = twt_resp.data['id'] twt_id = twt_resp.data['id']
print('creating reply img') print('creating reply img...', end='')
media_ids = [await self.get_ttweet_image_media_id(ttweet)] media_ids = [await self.get_ttweet_image_media_id(ttweet)]
# if ttweet.reply_to is not None: # if ttweet.reply_to is not None:
# re_ttweet = tt.TalentTweet(tweet_id=ttweet.reply_to, author_id=) # re_ttweet = tt.TalentTweet(tweet_id=ttweet.reply_to, author_id=)
# media_ids.insert(0, await self.get_ttweet_image_media_id()) # media_ids.insert(0, await self.get_ttweet_image_media_id())
print('posting reply tweet') print('posting reply tweet...', end='')
await self.post_tweet(reply_to_tweet=twt_id, media_ids=media_ids,) await self.post_tweet(reply_to_tweet=twt_id, media_ids=media_ids,)
print('done')
print('successfully posted ttweet!') print('successfully posted ttweet!')
return True return True
except tweepy.Forbidden as e: except tweepy.Forbidden as e:
+1 -1
View File
@@ -51,7 +51,7 @@ async def create_ttweet_image(ttweet):
filename = f'{get_project_dir()}/img.png' filename = f'{get_project_dir()}/img.png'
url = ttweet_to_url(ttweet) url = ttweet_to_url(ttweet)
img = None img = None
print(url)
try: os.remove(filename) try: os.remove(filename)
except: pass except: pass
try: try: