add ability to announce tweets by ID as cmd arg
- also fix a TalentTweet constructor filter
This commit is contained in:
+30
-3
@@ -14,6 +14,8 @@ from twapi import TwAPI
|
|||||||
import talenttweet as tt
|
import talenttweet as tt
|
||||||
import ttweetqueue as ttq
|
import ttweetqueue as ttq
|
||||||
|
|
||||||
|
PROGRAM_ARGS = None
|
||||||
|
|
||||||
safe_to_post_tweets = True
|
safe_to_post_tweets = True
|
||||||
errored = False
|
errored = False
|
||||||
|
|
||||||
@@ -61,7 +63,7 @@ async def get_cross_tweets_online():
|
|||||||
|
|
||||||
# return False = errored or we posted at least one ttweet
|
# return False = errored or we posted at least one ttweet
|
||||||
# return True = we didn't post a single ttweet
|
# return True = we didn't post a single ttweet
|
||||||
async def process_queue() -> bool:
|
async def process_queue(priority_tweet_ids: list[str]=None) -> bool:
|
||||||
global errored
|
global errored
|
||||||
global scraper
|
global scraper
|
||||||
global queue
|
global queue
|
||||||
@@ -119,12 +121,37 @@ async def run(PROGRAM_ARGS):
|
|||||||
queue.ttweets_dict[id] = tt.TalentTweet.create_from_tweety(t)
|
queue.ttweets_dict[id] = tt.TalentTweet.create_from_tweety(t)
|
||||||
queue.save_file()
|
queue.save_file()
|
||||||
|
|
||||||
|
# post tweets given in command line first
|
||||||
|
if PROGRAM_ARGS.post_id is not None and len(PROGRAM_ARGS.post_id) > 0:
|
||||||
|
PROGRAM_ARGS.post_id.sort()
|
||||||
|
print('Posting specified tweets first.')
|
||||||
|
for id in PROGRAM_ARGS.post_id:
|
||||||
|
try:
|
||||||
|
i = int(id)
|
||||||
|
except ValueError:
|
||||||
|
print(f'Invalid tweet {id}!')
|
||||||
|
continue
|
||||||
|
|
||||||
|
if i not in queue.finished_ttweets:
|
||||||
|
posted = await TwAPI.instance.post_ttweet_by_id(i)
|
||||||
|
if posted:
|
||||||
|
queue.add_finished_tweet(i)
|
||||||
|
print('Successfully posted tweet. Sleeping for 5 minutes')
|
||||||
|
await asyncio.sleep(60*5)
|
||||||
|
else:
|
||||||
|
print('Did not post tweet')
|
||||||
|
else:
|
||||||
|
print('Tweet was already finished')
|
||||||
|
|
||||||
|
print('Done processing specified tweets')
|
||||||
|
PROGRAM_ARGS.post_id = None
|
||||||
|
|
||||||
async def queue_loop():
|
async def queue_loop():
|
||||||
while True:
|
while True:
|
||||||
print(f'{queue.get_count()} cross-company tweets to attempt sharing.')
|
print(f'{queue.get_count()} cross-company tweets to announce.')
|
||||||
try:
|
try:
|
||||||
if safe_to_post_tweets:
|
if safe_to_post_tweets:
|
||||||
if await process_queue():
|
if await process_queue(PROGRAM_ARGS.post_id):
|
||||||
print('Posted no new tweets; we\'re caught up!')
|
print('Posted no new tweets; we\'re caught up!')
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ def init_argparse():
|
|||||||
p.add_argument('--no-listen', action='store_true', help='Run one scraping-posting cycle without waiting to run again.')
|
p.add_argument('--no-listen', action='store_true', help='Run one scraping-posting cycle without waiting to run again.')
|
||||||
p.add_argument('--refresh-queue', action='store_true', help='Refresh the details on each tweet currently in queue.')
|
p.add_argument('--refresh-queue', action='store_true', help='Refresh the details on each tweet currently in queue.')
|
||||||
p.add_argument('--straight-to-queue', action='store_true', help='Go through queue first before attempting to pull tweets.')
|
p.add_argument('--straight-to-queue', action='store_true', help='Go through queue first before attempting to pull tweets.')
|
||||||
|
p.add_argument('--post-id', action='append', help='ID of a tweet to try and post right away. Specify multiple to post multiple tweets in a row.')
|
||||||
return p
|
return p
|
||||||
|
|
||||||
def command_line():
|
def command_line():
|
||||||
|
|||||||
+4
-3
@@ -111,6 +111,7 @@ class TalentTweet:
|
|||||||
# filter users to only be talents
|
# filter users to only be talents
|
||||||
self.mentions = {x for x in mrq[0] if x in tl.talents}
|
self.mentions = {x for x in mrq[0] if x in tl.talents}
|
||||||
self.rt_mentions = {x for x in rt_mentions if x in tl.talents}
|
self.rt_mentions = {x for x in rt_mentions if x in tl.talents}
|
||||||
|
self.mentions.difference_update(self.rt_mentions)
|
||||||
|
|
||||||
self.reply_to = mrq[1]
|
self.reply_to = mrq[1]
|
||||||
self.quote_tweeted = mrq[2]
|
self.quote_tweeted = mrq[2]
|
||||||
@@ -171,7 +172,7 @@ class TalentTweet:
|
|||||||
|
|
||||||
def get_datetime_str(self):
|
def get_datetime_str(self):
|
||||||
unpad = '#' if platform.system() == 'Windows' else '-'
|
unpad = '#' if platform.system() == 'Windows' else '-'
|
||||||
return self.date_time.strftime(f'%{unpad}I:%M%p (%Z) · %b %{unpad}d, %Y')
|
return self.date_time.strftime(f'%b %{unpad}d, %Y · %{unpad}I:%M%p (%Z)')
|
||||||
|
|
||||||
def announce_text(self):
|
def announce_text(self):
|
||||||
# templates
|
# templates
|
||||||
@@ -207,6 +208,7 @@ class TalentTweet:
|
|||||||
rtm_msg(RETWEET_MENTIONS_B, rt_username)
|
rtm_msg(RETWEET_MENTIONS_B, rt_username)
|
||||||
else:
|
else:
|
||||||
ret += RETWEET.format(author_username, rt_username)
|
ret += RETWEET.format(author_username, rt_username)
|
||||||
|
print_mention_ids.clear()
|
||||||
elif self.reply_to is not None: # reply
|
elif self.reply_to is not None: # reply
|
||||||
reply_username = f'@/{util.get_username_with_company(self.reply_to)}' if self.reply_to != -1 else None
|
reply_username = f'@/{util.get_username_with_company(self.reply_to)}' if self.reply_to != -1 else None
|
||||||
if len(self.rt_mentions) > 0:
|
if len(self.rt_mentions) > 0:
|
||||||
@@ -221,8 +223,6 @@ class TalentTweet:
|
|||||||
ret += QUOTE_TWEET.format(author_username, quoted_username)
|
ret += QUOTE_TWEET.format(author_username, quoted_username)
|
||||||
elif len(self.mentions) > 0: # standalone tweet
|
elif len(self.mentions) > 0: # standalone tweet
|
||||||
ret += TWEET.format(author_username, ", ".join(mention_usernames))
|
ret += TWEET.format(author_username, ", ".join(mention_usernames))
|
||||||
f'[{self.get_datetime_str()}]\n'
|
|
||||||
return ret
|
|
||||||
else:
|
else:
|
||||||
raise ValueError(f'TalentTweet {self.tweet_id} has insufficient other parties')
|
raise ValueError(f'TalentTweet {self.tweet_id} has insufficient other parties')
|
||||||
|
|
||||||
@@ -233,5 +233,6 @@ class TalentTweet:
|
|||||||
f'{", ".join(mention_usernames)}'
|
f'{", ".join(mention_usernames)}'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# date
|
||||||
ret += f'\n\n{self.get_datetime_str()}'
|
ret += f'\n\n{self.get_datetime_str()}'
|
||||||
return ret
|
return ret
|
||||||
|
|||||||
@@ -147,3 +147,21 @@ class TwAPI:
|
|||||||
else:
|
else:
|
||||||
raise e
|
raise e
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
async def post_ttweet_by_id(self, id: int):
|
||||||
|
from scraper import Scraper
|
||||||
|
|
||||||
|
print(f'Manually posting tweet {id}')
|
||||||
|
s = Scraper()
|
||||||
|
t = s.get_tweet(id, True)
|
||||||
|
if not t:
|
||||||
|
print('Tweet could not be retrieved')
|
||||||
|
return False
|
||||||
|
|
||||||
|
ttweet = tt.TalentTweet.create_from_tweety(t)
|
||||||
|
if not ttweet.is_cross_company():
|
||||||
|
print(f'{ttweet.username}/{ttweet.tweet_id} is not cross-company!')
|
||||||
|
return False
|
||||||
|
|
||||||
|
print(f'Posting {ttweet.username}/{ttweet.tweet_id}...')
|
||||||
|
return await self.post_ttweet(ttweet)
|
||||||
Reference in New Issue
Block a user