def tokeVerify():
    access_token = request.get_json()["token"]
    course = request.get_json()["course"]
    their_token = request.get_json()["piazzaToken"]

    claims = get_claims(access_token, app.config["APP_ID"])
    if claims["scope"] == "Unauthorized":
        return "NO"
    else:
        auth_courses = [
            app.config["COURSE_MAPPING"].get(claim, {
                "courseName": ""
            }).get("courseName") for claim in claims["scope"]
        ]
    # print("HELLOR: {}".format(course not in auth_courses))
    if course not in auth_courses:
        return "NO"
    if course == "CS 4300":
        if their_token == "4300":
            return "OK"
        else:
            return "NO"
    h = html2text.HTML2Text()
    h.ignore_links = True
    parsed_piazza = h.handle(coursePiazzaDict[course].get_post(
        app.config["PIAZZA_" + course.replace(" ", "") +
                   "_TOKEN_POST"])["history"][0]["content"])
    split_piazza = parsed_piazza.split("\n")
    piazza_token = split_piazza[0]
    if piazza_token == their_token:
        return "OK"
    else:
        return "NO"
def get_user_courses():
    access_token = request.get_json()["token"]
    claims = get_claims(access_token, app.config["APP_ID"])
    if claims["scope"] == "Unauthorized":
        return jsonify([])
    else:
        to_json = []
        for claim in claims["scope"]:
            if app.config["COURSE_MAPPING"].get(claim, "") != "":
                to_json.append(app.config["COURSE_MAPPING"].get(claim))
        to_json = sorted(to_json, key=lambda x: x["courseName"])
        for d in to_json:
            d["add"] = False
        if "AddCourse" in claims["scope"]:
            to_json.append({
                "courseName": "Add Course",
                "add": True,
                "protected": True
            })

        return jsonify(to_json)
def search_results():
    access_token = request.get_json()["token"]
    if user_jwt_required(access_token, app.config["APP_ID"]):
        orig_query = unquote(request.get_json()["query"])
        app.logger.info("User queried: {}".format(orig_query))
        courseSelection = request.get_json()["course"]
        app.logger.info("User course: {}".format(courseSelection))
        # results = cosineSim(orig_query, vecPy.docVecDictionary , courseSelection, vecPy.courseRevsereIndexDictionary)

        # search selection: Default(both),Piazza only, Resource only
        # [Default, Piazza, Resource]

        searchSelection = request.get_json()["search"]
        # searchSelection = "Default"

        # Modify query based on concepts
        query = concept_modify_query(orig_query)
        app.logger.info("Modified Query: {}".format(query))

        # if searchSelection == "Default":
        # regular cosine similarity (start commenting out here)
        updated_query = get_all_tokens(query)
        cosine_results = cosineSim(updated_query, vecPy.docVecDictionary,
                                   courseSelection,
                                   vecPy.courseRevsereIndexDictionary)
        boolean_results = boolean(query, courseSelection)
        svd_results = LSI_SVD(updated_query, vecPy.docVecDictionary,
                              courseSelection,
                              vecPy.courseRevsereIndexDictionary,
                              vecPy.svdDictionary)

        # finalresults = results #np.multiply(results,vecPy.sourceDictionary[courseSelection])
        if (len(cosine_results) == 0 or len(svd_results) == 0):
            return jsonify([])
        finalresults = np.add(np.multiply(svd_results, boolean_results),
                              np.multiply(cosine_results, boolean_results))
        results_filter = (finalresults > 0.1)
        n = 50  # top x highest

        if len(finalresults) == 0:
            return jsonify([])

        reverseList = (-finalresults).argsort()  # [:n]
        reverseList_filter = results_filter[reverseList]

        n = min(sum(reverseList_filter), n)

        for item, score in zip(
                vecPy.courseDocDictionary[courseSelection][reverseList]
            [reverseList_filter],
                finalresults[reverseList][reverseList_filter]):
            item["score"] = score

        if courseSelection == "CS 4300":
            claims = get_claims(access_token, app.config["APP_ID"])
            to_json = []
            for claim in claims["scope"]:
                if app.config["COURSE_MAPPING"].get(claim, "") != "":
                    to_json.append(app.config["COURSE_MAPPING"].get(claim).get(
                        "courseName"))

            if "CS 4300" in to_json:
                pass
            else:
                return jsonify([])

            h = html2text.HTML2Text()
            h.ignore_links = True
            parsed_piazza = h.handle(coursePiazzaDict["CS 4300"].get_post(
                app.config["PIAZZA_CS4300_TOKEN_POST"])["history"][0]
                                     ["content"])
            split_piazza = parsed_piazza.split("\n")
            piazza_token = split_piazza[0]
            our_token = app.config["PIAZZA_CS4300_TOKEN"]
            keep_piazza = (piazza_token == our_token)

            # app.logger.info("Parsed Piazza: {}".format(repr(parsed_piazza)))
            # app.logger.info("Split Piazza: {}".format(repr(split_piazza)))
            # app.logger.info("Piazza Response: {}".format(repr(piazza_token)))
            # app.logger.info("Our token is: {}".format(repr(our_token)))
            # app.logger.info("Keeping Piazza? {}".format(keep_piazza))

            if keep_piazza:
                return jsonify(vecPy.courseDocDictionary[courseSelection]
                               [reverseList][reverseList_filter].tolist()[:n])

            else:
                modified_results = list(
                    filter(
                        lambda x: x["type"] != "Piazza",
                        vecPy.courseDocDictionary[courseSelection][reverseList]
                        [reverseList_filter].tolist()))
                if len(modified_results) == 0:
                    return jsonify([])
                n = min(n, len(modified_results))
                return jsonify(modified_results[:n])

        return jsonify(vecPy.courseDocDictionary[courseSelection][reverseList]
                       [reverseList_filter].tolist()[:n])

    else:
        return "Not Authorized"