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' 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 = flask.Flask(__name__)
app.config.from_envvar('SETTINGS') app.config.from_envvar('SETTINGS')
@ -36,7 +44,6 @@ def get_videos(user_id, client_id):
result.append(dict( result.append(dict(
id=int(vid['_id'].lstrip('v')), id=int(vid['_id'].lstrip('v')),
title=vid['title'], title=vid['title'],
game=vid['game'],
date=dateutil.parser.parse(vid['recorded_at']).date())) date=dateutil.parser.parse(vid['recorded_at']).date()))
return result return result
@ -62,12 +69,9 @@ def get_comments(video_id, client_id):
def main(): def main():
videos = get_videos(app.config['TWITCH_USER_ID'], app.config['TWITCH_CLIENT_ID']) 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() last = db.session.query(Quote).order_by(Quote.date.desc()).first()
if last: if last:
videos = [v for v in videos if v['date'] >= last.date] 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 = [] quotes = []
for video in videos: for video in videos:
app.logger.info('Processing video %d (%s)', video['id'], video['title']) app.logger.info('Processing video %d (%s)', video['id'], video['title'])
@ -75,18 +79,16 @@ def main():
for comment in comments: for comment in comments:
m = QUOTE.match(comment) m = QUOTE.match(comment)
if m: if m:
quotes.append(dict( quote = m.groupdict()
text=m.group('text'), quote['id'] = int(quote['id']) + 10000
video_id=video['id'], quote['date'] = dateutil.parser.parse(quote['date'], dayfirst=True)
game=video['game'], quote['video_id'] = video['id']
date=video['date'])) quotes.append(quote)
id = last_id + 1 for quote in quotes:
for quote in sorted(quotes, key=lambda q: q['date']):
now = sqlalchemy.func.now() 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: if not q:
q = Quote(id=id, created_at=now) q = Quote(id=quote['id'], created_at=now)
id += 1
q.video_id = quote['video_id'] q.video_id = quote['video_id']
q.date = quote['date'] q.date = quote['date']
q.game = quote['game'] q.game = quote['game']

Loading…
Cancel
Save