예제 #1
0
def get_category_list():
    try:
        category_list = endpoints.get_categories(connection)
    except Exception as e:
        bottle.response.status = 500
        return {"error": f"Server error – {e}"}
    return {"results": category_list}
예제 #2
0
def index():
    query = bottle.request.query.q
    timeframe = bottle.request.query.timeframe
    category_filter = bottle.request.query.getall(
        'category')  # multiple params possible
    metric = bottle.request.query.metric
    entity = "papers"
    page = bottle.request.query.page
    page_size = bottle.request.query.page_size
    error = ""

    default_front = (metric == '' and timeframe == '')

    # Don't accept bad values for fields that we cache on
    if metric not in ["downloads", "twitter"]:
        metric = "twitter"
    if metric == "twitter":
        if timeframe == "":
            timeframe = "day"
        if timeframe not in ["alltime", "day", "week", "month", "year"]:
            error = f"There was a problem with the submitted query: {timeframe} is not a recognized timeframe for tweets."
            bottle.response.status = 400
            return {"error": error}
    elif metric == "downloads":
        if timeframe == "":
            timeframe = "alltime"
        if timeframe not in ["alltime", "ytd", "lastmonth"]:
            error = f"There was a problem with the submitted query: {timeframe} is not a recognized timeframe for downloads."
            bottle.response.status = 400
            return {"error": error}

    category_list = endpoints.get_categories(
        connection)  # list of all article categories

    # Get rid of a category filter that's just one empty parameter:
    if len(category_filter) == 1 and category_filter[0] == "":
        category_filter = []
    else:
        # otherwise validate that the categories are valid
        for cat in category_filter:
            if cat not in category_list:
                error = f"There was a problem with the submitted query: {cat} is not a recognized category."
                bottle.response.status = 400
                return {"error": error}

    if page == "" or page == None:
        page = 0
    else:
        try:
            page = int(page)
        except Exception as e:
            error = f"Problem recognizing specified page number: {e}"
    if page < 0:
        error = f"There was a problem with the submitted query: {page} is not a valid page number."
        bottle.response.status = 400
        return {"error": error}

    if page_size == "":
        page_size = config.default_page_size
    else:
        try:
            page_size = int(page_size)
        except Exception as e:
            error = f"Problem recognizing specified page size: {e}"
            page_size = 0

    if page_size > config.max_page_size:
        error = f"There was a problem with the submitted query: Max page size is {config.max_page_size}."
        bottle.response.status = 400
        return {"error": error}

    results = {}  # a list of articles for the current page
    totalcount = 0  # how many results there are in total

    if error == "":  # if nothing's gone wrong yet, fetch results:
        try:
            results, totalcount = endpoints.paper_query(
                query, category_filter, timeframe, metric, page, page_size,
                connection)
        except Exception as e:
            error = f"There was a problem with the submitted query: {e}"
            bottle.response.status = 500
            return {"error": error}
    # If daily twitter stats aren't available go weekly:
    if default_front and totalcount == 0:
        timeframe = 'week'
        try:
            results, totalcount = endpoints.paper_query(
                query, category_filter, timeframe, metric, page, page_size,
                connection)
        except Exception as e:
            error = f"There was a problem with the submitted query: {e}"
            bottle.response.status = 500
            return {"error": error}
    # If there is an unreasonably low number of weekly results, just roll over to downloads instead
    if default_front and totalcount < 150:
        metric = 'downloads'
        timeframe = 'lastmonth'
        try:
            results, totalcount = endpoints.paper_query(
                query, category_filter, timeframe, metric, page, page_size,
                connection)
        except Exception as e:
            error = f"There was a problem with the submitted query: {e}"
            bottle.response.status = 500
            return {"error": error}

    # CACHE CONTROL
    # website front page
    if bottle.request.query_string == "":
        bottle.response.set_header(
            "Cache-Control",
            f'max-age={config.cache["front_page"]}, stale-while-revalidate=172800'
        )
    # if it's a simple query:
    if query == "" and page < 3 and page_size == config.default_page_size:
        bottle.response.set_header(
            "Cache-Control",
            f'max-age={config.cache["simple"]}, stale-while-revalidate=172800')

    resp = models.PaperQueryResponse(results, query, timeframe,
                                     category_filter, metric, page, page_size,
                                     totalcount)
    return resp.json()