fix listen mode, various logic
This commit is contained in:
+6
-3
@@ -105,20 +105,23 @@ async def process_queue() -> bool:
|
||||
|
||||
try:
|
||||
while len(queue.ttweets_dict) > 0:
|
||||
print(f'({ttweets_posted+1}/{queued_ttweets_count})')
|
||||
key = list(queue.ttweets_dict.keys())[0]
|
||||
ttweet = queue.ttweets_dict[key]
|
||||
queue.good = False
|
||||
tweet_was_successful = await TwAPI.instance.post_ttweet(ttweet, is_catchup=True)
|
||||
queue.ttweets_dict.pop(key)
|
||||
if tweet_was_successful:
|
||||
|
||||
print('saving new queue...')
|
||||
queue.good = True
|
||||
queue.save_file()
|
||||
if tweet_was_successful:
|
||||
ttweets_posted += 1
|
||||
print(f'({ttweets_posted}/{queued_ttweets_count}) done')
|
||||
if len(queue.ttweets_dict) > 0:
|
||||
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:
|
||||
print('Unhandled error occurred while posting tweets from queue.')
|
||||
errored = True
|
||||
|
||||
+8
-2
@@ -10,9 +10,15 @@ import api_secrets
|
||||
import talent_lists as tl
|
||||
|
||||
def on_response(resp):
|
||||
print(resp)
|
||||
print(resp.data)
|
||||
try:
|
||||
ttweet = TalentTweet.create_from_v2api_response(resp)
|
||||
except ValueError as e:
|
||||
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():
|
||||
|
||||
+7
-5
@@ -3,7 +3,7 @@ import platform
|
||||
|
||||
import pytz
|
||||
|
||||
from twapi import *
|
||||
import twapi
|
||||
import talent_lists
|
||||
import util
|
||||
|
||||
@@ -69,13 +69,14 @@ class TalentTweet:
|
||||
if quoted_id == -1:
|
||||
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')
|
||||
return TalentTweet(tweet_id=tweet.id, author_id=tweet.user_id, date_time=date_time, mrq=(mentions, reply_to, quoted_id))
|
||||
|
||||
@staticmethod
|
||||
def create_from_v2api_response(resp):
|
||||
tweet = resp.data
|
||||
mrq = TwAPI.get_mrq(tweet, resp)
|
||||
mrq = twapi.TwAPI.get_mrq(tweet, resp)
|
||||
rt_target = None
|
||||
rt_author_id = None
|
||||
|
||||
@@ -99,7 +100,7 @@ class TalentTweet:
|
||||
|
||||
@staticmethod
|
||||
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)
|
||||
|
||||
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)
|
||||
try:
|
||||
self.all_parties.remove(None)
|
||||
except: pass
|
||||
try:
|
||||
self.all_parties.remove(self.author_id)
|
||||
except:
|
||||
pass
|
||||
except: pass
|
||||
|
||||
|
||||
def __repr__(self) -> str:
|
||||
|
||||
@@ -85,3 +85,5 @@ class TalentTweetQueue:
|
||||
if self.good:
|
||||
print('Ended in good state, deleting backup queue...')
|
||||
os.remove(self.queue_backup_path)
|
||||
else:
|
||||
print('Ended in bad state, keeping backup queue.')
|
||||
+19
-15
@@ -174,43 +174,45 @@ class TwAPI:
|
||||
async def post_ttweet(self, ttweet: tt.TalentTweet, is_catchup=False):
|
||||
print(f'------{ttweet.tweet_id} ({util.get_username_local(ttweet.author_id)})------')
|
||||
|
||||
REPLY = '{0} replied to {1}!\n'
|
||||
QUOTE_TWEET = '{0} quote tweeted {1}!\n'
|
||||
TWEET = '{0} tweeted!\n'
|
||||
RETWEET = '{0} retweeted {1}!\n'
|
||||
REPLY = '{0} {1}replied to {2}!\n'
|
||||
QUOTE_TWEET = '{0} {1}quote tweeted {2}!\n'
|
||||
TWEET = '{0} {1}tweeted!\n'
|
||||
RETWEET = '{0} {1}retweeted {2}!\n'
|
||||
|
||||
def create_text():
|
||||
author_username = f'@/{util.get_username_local(ttweet.author_id)}'
|
||||
mention_ids = set()
|
||||
ret = str()
|
||||
just = ''
|
||||
if is_catchup:
|
||||
# ret += '[catch-up tweet]\n'
|
||||
ret += f'{ttweet.get_datetime_str()}\n'
|
||||
pass
|
||||
else:
|
||||
just = 'just '
|
||||
|
||||
# Tweet types
|
||||
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()
|
||||
elif ttweet.reply_to is not None: # reply (w/ qrt; push it into mentions)
|
||||
reply_username = f'@/{util.get_username_local(ttweet.reply_to)}'
|
||||
ret += REPLY.format(author_username, reply_username)
|
||||
reply_username = f'@/{util.get_username(ttweet.reply_to)}'
|
||||
ret += REPLY.format(author_username, just, reply_username)
|
||||
|
||||
mention_ids = set(ttweet.mentions)
|
||||
mention_ids.add(ttweet.quote_retweeted)
|
||||
try: mention_ids.remove(None)
|
||||
except: pass
|
||||
elif ttweet.quote_retweeted is not None: # standalone qrt
|
||||
quoted_username = f'@/{util.get_username_local(ttweet.quote_retweeted)}'
|
||||
ret += QUOTE_TWEET.format(author_username, quoted_username)
|
||||
quoted_username = f'@/{util.get_username(ttweet.quote_retweeted)}'
|
||||
ret += QUOTE_TWEET.format(author_username, just, quoted_username)
|
||||
elif len(ttweet.mentions) > 0: # standalone tweet w/ mentions
|
||||
ret += TWEET.format(author_username)
|
||||
ret += TWEET.format(author_username, just)
|
||||
else:
|
||||
raise ValueError(f'TalentTweet {ttweet.tweet_id} has insufficient other parties')
|
||||
|
||||
# mention line
|
||||
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 += (
|
||||
'mentioning '
|
||||
f'{" ".join(mention_usernames)}\n'
|
||||
@@ -222,16 +224,18 @@ class TwAPI:
|
||||
try:
|
||||
# print('creating reply img')
|
||||
# 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)
|
||||
print('done')
|
||||
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)]
|
||||
# if ttweet.reply_to is not None:
|
||||
# re_ttweet = tt.TalentTweet(tweet_id=ttweet.reply_to, author_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,)
|
||||
print('done')
|
||||
print('successfully posted ttweet!')
|
||||
return True
|
||||
except tweepy.Forbidden as e:
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ async def create_ttweet_image(ttweet):
|
||||
filename = f'{get_project_dir()}/img.png'
|
||||
url = ttweet_to_url(ttweet)
|
||||
img = None
|
||||
|
||||
print(url)
|
||||
try: os.remove(filename)
|
||||
except: pass
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user