From 0a2d0fe5913cc18c73d6770976170a90a19812e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Fri, 27 Apr 2018 12:14:45 +0200 Subject: [PATCH] Sync on bot responses instead of syncing on "!quote add" commands --- api/sync_with_twitch.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/api/sync_with_twitch.py b/api/sync_with_twitch.py index 6148449..ae08de3 100644 --- a/api/sync_with_twitch.py +++ b/api/sync_with_twitch.py @@ -10,6 +10,14 @@ from db import db, Quote TWITCH_API_URL = 'https://api.twitch.tv/v5' +QUOTE = re.compile(r'''^ + (?P.+)\s+-->\s+ + Sweet!\s+Thanks\s+for\s+the\s+quote!\s+ + \#(?P\d+):\s+ + "(?P.+)"\s+ + \[(?P.+)\]\s+ + \[(?P.+)\]$''', re.VERBOSE) + app = flask.Flask(__name__) app.config.from_envvar('SETTINGS') @@ -36,7 +44,6 @@ def get_videos(user_id, client_id): result.append(dict( id=int(vid['_id'].lstrip('v')), title=vid['title'], - game=vid['game'], date=dateutil.parser.parse(vid['recorded_at']).date())) return result @@ -62,12 +69,9 @@ def get_comments(video_id, client_id): def main(): videos = get_videos(app.config['TWITCH_USER_ID'], app.config['TWITCH_CLIENT_ID']) - last_id = 0 last = db.session.query(Quote).order_by(Quote.date.desc()).first() if last: videos = [v for v in videos if v['date'] >= last.date] - last_id = last.id - QUOTE = re.compile(r'^\!quote\s+add\s+(?P")?(?P.+)(?(q)")$') quotes = [] for video in videos: app.logger.info('Processing video %d (%s)', video['id'], video['title']) @@ -75,18 +79,16 @@ def main(): for comment in comments: m = QUOTE.match(comment) if m: - quotes.append(dict( - text=m.group('text'), - video_id=video['id'], - game=video['game'], - date=video['date'])) - id = last_id + 1 - for quote in sorted(quotes, key=lambda q: q['date']): + quote = m.groupdict() + quote['id'] = int(quote['id']) + 10000 + quote['date'] = dateutil.parser.parse(quote['date'], dayfirst=True) + quote['video_id'] = video['id'] + quotes.append(quote) + for quote in quotes: now = sqlalchemy.func.now() - q = db.session.query(Quote).filter(Quote.text == quote['text']).first() + q = db.session.query(Quote).filter(Quote.id == quote['id']).first() if not q: - q = Quote(id=id, created_at=now) - id += 1 + q = Quote(id=quote['id'], created_at=now) q.video_id = quote['video_id'] q.date = quote['date'] q.game = quote['game']