<commit_before>"""
Core API Blueprints. 
"""

from flask import Blueprint, current_app, jsonify, request
from flaskutil import jsonp

from usersession import get_session_id, get_serverside_session
from tokengen import generate_unique_token
from secretkey import compute_signature

import json

blueprint = Blueprint('api', __name__)


@blueprint.route('/createSession')
@jsonp
def LL_create_session():
    token = generate_unique_token()
    videoId = request.args.get('videoId')
    ts = request.args.get('ts')
    session_id = get_session_id()
    
    mongo = current_app.mongo
    mongo.db.interactionSessions.insert({
        '_id': token,
        'videoId': videoId,
        'ts': ts,
        'interactions': [],
        'userSession': session_id
    })
    mongo.db.interactionSessions.ensure_index('videoId')
    mongo.db.interactionSessions.ensure_index('userSession')
    
    return jsonify({'token': token})


@blueprint.route('/sendInteractions')
@jsonp
def LL_send_interactions():
    mongo = current_app.mongo
    error = None
    session_id = get_session_id()
    token = request.args.get('token')
    interactionSession = mongo.db.interactionSessions.find_one({'_id': token})
    if interactionSession:
        if interactionSession['userSession'] == session_id:
            interactions = json.loads( request.args.get('interactions') )
            mongo.db.interactionSessions.update({'_id': token}, {
                '$pushAll': {'interactions': interactions}
            })
            
            likes = []
            for ts, evtType, tc, last_tc in interactions:
                if evtType == 'LIKE':
                    likes.append(tc)
            
            if likes:
                mongo.db.userSessions.update({'_id': session_id}, {
                    '$pushAll': {'likes.%s' % (interactionSession['videoId']): likes}
                })
            
        else:
            error = 403
    else:
        error = 404
    
    return jsonify({'ok': 'ok'} if not error else {'error': error})



@blueprint.route('/aggregate')
@jsonp
def LL_aggregate():
    userSession = get_serverside_session()
    videoId = request.args.get('videoId')
    
    numSessions = 0
    playbacks = []
    seeks = None
    mca = None
    likedPoints = []
    
    myLikes = userSession['likes'].get(videoId, [])
    
    for interactionSession in current_app.mongo.db.interactionSessions.find({'videoId': videoId}):
        numSessions += 1
        processInteractionSession(interactionSession['interactions'], playbacks, likedPoints)
    
    aggregate = dict(numSessions=numSessions, playbacks=playbacks, seeks=seeks, mca=mca, likedPoints = likedPoints, myLikes=myLikes)
    return jsonify(aggregate)

def processInteractionSession(interactions, playbacks, likedPoints):
    playback = []
    curStart = None
    last_tick = None
    last_last_tc = None
    
    for ts, evtType, tc, last_tc in sorted(interactions):
        if evtType == 'LIKE':
            likedPoints.append(tc)
        elif evtType == 'PLAYING':
            if curStart is not None:
                playback.append( (curStart, last_tc) )
            curStart = tc
        elif evtType == 'PAUSED':
            if curStart is not None:
                playback.append( (curStart, last_tc) )
                curStart = None
        
        last_last_tc = last_tc
    
    if curStart is not None and last_last_tc is not None:
        playback.append( (curStart, last_last_tc) )
    
    playbacks.append(playback)


@blueprint.route('/testKey', methods=['POST'])
def LL_testKey():
    try:
        data = json.loads(request.data)
        key = current_app.secret_key
        
        msg = data.get('msg','')
        their_sig = data.get('sig','')
        our_sig = compute_signature(key, msg)
        
        ok = our_sig == their_sig
        
        return jsonify({'ok': 'ok' if ok else 'no'}) 
        
    except ValueError, e:
        return jsonify({'error': e.message})<commit_msg>Add postMCA to API<commit_after>"""
Core API Blueprints. 
"""

from flask import Blueprint, current_app, jsonify, request
from flaskutil import jsonp

from usersession import get_session_id, get_serverside_session
from tokengen import generate_unique_token
from secretkey import compute_signature

import json

blueprint = Blueprint('api', __name__)


@blueprint.route('/createSession')
@jsonp
def LL_create_session():
    token = generate_unique_token()
    videoId = request.args.get('videoId')
    ts = request.args.get('ts')
    session_id = get_session_id()
    
    mongo = current_app.mongo
    mongo.db.interactionSessions.insert({
        '_id': token,
        'videoId': videoId,
        'ts': ts,
        'interactions': [],
        'userSession': session_id
    })
    mongo.db.interactionSessions.ensure_index('videoId')
    mongo.db.interactionSessions.ensure_index('userSession')
    
    return jsonify({'token': token})


@blueprint.route('/sendInteractions')
@jsonp
def LL_send_interactions():
    mongo = current_app.mongo
    error = None
    session_id = get_session_id()
    token = request.args.get('token')
    interactionSession = mongo.db.interactionSessions.find_one({'_id': token})
    if interactionSession:
        if interactionSession['userSession'] == session_id:
            interactions = json.loads( request.args.get('interactions') )
            mongo.db.interactionSessions.update({'_id': token}, {
                '$pushAll': {'interactions': interactions}
            })
            
            likes = []
            for ts, evtType, tc, last_tc in interactions:
                if evtType == 'LIKE':
                    likes.append(tc)
            
            if likes:
                mongo.db.userSessions.update({'_id': session_id}, {
                    '$pushAll': {'likes.%s' % (interactionSession['videoId']): likes}
                })
            
        else:
            error = 403
    else:
        error = 404
    
    return jsonify({'ok': 'ok'} if not error else {'error': error})



@blueprint.route('/aggregate')
@jsonp
def LL_aggregate():
    userSession = get_serverside_session()
    videoId = request.args.get('videoId')
    
    numSessions = 0
    playbacks = []
    seeks = None
    mca = None
    likedPoints = []
    
    myLikes = userSession['likes'].get(videoId, [])
    
    for interactionSession in current_app.mongo.db.interactionSessions.find({'videoId': videoId}):
        numSessions += 1
        processInteractionSession(interactionSession['interactions'], playbacks, likedPoints)
    
    aggregate = dict(numSessions=numSessions, playbacks=playbacks, seeks=seeks, mca=mca, likedPoints = likedPoints, myLikes=myLikes)
    return jsonify(aggregate)

def processInteractionSession(interactions, playbacks, likedPoints):
    playback = []
    curStart = None
    last_tick = None
    last_last_tc = None
    
    for ts, evtType, tc, last_tc in sorted(interactions):
        if evtType == 'LIKE':
            likedPoints.append(tc)
        elif evtType == 'PLAYING':
            if curStart is not None:
                playback.append( (curStart, last_tc) )
            curStart = tc
        elif evtType == 'PAUSED':
            if curStart is not None:
                playback.append( (curStart, last_tc) )
                curStart = None
        
        last_last_tc = last_tc
    
    if curStart is not None and last_last_tc is not None:
        playback.append( (curStart, last_last_tc) )
    
    playbacks.append(playback)


@blueprint.route('/testKey', methods=['POST'])
def LL_testKey():
    try:
        data = json.loads(request.data)
        key = current_app.secret_key
        
        msg = data.get('msg','')
        their_sig = data.get('sig','')
        our_sig = compute_signature(key, msg)
        
        ok = our_sig == their_sig
        
        return jsonify({'ok': 'ok' if ok else 'no'}) 
        
    except ValueError, e:
        return jsonify({'error': e.message})



@blueprint.route('/postMCA', methods=['POST'])
def LL_postMCA():
    try:
        raw_data = request.data
        key = current_app.secret_key
        their_sig = request.args.get('s')
        our_sig = compute_signature(key, raw_data)
        
        ok = our_sig == their_sig
        
        if not ok:
            return jsonify({'ok': 'no'})
        
        # EDIT ZONE BELOW ====
        
        data = json.loads(raw_data)
        
        videoId = data['videoId']
        mcaName = data['mcaName']
        mcaType = data['mcaType']
        mcaData = data['mcaData']
        
        mongo = current_app.mongo
        mongo.db.mca.update({'_id': videoId}, {'$set': {
            #'_id': videoId,
            'mca-%s' % mcaName: {
                'type': mcaType,
                'data': mcaData
            }
        }}, True)
        mongo.db.interactionSessions.ensure_index('mca')
        
        return jsonify({'ok': 'ok'}) 
        
    except ValueError, e:
        return jsonify({'error': e.message})

{'fraction_non_alphanumeric': 0.1166648113102527, 'fraction_numerical': 0.0017811421574084382, 'mean_word_length': 2.3224852071005917, 'pattern_counts': {'":': 0, '<': 3, '<?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': '736323', 'n_tokens_mistral': 2756, 'n_tokens_neox': 2732, 'n_words': 580}