<commit_before>import requests
from requests.auth import HTTPBasicAuth
from flask import Flask, request, render_template, jsonify
from flask.ext.cache import Cache

import config

app = Flask(__name__)
cache = Cache(app, config=config.CACHE)


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


@app.route("/search")
def search():
    search_term = request.args.get('q')
    response = _search_for_term(search_term)
    return jsonify(response)


def _search_for_term(search_term):
    cache_key = 's-{}'.format(search_term)
    response = cache.get(cache_key)
    if not response:
        response = _make_request(search_term)
    cache.set(cache_key, response, 300)
    return response


def _make_request(search_term):
    basic_auth = HTTPBasicAuth(config.GITHUB_TOKEN, 'x-oauth-basic')
    url = ('https://api.github.com/search/repositories?q="cookiecutter%20'
           'template"+{term}+in:description&sort=stars&order=desc')

    github_response = requests.get(url.format(term=search_term),
                                   auth=basic_auth)

    if github_response.status_code == 401:
        response = {
            'error': "We're over quota. Please come back after a few minutes :)"
        }
    elif github_response.status_code == 200:
        response = _transform_response(github_response)
    else:
        response = {
            'error': 'An unknown error ocurred: {}'.format(github_response.status_code)
        }

    return response<commit_msg>Don't cache error responses<commit_after>import requests
from requests.auth import HTTPBasicAuth
from flask import Flask, request, render_template, jsonify
from flask.ext.cache import Cache

import config

app = Flask(__name__)
cache = Cache(app, config=config.CACHE)


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


@app.route("/search")
def search():
    search_term = request.args.get('q')
    response = _search_for_term(search_term)
    return jsonify(response)


def _search_for_term(search_term):
    response = _make_request(search_term)
    return _parse_response(response)


@cache.memoize(timeout=300)
def _make_request(search_term):
    basic_auth = HTTPBasicAuth(config.GITHUB_TOKEN, 'x-oauth-basic')
    url = ('https://api.github.com/search/repositories?q="cookiecutter%20'
           'template"+{term}+in:description&sort=stars&order=desc')

    return requests.get(url.format(term=search_term), auth=basic_auth)


def _parse_response(response):
    if response.status_code == 401:
        response = {
            'error': "We're over quota. Please come back after a few minutes :)"
        }
    elif response.status_code == 200:
        response = _transform_response(response)
    else:
        response = {
            'error': 'An unknown error ocurred: {}'.format(response.status_code)
        }

    return response
{'fraction_non_alphanumeric': 0.11523642732049037, 'fraction_numerical': 0.007705779334500876, 'mean_word_length': 3.6288492706645057, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '541073', 'n_tokens_mistral': 906, 'n_tokens_neox': 859, 'n_words': 185}