<repo_name>Granitosaurus/matchtick.com<file_sep>/ggmtgg/__init__.py
from flask import Flask
from flask_redis import FlaskRedis
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

app = Flask(__name__)
app.config.from_pyfile('config.py')
redis = FlaskRedis(app, decode_responses=True)
limiter = Limiter(app, key_func=get_remote_address)



import ggmtgg.views
import ggmtgg.updater
<file_sep>/ggmtgg/updater.py
import json

from ggmt.matchticker import GosuTicker
from ggmt.tournament import LiquidBracketDownloader, EVENT_CURRENT, EVENT_FUTURE, EVENT_PAST
from redis import Redis

from ggmtgg import app


@app.cli.command('update-matchticker')
def update_matchticker():
    r = Redis()
    for game in GosuTicker.games:
        print(f'updating matchticker {game}')
        g = GosuTicker(game)
        value = list(g.download_matches())
        r.delete(f'ggmt_tick_{game}')
        r.set(f'ggmt_tick_{game}', json.dumps(value))


@app.cli.command('update-tournaments')
def update_tournaments():
    r = Redis()
    for cat in [EVENT_FUTURE, EVENT_CURRENT, EVENT_PAST]:
        for game in LiquidBracketDownloader.games:
            print(f'updating tournament {game}_{cat.lower()}')
            dl = LiquidBracketDownloader(game)
            value = dl.find_tournaments(cat)
            r.delete(f'ggmt_tournament_{game}_{cat.lower()}')
            r.set(f'ggmt_tournament_{game}_{cat.lower()}', json.dumps(value))


if __name__ == '__main__':
    update_tournaments()
    update_matchticker()
<file_sep>/ggmtgg/views.py
import json

from flask import render_template, request, redirect, url_for, g, session, abort
from jinja2 import TemplateNotFound

from ggmtgg import app, redis, config, limiter
from ggmt.tournament import LiquidBracketDownloader
from ggmt.matchticker import GosuTicker


def error(reason, suggestion=None):
    data = {
        'error': reason,
        'suggestion': suggestion or ''
    }
    return json.dumps(data)


api_limit = limiter.shared_limit("100/hour", "api", error_message=error('Exceeded 100/hour api call limit'))


@app.before_first_request
def make_session_permanent():
    session.permanent = True


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/tick/<string:game>')
def tick(game):
    data = redis.get(f'ggmt_tick_{game}')
    data = json.loads(data)
    return render_template('tick.html', data=data)


@app.route('/api/tick/<string:game>')
@api_limit
def api_tick(game):
    if game not in GosuTicker.games:
        return error(f'unknown_game;choose from: {GosuTicker.games}')
    return redis.get(f'ggmt_tick_{game}')


@app.route('/api/tournament/<string:game>/<string:time>')
@app.route('/api/tournament/<string:game>', defaults={'time': 'all'})
@app.route('/api/tournament', defaults={'time': None, 'game': None})
@api_limit
def api_tournament(game, time):
    if game not in LiquidBracketDownloader.games:
        return error(f'unknown_game;choose from: {LiquidBracketDownloader.games}',
                     suggestion='/tournament/game')
    times = config.TOURNAMENT_TIMES + ['all']
    if time not in times:
        return error(f'unknown_time;choose from: {times}')
    if time != 'all':
        return redis.get(f'ggmt_tournament_{game}_{time}')
    else:
        items = [json.loads(redis.get(f'ggmt_tournament_{game}_{i}')) for i in config.TOURNAMENT_TIMES]
        items = [tour for time in items for tour in time]
        return json.dumps(items, ensure_ascii=False)


@app.route('/<string:page_name>')
def static_page(page_name):
    try:
        return render_template('{}.html'.format(page_name))
    except TemplateNotFound:
        return abort(404)
<file_sep>/setup.py
from distutils.core import setup

setup(
    name='ggmt.gg',
    version='0.1',
    packages=['ggmtgg'],
    url='',
    license='GPLv3',
    author='granitosaurus',
    author_email='',
    description=''
)
<file_sep>/ggmtgg/config.py
# WTF_CSRF_ENABLED = True
from ggmt.matchticker import GosuTicker
from ggmt.tournament import EVENT_PAST, EVENT_FUTURE, EVENT_CURRENT

SECRET_KEY = 'very-secret'
REDIS_URL = "redis://:@localhost:6379/0"

# rate limiting
RATELIMIT_STORAGE_URL = "redis://:@localhost:6379/0"
RATELIMIT_HEADERS_ENABLED = True

# ggmt settings
GAMES = {
    'Dota 2': 'dota2',
    'CS:GO': 'counterstrike',
    'Hearthstone': 'hearthstone',
    'Heroes of the Storm': 'heroesofthestorm',
    'League of Legends': 'lol',
    'Overwatch': 'overwatch',
    'Starcraft 2': 'starcraft2',
    'All': 'all',
}
TOURNAMENT_TIMES = [t.lower() for t in [EVENT_PAST, EVENT_FUTURE, EVENT_CURRENT]]
