fix(main.py): 修复少于 4 张图片时同步文案有误的问题
修复少于 4 张图片时同步文案有误的问题 Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
parent
f60c1f03a5
commit
7b1b455991
58
main.py
58
main.py
@ -149,46 +149,30 @@ unsynced_titles = set([p.title for p in unsynced_photos])
|
|||||||
for title in unsynced_titles:
|
for title in unsynced_titles:
|
||||||
# get unsynced photos by title
|
# get unsynced photos by title
|
||||||
photos = Photo.select().where(Photo.title == title, Photo.synced == False).order_by(Photo.created_at.asc())
|
photos = Photo.select().where(Photo.title == title, Photo.synced == False).order_by(Photo.created_at.asc())
|
||||||
if len(photos) > 1:
|
batches = [photos[i:i+4] for i in range(0, len(photos), 4)]
|
||||||
batches = [photos[i:i+4] for i in range(0, len(photos), 4)]
|
for i, batch in enumerate(batches):
|
||||||
for i, batch in enumerate(batches):
|
idx = i + 1
|
||||||
idx = i + 1
|
if len(photos) > 4:
|
||||||
status = f'{title} ({idx}/{len(batches)})'
|
status = f'{title} ({idx}/{len(batches)})'
|
||||||
media_ids = []
|
else:
|
||||||
for p in batch:
|
status = title
|
||||||
# upload photo to mastodon
|
|
||||||
photo_url = get_flickr_photo_url(p)
|
|
||||||
short_url = get_flickr_photo_page_url(p, short=True)
|
|
||||||
time.sleep(3)
|
|
||||||
img_id = upload_photo_to_mastodon(photo_url)
|
|
||||||
if img_id:
|
|
||||||
media_ids.append(img_id)
|
|
||||||
status += f'\n{short_url}'
|
|
||||||
else:
|
|
||||||
logger.error('Error: {}'.format(p.title))
|
|
||||||
# post to mastodon
|
|
||||||
mastodon_client.status_post(status=status, media_ids=media_ids)
|
|
||||||
logger.info('Post: {}'.format(status))
|
|
||||||
# update synced
|
|
||||||
for p in batch:
|
|
||||||
p.synced = True
|
|
||||||
p.synced_at = datetime.datetime.now()
|
|
||||||
p.save()
|
|
||||||
else:
|
|
||||||
p = photos[0]
|
|
||||||
status = title
|
|
||||||
media_ids = []
|
media_ids = []
|
||||||
img_id = upload_photo_to_mastodon(p)
|
for p in batch:
|
||||||
if img_id:
|
# upload photo to mastodon
|
||||||
media_ids.append(img_id)
|
photo_url = get_flickr_photo_url(p)
|
||||||
short_url = get_flickr_photo_page_url(p, short=True)
|
short_url = get_flickr_photo_page_url(p, short=True)
|
||||||
status += f'\n{short_url}'
|
time.sleep(3)
|
||||||
# post to mastodon
|
img_id = upload_photo_to_mastodon(photo_url)
|
||||||
mastodon_client.status_post(status=status, media_ids=media_ids)
|
if img_id:
|
||||||
logger.info('Post: {}'.format(status))
|
media_ids.append(img_id)
|
||||||
# update synced
|
status += f'\n{short_url}'
|
||||||
|
else:
|
||||||
|
logger.error('Error: {}'.format(p.title))
|
||||||
|
# post to mastodon
|
||||||
|
mastodon_client.status_post(status=status, media_ids=media_ids)
|
||||||
|
logger.info('Post: {}'.format(status))
|
||||||
|
# update synced
|
||||||
|
for p in batch:
|
||||||
p.synced = True
|
p.synced = True
|
||||||
p.synced_at = datetime.datetime.now()
|
p.synced_at = datetime.datetime.now()
|
||||||
p.save()
|
p.save()
|
||||||
else:
|
|
||||||
logger.error('Error: {}'.format(p.title))
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user