示例#1
0
async def get_quotes(request, modo, orden, query):
    # CORS
    request.setHeader('Access-Control-Allow-Origin', '*')
    request.setHeader('Access-Control-Allow-Methods', 'GET')
    request.setHeader('Access-Control-Allow-Headers',
                      'x-prototype-version,x-requested-with')
    request.setHeader('Access-Control-Max-Age', "2520")

    runner = SpiderRunner()
    output_data = []
    filtros = None
    rango_minimo, rango_maximo = 0, math.inf
    sitios_a_buscar = SITES_TO_SEARCH

    if b"filtro" in request.args:
        filtros = [filtro.decode("utf-8")
                   for filtro in request.args[b"filtro"]]

    if b"rango" in request.args:
        rango_minimo, rango_maximo = request.args[b"rango"]
        rango_minimo = int(rango_minimo.decode("utf-8"))
        rango_maximo = int(rango_maximo.decode("utf-8"))
        rango_maximo = math.inf if rango_maximo == -1 else rango_maximo

    if filtros is not None:
        sitios_a_buscar = [
            sitio for sitio in sitios_a_buscar if sitio not in filtros]

    _encoder = ScrapyJSONEncoder(ensure_ascii=True)
    for site in sitios_a_buscar:
        if site == "steampowered":
            results = await runner.crawl(SteamSpider, modo=modo, query=query, url_search=CONFIG_SITE[site]["url_search"], rango=(rango_minimo, rango_maximo))
        elif site == "nuuvem":
            results = await runner.crawl(NuuvemSpider, modo=modo, query=query, url_search=CONFIG_SITE[site]["url_search"], rango=(rango_minimo, rango_maximo))
        elif site == "gog":
            results = await runner.crawl(GOGSpider, modo=modo, query=query, url_search=CONFIG_SITE[site]["url_search"], rango=(rango_minimo, rango_maximo))
        elif site == "gamesplanet":
            results = await runner.crawl(GamesPlantetSpider, modo=modo, query=query, url_search=CONFIG_SITE[site]["url_search"], rango=(rango_minimo, rango_maximo))
        output = return_spider_output(results, output_data, site)
        output_data = output
    if orden != "default":
        if orden != "relevancia":
            tipo_orden, indice_orden = orden.split("_")
        else: 
            tipo_orden = orden
            indice_orden = ""
        output_data = sort_data(output_data, tipo_orden, indice_orden, query)

    return _encoder.encode(output_data)
 def test_sort_metro_by_relevance(self):
     r = sort_data(BUSQUEDA_METRO_SIN_ORDENAR, "relevancia", "desc",
                   "metro")
     self.assertEqual(r, METRO_POR_REELEVANCIA)
 def test_sort_metro_by_Z_A_name(self):
     r = sort_data(BUSQUEDA_METRO_SIN_ORDENAR, "nombre", "desc", "metro")
     self.assertEqual(r, Z_A_DE_METRO_POR_TITULO)
 def test_sort_metro_by_descending_price(self):
     r = sort_data(BUSQUEDA_METRO_SIN_ORDENAR, "precio", "desc", "metro")
     self.assertEqual(r, MAYOR_A_MENOR_PRECIO_DE_METRO)