From 7434dc787bff958c83a4c0a223a8ce44d87213ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= Date: Sun, 29 Apr 2018 18:42:24 +0200 Subject: [PATCH] Remove no longer useful sync_with_twitch.py script --- api/sync_with_twitch.py | 101 ---------------------------------------- 1 file changed, 101 deletions(-) delete mode 100644 api/sync_with_twitch.py diff --git a/api/sync_with_twitch.py b/api/sync_with_twitch.py deleted file mode 100644 index 0b416a7..0000000 --- a/api/sync_with_twitch.py +++ /dev/null @@ -1,101 +0,0 @@ -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.+)\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') -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) - 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.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()