<commit_before>try:
    import simplejson as json
except ImportError:
    import json

from flask import Flask, Response, render_template, request
from werkzeug.contrib.cache import SimpleCache
import requests


app = Flask('roundup')
app.config.from_object('settings')
# TODO: In a production environment, you would never use an in-memory cache like
# this.  You should instead use something like Redis or Memcached.
cache = SimpleCache()


def get_top_posts(*args, **kwargs):
    kwargs['time'] = kwargs.get('time') or '6h'

    url = '{}/realtime/posts'.format(app.config['PARSELY']['api_base_url'])
    posts = []

    for apikey, secret in app.config['PARSELY']['apikeys']:
        kwargs['apikey'] = apikey
        kwargs['secret'] = secret
        apikey_posts = requests.get(url, params=kwargs).json()['data']
        for post in apikey_posts:
            post['apikey'] = apikey

        posts.extend(apikey_posts)

    posts = sorted(posts, key=lambda x: x['_hits'], reverse=True)
    return posts[:kwargs['limit']]


@app.route('/top_posts')
def top_posts():
    limit = request.args.get('limit') or app.config['NUMBER_OF_POSTS']
    if isinstance(limit, basestring):
        limit = int(limit)
    posts = cache.get('top_posts')
    if posts is None:
        posts = get_top_posts(limit=limit)
        cache.set('top_posts', posts, timeout=app.config['CACHE_EXPIRY'])

    return Response(json.dumps(posts), content_type='application/json')


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


if __name__ == '__main__':
    app.run()<commit_msg>Better handling of limit param in top posts<commit_after>try:
    import simplejson as json
except ImportError:
    import json

from flask import Flask, Response, render_template, request
from werkzeug.contrib.cache import SimpleCache
import requests


app = Flask('roundup')
app.config.from_object('settings')
# TODO: In a production environment, you would never use an in-memory cache like
# this.  You should instead use something like Redis or Memcached.
cache = SimpleCache()


def get_top_posts(*args, **kwargs):
    kwargs['time'] = kwargs.get('time') or '6h'

    url = '{}/realtime/posts'.format(app.config['PARSELY']['api_base_url'])
    posts = []

    for apikey, secret in app.config['PARSELY']['apikeys']:
        kwargs['apikey'] = apikey
        kwargs['secret'] = secret
        apikey_posts = requests.get(url, params=kwargs).json()['data']
        for post in apikey_posts:
            post['apikey'] = apikey

        posts.extend(apikey_posts)

    posts = sorted(posts, key=lambda x: x['_hits'], reverse=True)
    return posts[:kwargs['limit']]


@app.route('/top_posts')
def top_posts():
    limit = request.args.get('limit', default=app.config['NUMBER_OF_POSTS'],
                             type=int)
    if limit <= 0:
        limit = app.config['NUMBER_OF_POSTS']

    posts = cache.get('top_posts')
    if posts is None:
        posts = get_top_posts(limit=limit)
        cache.set('top_posts', posts, timeout=app.config['CACHE_EXPIRY'])

    return Response(json.dumps(posts), content_type='application/json')


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


if __name__ == '__main__':
    app.run()
{'fraction_non_alphanumeric': 0.1381011097410604, 'fraction_numerical': 0.0015413070283600493, 'mean_word_length': 3.7303206997084546, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5350027', 'n_tokens_mistral': 1028, 'n_tokens_neox': 1002, 'n_words': 250}