コード例 #1
0
ファイル: api.py プロジェクト: kanicreampasta/rakutanAPI-py
def get_lecture_by_search_word(search_word=None):
    search_word = search_word.strip().replace('%', '%')
    res = fn.get_lecture_by_search_word(search_word)
    if res.result == "success":
        tmp = []
        for rakutan in res.rakutanList:
            tmp.append(Rakutan.to_dict(rakutan))
        return jsonify({"searchResult": tmp, "searchCount": res.count})
    else:
        return res.result
コード例 #2
0
ファイル: func.py プロジェクト: kanicreampasta/rakutanAPI-py
def get_lecture_by_search_word(search_word):
    """
    Find rakutan info from search word.
    :param search_word: (str) search word
    :return: (dict) if success -> "result" would be "success" otherwise error message will be placed here.
    And if success -> "rakutanList" will hold Rakutan objects list.
    """
    db = DB()

    # if search word has % in first letter, partial match search will be performed
    if search_word[0] == '%':
        query = {
            'lectureName': {
                '$regex': f'{search_word[1:]}',
                '$options': 'i'
            }
        }
    else:
        query = {'lectureName': {'$regex': f'^{search_word}', '$options': 'i'}}

    # result, count, queryResult = db.find('rakutan', query, projection={'_id': False})
    result, count, queryResult = [
        *db.find('rakutan2020', query, projection={
            '_id': False
        }).values()
    ]

    res = DotDict({"result": None, "count": None, "rakutanList": None})

    if result == "success":
        if count == 0:
            res.result = response[2404].format(search_word)
        else:
            res.result = "success"
            res.count = count
            res.rakutanList = Rakutan.from_list(queryResult)
    else:
        res.result = response[2001].format(search_word)

    return res
コード例 #3
0
ファイル: func.py プロジェクト: tinaxd/rakutanAPI-py
def get_lecture_by_id(kid):
    """
    Find rakutan info from lecture ID
    :param kid: (int) kougi ID(lecture ID)
    :return: (dict) if success -> "result" would be "success" otherwise error message will be placed here.
    And if success -> "rakutan" will hold a Rakutan object.
    """
    db = DB()
    query = {'id': int(kid)}
    result, count, queryResult = db.find('rakutan', query)

    res = {"result": None, "rakutan": None}

    if result == "success":
        if count == 0:
            res.result = response[1404].format(kid)
        else:
            res.result = "success"
            res.rakutan = Rakutan.from_dict(queryResult[0])
    else:
        res.result = response[1001].format(kid)

    return res
コード例 #4
0
ファイル: func.py プロジェクト: kanicreampasta/rakutanAPI-py
def get_lecture_by_id(lecID):
    """
    Find rakutan info from lecture ID
    :param lecID: (int) lecture ID
    :return: (dict) if success -> "result" would be "success" otherwise error message will be placed here.
    And if success -> "rakutan" will hold a Rakutan object.
    """
    db = DB()
    query = {'lecID': int(lecID)}
    result, count, queryResult = [*db.find('rakutan2020', query).values()]

    res = DotDict({"result": None, "rakutan": None})

    if result == "success":
        if count == 0:
            res.result = response[1404].format(lecID)
        else:
            res.result = "success"
            res.rakutan = Rakutan.from_dict(queryResult[0])
    else:
        res.result = response[1001].format(lecID)

    return res
コード例 #5
0
ファイル: api.py プロジェクト: kanicreampasta/rakutanAPI-py
def get_lecture_by_id(lecID=None):
    res = fn.get_lecture_by_id(lecID)
    if res.result == "success":
        return jsonify(Rakutan.to_dict(res.rakutan))
    else:
        return res.result
コード例 #6
0
ファイル: api.py プロジェクト: kanicreampasta/rakutanAPI-py
def get_omikuji(omikujiType=None):
    res = fn.get_omikuji(omikujiType)
    if res.result == "success":
        return jsonify(Rakutan.to_dict(res.rakutan))
    else:
        return res.result
コード例 #7
0
ファイル: func.py プロジェクト: kanicreampasta/rakutanAPI-py
def get_omikuji(omikujiType):
    """
    Get omikuji.
    :param omikujiType: (str) omikuji type. ["normal", "oni"]
    :return: (dict) if success -> "result" would be "success" otherwise error message will be placed here.
    And if success -> "rakutan" will hold a Rakutan object.
    """
    db = DB()

    res = DotDict({"result": None, "rakutan": None})

    if omikujiType == "oni":
        query = {
            '$and': [{
                'facultyName': '国際高等教育院'
            }, {
                'total.0': {
                    '$gt': 4
                }
            }, {
                '$expr': {
                    '$lt': [{
                        '$arrayElemAt': ['$accepted', 0]
                    }, {
                        '$multiply': [0.31, {
                            '$arrayElemAt': ['$total', 0]
                        }]
                    }]
                }
            }]
        }
    elif omikujiType == "normal":
        query = {
            '$and': [{
                'facultyName': '国際高等教育院'
            }, {
                'accepted.0': {
                    '$gt': 15
                }
            }, {
                '$expr': {
                    '$gt': [{
                        '$arrayElemAt': ['$accepted', 0]
                    }, {
                        '$multiply': [0.8, {
                            '$arrayElemAt': ['$total', 0]
                        }]
                    }]
                }
            }]
        }
    else:
        res.result = response[4002].format(omikujiType)
        return res

    result, count, queryResult = [*db.find('rakutan2020', query).values()]

    if result == "success":
        if count == 0:
            res.result = response[4404].format(omikujiType)
        else:
            res.result = "success"
            res.rakutan = random.choice(Rakutan.from_list(queryResult))
    else:
        res.result = response[4001].format(omikujiType)

    return res