fix(syncing): Update Photo model and syncing logic

This commit is contained in:
Ching 2024-02-11 14:48:24 +08:00
parent 7b1b455991
commit 6eb7e11a1a
2 changed files with 12 additions and 3 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"conventionalCommits.scopes": [
"syncing"
]
}

10
main.py
View File

@ -30,7 +30,8 @@ class Photo(peewee.Model):
ispublic = peewee.IntegerField() ispublic = peewee.IntegerField()
isfriend = peewee.IntegerField() isfriend = peewee.IntegerField()
isfamily = peewee.IntegerField() isfamily = peewee.IntegerField()
synced = peewee.BooleanField(default=False) # synced = peewee.BooleanField(default=False)
status = peewee.CharField(null=True) # unsynced, syncing, synced
created_at = peewee.DateTimeField(default=datetime.datetime.now) created_at = peewee.DateTimeField(default=datetime.datetime.now)
synced_at = peewee.DateTimeField(null=True) synced_at = peewee.DateTimeField(null=True)
@ -118,6 +119,7 @@ for photo in user_photos['photos']['photo']:
ispublic=photo['ispublic'], ispublic=photo['ispublic'],
isfriend=photo['isfriend'], isfriend=photo['isfriend'],
isfamily=photo['isfamily'], isfamily=photo['isfamily'],
status='unsynced',
) )
logger.info('New photo: {}'.format(photo['title'])) logger.info('New photo: {}'.format(photo['title']))
@ -143,9 +145,10 @@ def upload_photo_to_mastodon(photo_url):
# get all un-synced photos # get all un-synced photos
unsynced_photos = Photo.select().where(Photo.synced == False).order_by(Photo.created_at.asc()) Photo.update(status='syncing').where(Photo.status == 'unsynced').execute()
syncing_photos = Photo.select().where(Photo.status == 'syncing').order_by(Photo.created_at.asc())
# get titles # get titles
unsynced_titles = set([p.title for p in unsynced_photos]) unsynced_titles = set([p.title for p in syncing_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())
@ -168,6 +171,7 @@ for title in unsynced_titles:
status += f'\n{short_url}' status += f'\n{short_url}'
else: else:
logger.error('Error: {}'.format(p.title)) logger.error('Error: {}'.format(p.title))
Photo.update(status='unsynced').where(Photo.id == p.id).execute()
# post to mastodon # post to mastodon
mastodon_client.status_post(status=status, media_ids=media_ids) mastodon_client.status_post(status=status, media_ids=media_ids)
logger.info('Post: {}'.format(status)) logger.info('Post: {}'.format(status))