added patchwork code for backing up queue
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
|
||||
import traceback
|
||||
import datetime
|
||||
import asyncio
|
||||
import shutil
|
||||
|
||||
import twint
|
||||
|
||||
@@ -159,6 +161,8 @@ async def process_queue(ttweets_dict: dict) -> int:
|
||||
ttweet = ttweets_dict[key]
|
||||
if await TwAPI.instance.post_ttweet(ttweet, is_catchup=True):
|
||||
ttweets_posted += 1
|
||||
print('resting for 60s...')
|
||||
await asyncio.sleep(60)
|
||||
ttweets_dict.pop(key)
|
||||
# TODO: add ttweet.tweet_id to some success list
|
||||
except:
|
||||
@@ -184,14 +188,28 @@ async def run(program_args):
|
||||
global PROGRAM_ARGS
|
||||
global safe_to_post_tweets
|
||||
PROGRAM_ARGS = program_args
|
||||
|
||||
# in case we we experience failure and we're left with blank queue.txt
|
||||
# TODO: create TweetQueue class to organize file IO better; move all backup operations to there
|
||||
queue_backup = os.path.join(util.get_project_dir(), 'queue_in_progress.txt')
|
||||
queue_path = get_queue_path()
|
||||
|
||||
if os.path.exists(queue_backup):
|
||||
shutil.copyfile(queue_backup, queue_path)
|
||||
else:
|
||||
shutil.copyfile(queue_path, queue_backup)
|
||||
|
||||
ret = None
|
||||
|
||||
while True:
|
||||
ttweets_dict = await get_cross_talent_tweets(queue_path)
|
||||
print(f'found {len(ttweets_dict)} cross-company tweets')
|
||||
if safe_to_post_tweets:
|
||||
if await process_queue(ttweets_dict) == 0:
|
||||
print('Posted no new tweets; we\'re caught up!')
|
||||
os.remove(queue_backup) # keep updated queue
|
||||
return True
|
||||
else:
|
||||
print('Tweets were not retrieved cleanly.')
|
||||
os.remove(queue_path) # keep backup queue
|
||||
return False
|
||||
+2
-3
@@ -212,14 +212,13 @@ class TwAPI:
|
||||
ret += f'\n{util.ttweet_to_url(ttweet)}'
|
||||
return ret
|
||||
|
||||
img_media_id_task = asyncio.create_task(self.get_ttweet_image_media_id(ttweet))
|
||||
text = create_text()
|
||||
try:
|
||||
print('posting main tweet')
|
||||
twt_resp = await self.post_tweet(text)
|
||||
twt_id = twt_resp.data['id']
|
||||
print('waiting on reply img')
|
||||
media_id = await img_media_id_task
|
||||
print('creating reply img')
|
||||
media_id = await self.get_ttweet_image_media_id(ttweet)
|
||||
print('posting reply tweet')
|
||||
await self.post_tweet(reply_to_tweet=twt_id, media_id=media_id,)
|
||||
print('successfully posted ttweet!')
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 117 KiB |
Reference in New Issue
Block a user