You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.0 KiB

import re
import dateutil.parser
import flask
import requests
import sqlalchemy
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')
app.config['DEBUG'] = True
db.init_app(app)
app.app_context().push()
def get_videos(user_id, client_id):
def request(offset, limit):
url = '{0}/channels/{1}/videos'.format(TWITCH_API_URL, user_id)
params = dict(client_id=client_id, offset=offset, limit=limit)
r = requests.get(url, params=params)
r.raise_for_status()
return r.json()
result = []
data = request(0, 1)
total = data.get('_total', 0)
limit = 100
for offset in range(0, total, limit):
data = request(offset, limit)
for vid in data.get('videos', []):
result.append(dict(
id=int(vid['_id'].lstrip('v')),
title=vid['title'],
date=dateutil.parser.parse(vid['recorded_at']).date()))
return result
def get_comments(video_id, client_id):
def request(cursor):
url = '{0}/videos/{1}/comments'.format(TWITCH_API_URL, video_id)
params = dict(client_id=client_id, cursor=cursor)
r = requests.get(url, params=params)
r.raise_for_status()
return r.json()
result = []
cursor = ''
while True:
data = request(cursor)
for comment in data.get('comments', []):
result.append(comment['message']['body'])
cursor = data.get('_next')
if not cursor:
break
return result
def main():
videos = get_videos(app.config['TWITCH_USER_ID'], app.config['TWITCH_CLIENT_ID'])
last = db.session.query(Quote).order_by(Quote.date.desc()).first()
if last:
videos = [v for v in videos if v['date'] >= last.date]
quotes = []
for video in videos:
app.logger.info('Processing video %d (%s)', video['id'], video['title'])
comments = get_comments(video['id'], app.config['TWITCH_CLIENT_ID'])
for comment in comments:
m = QUOTE.match(comment)
if m:
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.id == quote['id']).first()
if not q:
q = Quote(id=quote['id'], created_at=now)
q.video_id = quote['video_id']
q.date = quote['date']
q.game = quote['game']
q.text = quote['text']
q.updated_at = now
app.logger.info('Adding quote %d (%s)', q.id, q.text)
db.session.add(q)
db.session.commit()
if __name__ == '__main__':
main()