<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import hashlib

from heartbeat import Heartbeat
from flask import jsonify, request, abort

from downstream_node.startup import app
from downstream_node.config import config
from downstream_node.models import Challenges
from downstream_node.lib import gen_challenges
from downstream_node.lib.utils import query_to_list, load_heartbeat


@app.route('/')
def api_index():
    return jsonify(msg='ok')


@app.route('/api/downstream/challenges/<filepath>')
def api_downstream_challenge(filepath):
    """

    :param filepath:
    """
    # Make assertions about the request to make sure it's valid.

    # Commenting out while still in development, should be used in prod
    # try:
    #     assert os.path.isfile(os.path.join('/opt/files', filename))
    # except AssertionError:
    #     resp = jsonify(msg="file name is not valid")
    #     resp.status_code = 400
    #     return resp

    # Hardcode filepath to the testfile in tests while in development
    filepath = os.path.abspath(
        os.path.join(
            os.path.split(__file__)[0], '..', 'tests', 'thirty-two_meg.testfile')
    )

    root_seed = hashlib.sha256(os.urandom(32)).hexdigest()
    filename = os.path.split(filepath)[1]

    query = Challenges.query.filter(Challenges.filename == filename)

    if not query.all():
        gen_challenges(filepath, root_seed)
        query = Challenges.query.filter(Challenges.filename == filename)

    return jsonify(challenges=query_to_list(query))


@app.route('/api/downstream/challenges/answer/<filepath>', methods=['GET', 'POST'])
def api_downstream_challenge_answer(filepath):
    """

    :param filepath:
    :return:
    """
    request_json = request.get_json(force=True, silent=True)

    # Make assertions about the request to make sure it's valid.
    try:
        assert request_json
    except AssertionError:
        resp = jsonify(msg="missing request json")
        resp.status_code = 400
        return resp

    req_json_keys = ['seed', 'rootseed', 'block', 'response']

    try:
        assert sorted(req_json_keys) == sorted(request_json.keys())
    except AssertionError:
        resp = jsonify(msg="missing data")
        resp.status_code = 400
        return resp

    # Hardcode filepath to the testfile in tests while in development
    filepath = os.path.abspath(
        os.path.join(
            os.path.split(__file__)[0], '..', 'tests', 'thirty-two_meg.testfile')
    )
    filename = os.path.split(filepath)[1]

    # Commenting out while still in development, should be used in prod
    # try:
    #     assert os.path.isfile(os.path.join('/opt/files', filename))
    # except AssertionError:
    #     resp = jsonify(msg="file name is not valid")
    #     resp.status_code = 400
    #     return resp

    query = Challenges.query.filter(
        Challenges.filename == filename,
        Challenges.rootseed == request_json.get('rootseed'),
        Challenges.block == request_json.get('block'),
        Challenges.seed == request_json.get('seed'),
    )
    challenge = query.all()

    # Oh, and challenge should only be len of 1, or we gots problems
    if len(challenge) == 0:
        abort(404)
    elif len(challenge) < 1:
        abort(400)

    node_hb = Heartbeat(filepath, secret=config.SECRET_KEY)
    node_hb = load_heartbeat(node_hb, query)
    result = node_hb.check_answer(request_json.get('response'))

    if result is True:
        return jsonify(msg='ok', match=True)
    elif result is False:
        return jsonify(msg='ok', match=False)
    else:
        resp = jsonify(msg='error')<commit_msg>Remove unneeded and breaky-breaky reference to rootseed<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import hashlib

from heartbeat import Heartbeat
from flask import jsonify, request, abort

from downstream_node.startup import app
from downstream_node.config import config
from downstream_node.models import Challenges
from downstream_node.lib import gen_challenges
from downstream_node.lib.utils import query_to_list, load_heartbeat


@app.route('/')
def api_index():
    return jsonify(msg='ok')


@app.route('/api/downstream/challenges/<filepath>')
def api_downstream_challenge(filepath):
    """

    :param filepath:
    """
    # Make assertions about the request to make sure it's valid.

    # Commenting out while still in development, should be used in prod
    # try:
    #     assert os.path.isfile(os.path.join('/opt/files', filename))
    # except AssertionError:
    #     resp = jsonify(msg="file name is not valid")
    #     resp.status_code = 400
    #     return resp

    # Hardcode filepath to the testfile in tests while in development
    filepath = os.path.abspath(
        os.path.join(
            os.path.split(__file__)[0], '..', 'tests', 'thirty-two_meg.testfile')
    )

    root_seed = hashlib.sha256(os.urandom(32)).hexdigest()
    filename = os.path.split(filepath)[1]

    query = Challenges.query.filter(Challenges.filename == filename)

    if not query.all():
        gen_challenges(filepath, root_seed)
        query = Challenges.query.filter(Challenges.filename == filename)

    return jsonify(challenges=query_to_list(query))


@app.route('/api/downstream/challenges/answer/<filepath>', methods=['GET', 'POST'])
def api_downstream_challenge_answer(filepath):
    """

    :param filepath:
    :return:
    """
    request_json = request.get_json(force=True, silent=True)

    # Make assertions about the request to make sure it's valid.
    try:
        assert request_json
    except AssertionError:
        resp = jsonify(msg="missing request json")
        resp.status_code = 400
        return resp

    req_json_keys = ['seed', 'block', 'response']

    try:
        assert sorted(req_json_keys) == sorted(request_json.keys())
    except AssertionError:
        resp = jsonify(msg="missing data")
        resp.status_code = 400
        return resp

    # Hardcode filepath to the testfile in tests while in development
    filepath = os.path.abspath(
        os.path.join(
            os.path.split(__file__)[0], '..', 'tests', 'thirty-two_meg.testfile')
    )
    filename = os.path.split(filepath)[1]

    # Commenting out while still in development, should be used in prod
    # try:
    #     assert os.path.isfile(os.path.join('/opt/files', filename))
    # except AssertionError:
    #     resp = jsonify(msg="file name is not valid")
    #     resp.status_code = 400
    #     return resp

    query = Challenges.query.filter(
        Challenges.filename == filename,
        Challenges.block == request_json.get('block'),
        Challenges.seed == request_json.get('seed'),
    )
    challenge = query.all()

    # Oh, and challenge should only be len of 1, or we gots problems
    if len(challenge) == 0:
        abort(404)
    elif len(challenge) < 1:
        abort(400)

    node_hb = Heartbeat(filepath, secret=config.SECRET_KEY)
    node_hb = load_heartbeat(node_hb, query)
    result = node_hb.check_answer(request_json.get('response'))

    if result is True:
        return jsonify(msg='ok', match=True)
    elif result is False:
        return jsonify(msg='ok', match=False)
    else:
        resp = jsonify(msg='error')
{'fraction_non_alphanumeric': 0.11570817818231968, 'fraction_numerical': 0.008550544752447937, 'mean_word_length': 3.0650224215246635, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '1971535', 'n_tokens_mistral': 2368, 'n_tokens_neox': 2210, 'n_words': 550}