Prevent inserting duplicate comments in one transaction

master
Nikola Forró 6 years ago
parent 363a0c7cce
commit 7b6c87e262

@ -68,11 +68,19 @@ class Sync(object):
db.session.add(video) db.session.add(video)
db.session.commit() db.session.commit()
app.logger.info('Updated videos: %s', ', '.join(updated) if updated else 'NONE') app.logger.info('Updated videos: %s', ', '.join(updated) if updated else 'NONE')
comments = {}
for com in twitch.fetch_comments(updated): for com in twitch.fetch_comments(updated):
q = db.session.query(Video).filter(Video.id == cls._get(com, 'content_id')) id = cls._get(com, '_id')
video = q.first() assoc = dict(
if not video: video_id=cls._get(com, 'content_id'),
continue offset=cls._get(com, 'content_offset_seconds'))
if id in comments:
comments[id].update(com)
comments[id]['assocs'].append(assoc)
else:
comments[id] = com
comments[id]['assocs'] = [assoc]
for com in comments.values():
id = cls._get(com, '_id') id = cls._get(com, '_id')
q = db.session.query(Comment).filter(Comment.id == id) q = db.session.query(Comment).filter(Comment.id == id)
comment = q.first() comment = q.first()
@ -91,6 +99,11 @@ class Sync(object):
comment.message_user_badges = badges comment.message_user_badges = badges
comment.created_at = cls._to_datetime(cls._get(com, 'created_at')) comment.created_at = cls._to_datetime(cls._get(com, 'created_at'))
comment.updated_at = cls._to_datetime(cls._get(com, 'updated_at')) comment.updated_at = cls._to_datetime(cls._get(com, 'updated_at'))
for assc in com['assocs']:
q = db.session.query(Video).filter(Video.id == assc['video_id'])
video = q.first()
if not video:
continue
q = db.session.query(Association).filter( q = db.session.query(Association).filter(
Association.video_id == video.id, Association.video_id == video.id,
Association.comment_id == comment.id) Association.comment_id == comment.id)
@ -98,7 +111,7 @@ class Sync(object):
if not assoc: if not assoc:
assoc = Association() assoc = Association()
assoc.comment = comment assoc.comment = comment
assoc.offset = cls._get(com, 'content_offset_seconds') assoc.offset = assc['offset']
video.associations.append(assoc) video.associations.append(assoc)
db.session.add(video) db.session.add(video)
db.session.commit() db.session.commit()

Loading…
Cancel
Save