From 6eb7e11a1ab2b8270473d12e529b2fc5add2f046 Mon Sep 17 00:00:00 2001 From: Ching Date: Sun, 11 Feb 2024 14:48:24 +0800 Subject: [PATCH] fix(syncing): Update Photo model and syncing logic --- .vscode/settings.json | 5 +++++ main.py | 10 +++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..2fcd7f7 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "conventionalCommits.scopes": [ + "syncing" + ] +} diff --git a/main.py b/main.py index 32edebe..44c162f 100644 --- a/main.py +++ b/main.py @@ -30,7 +30,8 @@ class Photo(peewee.Model): ispublic = peewee.IntegerField() isfriend = 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) synced_at = peewee.DateTimeField(null=True) @@ -118,6 +119,7 @@ for photo in user_photos['photos']['photo']: ispublic=photo['ispublic'], isfriend=photo['isfriend'], isfamily=photo['isfamily'], + status='unsynced', ) logger.info('New photo: {}'.format(photo['title'])) @@ -143,9 +145,10 @@ def upload_photo_to_mastodon(photo_url): # 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 -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: # get unsynced photos by title 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}' else: logger.error('Error: {}'.format(p.title)) + Photo.update(status='unsynced').where(Photo.id == p.id).execute() # post to mastodon mastodon_client.status_post(status=status, media_ids=media_ids) logger.info('Post: {}'.format(status))