Ejemplo n.º 1
0
def sqlite_handler(request):
    short_name = request.GET.get("name")
    apikey = request.GET.get("apikey", None)

    scraper, err = getscraperorresponse(short_name)
    if err:
        result = json.dumps({"error": err, "short_name": short_name})
        if request.GET.get("callback"):
            result = "%s(%s)" % (request.GET.get("callback"), result)
        return HttpResponse(result)

    u, s, kd = None, None, ""
    if request.user.is_authenticated():
        u = request.user

    if scraper.privacy_status != "private":
        s = scraper  # XX why this only when not private? FAI
        kd = short_name
    else:
        # When private we MUST have an apikey and it should match
        if not scraper.api_actionauthorized(apikey):
            result = json.dumps({"error": "Invalid API Key", "short_name": short_name})
            if request.GET.get("callback"):
                result = "%s(%s)" % (request.GET.get("callback"), result)
            return HttpResponse(result)

    APIMetric.record("sqlite", key_data=kd, user=u, code_object=s)

    dataproxy = DataStore(request.GET.get("name"))
    lattachlist = request.GET.get("attach", "").split(";")
    attachlist = []
    for aattach in lattachlist:
        if aattach:
            aa = aattach.split(",")
            attachi = {"name": aa[0], "asname": (len(aa) == 2 and aa[1] or None)}
            attachlist.append(attachi)
            dataproxy.request(
                {
                    "maincommand": "sqlitecommand",
                    "command": "attach",
                    "name": attachi["name"],
                    "asname": attachi["asname"],
                }
            )

    sqlquery = request.GET.get("query", "")
    format = request.GET.get("format", "json")
    if format == "json":
        format = "jsondict"

    req = {"maincommand": "sqliteexecute", "sqlquery": sqlquery, "data": None, "attachlist": attachlist}
    if format == "csv":
        req["streamchunking"] = 1000

    # This is inlined from the dataproxy.request() function to allow for
    # receiveoneline to perform multiple readlines in this case.
    # (this is the stream-chunking thing.  the right interface is not yet
    # apparent)

    dataproxy.m_socket.sendall(json.dumps(req) + "\n")

    if format not in ["jsondict", "jsonlist", "csv", "htmltable", "rss2"]:
        dataproxy.close()
        return HttpResponse("Error: the format '%s' is not supported" % format)

    if format in ["csv", "htmltable"]:
        return out_csvhtml(dataproxy, scraper.short_name, format)
    if format == "rss2":
        return out_rss2(dataproxy, scraper)

    return out_json(dataproxy, request.GET.get("callback"), scraper.short_name, format)
Ejemplo n.º 2
0
def sqlite_handler(request):
    short_name = request.GET.get('name')
    apikey = request.GET.get('apikey', None)

    scraper, err = getscraperorresponse(short_name)
    if err:
        result = json.dumps({'error': err, "short_name": short_name})
        if request.GET.get("callback"):
            result = "%s(%s)" % (request.GET.get("callback"), result)
        return HttpResponse(result)

    u, s, kd = None, None, ""
    if request.user.is_authenticated():
        u = request.user

    if scraper.privacy_status != "private":
        s = scraper  # XX why this only when not private? FAI
        kd = short_name
    else:
        # When private we MUST have an apikey and it should match
        if not scraper.api_actionauthorized(apikey):
            result = json.dumps({
                'error': "Invalid API Key",
                "short_name": short_name
            })
            if request.GET.get("callback"):
                result = "%s(%s)" % (request.GET.get("callback"), result)
            return HttpResponse(result)

    APIMetric.record("sqlite", key_data=kd, user=u, code_object=s)

    dataproxy = DataStore(request.GET.get('name'))
    lattachlist = request.GET.get('attach', '').split(";")
    attachlist = []
    for aattach in lattachlist:
        if aattach:
            aa = aattach.split(",")
            attachi = {
                "name": aa[0],
                "asname": (len(aa) == 2 and aa[1] or None)
            }
            attachlist.append(attachi)
            dataproxy.request({
                "maincommand": "sqlitecommand",
                "command": "attach",
                "name": attachi["name"],
                "asname": attachi["asname"]
            })

    sqlquery = request.GET.get('query', "")
    format = request.GET.get("format", "json")
    if format == "json":
        format = "jsondict"

    req = {
        "maincommand": "sqliteexecute",
        "sqlquery": sqlquery,
        "data": None,
        "attachlist": attachlist
    }
    if format == "csv":
        req["streamchunking"] = 1000

    # This is inlined from the dataproxy.request() function to allow for
    # receiveoneline to perform multiple readlines in this case.
    # (this is the stream-chunking thing.  the right interface is not yet
    # apparent)

    dataproxy.m_socket.sendall(json.dumps(req) + '\n')

    if format not in [
            "jsondict", "jsonlist", "csv", "htmltable", "rss2",
            "base64singleton", "htmltable_unescaped"
    ]:
        dataproxy.close()
        return HttpResponse("Error: the format '%s' is not supported" % format)

    if format in ["csv", 'htmltable', 'htmltable_unescaped']:
        return out_csvhtml(dataproxy, scraper.short_name, format)
    if format == "rss2":
        return out_rss2(dataproxy, scraper)
    if format == "base64singleton":
        return out_base64singleton(dataproxy,
                                   request.GET.get('mimetype', "text/plain"))

    return out_json(dataproxy, request.GET.get("callback"), scraper.short_name,
                    format)