From 8245e5af295a04c415cb441c89654d0ba634b342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Thu, 14 Jun 2018 12:48:49 +0200 Subject: [PATCH] Fix synchronization of Twitch comments --- comments-api/app.py | 2 +- comments-api/sync.py | 37 ++++++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/comments-api/app.py b/comments-api/app.py index 4d6f209..a20846d 100644 --- a/comments-api/app.py +++ b/comments-api/app.py @@ -128,7 +128,7 @@ class CommentResource(flask_restful.Resource): if not video: flask_restful.abort(404, message='Video {0} does not exist'.format(video_id)) q = db.session.query(Association).join(Comment).filter( - Association.video_id == video_id, Comment.id == comment_id) + Association.video_id == video_id, Association.comment_id == comment_id) assoc = q.first() if not assoc: flask_restful.abort(404, diff --git a/comments-api/sync.py b/comments-api/sync.py index 6d74365..2501ab8 100644 --- a/comments-api/sync.py +++ b/comments-api/sync.py @@ -74,24 +74,31 @@ class Sync(object): if not video: continue id = cls._get(com, '_id') - q = db.session.query(Association).filter(Video.id == video.id, Comment.id == id) - assoc = q.first() - if not assoc: - assoc = Association(comment=Comment(id=id)) - assoc.offset=cls._get(com, 'content_offset_seconds') - assoc.comment.commenter_id = cls._get(com, 'commenter', '_id') - assoc.comment.commenter_name = cls._get(com, 'commenter', 'name') - assoc.comment.commenter_display_name = cls._get(com, 'commenter', 'display_name') - assoc.comment.commenter_logo = cls._get(com, 'commenter', 'logo') - assoc.comment.source = cls._get(com, 'source') - assoc.comment.message_body = cls._get(com, 'message', 'body') - assoc.comment.message_user_color = cls._get(com, 'message', 'user_color') + q = db.session.query(Comment).filter(Comment.id == id) + comment = q.first() + if not comment: + comment = Comment(id=id) + comment.commenter_id = cls._get(com, 'commenter', '_id') + comment.commenter_name = cls._get(com, 'commenter', 'name') + comment.commenter_display_name = cls._get(com, 'commenter', 'display_name') + comment.commenter_logo = cls._get(com, 'commenter', 'logo') + comment.source = cls._get(com, 'source') + comment.message_body = cls._get(com, 'message', 'body') + comment.message_user_color = cls._get(com, 'message', 'user_color') badges = cls._get(com, 'message', 'user_badges') if badges: badges = ','.join(['{_id}:{version}'.format(**b) for b in badges]) - assoc.comment.message_user_badges = badges - assoc.comment.created_at = cls._to_datetime(cls._get(com, 'created_at')) - assoc.comment.updated_at = cls._to_datetime(cls._get(com, 'updated_at')) + comment.message_user_badges = badges + comment.created_at = cls._to_datetime(cls._get(com, 'created_at')) + comment.updated_at = cls._to_datetime(cls._get(com, 'updated_at')) + q = db.session.query(Association).filter( + Association.video_id == video.id, + Association.comment_id == comment.id) + assoc = q.first() + if not assoc: + assoc = Association() + assoc.comment = comment + assoc.offset = cls._get(com, 'content_offset_seconds') video.associations.append(assoc) db.session.add(video) db.session.commit()