Sync on bot responses instead of syncing on "!quote add" commands

master
Nikola Forró 7 years ago
parent 72bfeb19b5
commit 0a2d0fe591

@ -10,6 +10,14 @@ from db import db, Quote
TWITCH_API_URL = 'https://api.twitch.tv/v5'
QUOTE = re.compile(r'''^
(?P<user>.+)\s+-->\s+
Sweet!\s+Thanks\s+for\s+the\s+quote!\s+
\#(?P<id>\d+):\s+
"(?P<text>.+)"\s+
\[(?P<game>.+)\]\s+
\[(?P<date>.+)\]$''', 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<q>")?(?P<text>.+)(?(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']

Loading…
Cancel
Save