| Index: sitescripts/filterhits/web/query.py |
| diff --git a/sitescripts/filterhits/web/query.py b/sitescripts/filterhits/web/query.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b7ee2f05c1a8c3080f06fb67573708eefbf0cb64 |
| --- /dev/null |
| +++ b/sitescripts/filterhits/web/query.py |
| @@ -0,0 +1,65 @@ |
| +# coding: utf-8 |
| + |
| +# This file is part of the Adblock Plus web scripts, |
| +# Copyright (C) 2006-2014 Eyeo GmbH |
| +# |
| +# Adblock Plus is free software: you can redistribute it and/or modify |
| +# it under the terms of the GNU General Public License version 3 as |
| +# published by the Free Software Foundation. |
| +# |
| +# Adblock Plus is distributed in the hope that it will be useful, |
| +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| +# GNU General Public License for more details. |
| +# |
| +# You should have received a copy of the GNU General Public License |
| +# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| + |
| +import os, MySQLdb, json |
| +from urlparse import parse_qsl |
| +from sitescripts.web import url_handler |
| +from sitescripts.utils import cached, get_config, setupStderr |
| + |
|
Sebastian Noack
2015/02/11 16:00:12
Nit: The newline should be between the corelib and
kzar
2015/02/17 10:52:24
Done.
|
| +import sitescripts.filterhits.common as common |
|
Sebastian Noack
2015/02/11 16:00:12
Nit: You are misusing the "import .. as" syntax he
kzar
2015/02/17 10:52:24
Done.
|
| +import sitescripts.filterhits.db as db |
| + |
| +def query_sql(domain=None, filter=None, skip=0, take=20, order_by="hits DESC", **_): |
| + sql = """SELECT SQL_CALC_FOUND_ROWS domain, filter, hits |
| + FROM geometrical_mean as g |
| + LEFT JOIN filters as f ON f.md5=g.filter_md5 |
| + %s |
| + ORDER BY %s |
| + LIMIT %d, %d;""" |
| + where = ["domain LIKE '%%%s%%'" % db.escape(domain) if domain else None, |
| + "filter LIKE '%%%s%%'" % db.escape(filter) if filter else None] |
| + where = " AND ".join([f for f in where if f]) |
| + where = "WHERE " + where if where else "" |
| + return sql % (where, db.escape(order_by), int(skip), int(take)) |
| + |
| +@url_handler("/query") |
| +def query(environ, start_response): |
| + setupStderr(environ["wsgi.errors"]) |
| + config = get_config() |
| + params = dict(parse_qsl(environ.get('QUERY_STRING', ''))) |
| + |
| + try: |
| + db.connect(config.get("filterhitstats", "dbuser"), |
|
Sebastian Noack
2015/02/11 16:00:12
It seems this pattern is repeated. How about retri
Sebastian Noack
2015/02/17 14:59:17
What's about this comment?
kzar
2015/02/24 18:05:11
I prefer to do it this way, during development I e
Sebastian Noack
2015/02/26 16:39:25
That doesn't make any sense. The database and cred
kzar
2015/02/28 19:39:56
OK, I changed my mind here and I've now changed it
|
| + config.get("filterhitstats", "dbpassword"), |
| + config.get("filterhitstats", "database")) |
| + results = db.query(query_sql(**params), dict_result=True) |
| + total = db.query("SELECT FOUND_ROWS();")[0][0] |
| + except MySQLdb.Error: |
| + return common.showError("Failed to query database!", start_response, |
| + "500 Database error") |
| + finally: |
| + db.disconnect() |
| + |
| + try: |
| + echo = int(params["echo"]) |
| + except (ValueError, KeyError): |
| + echo = 0 |
| + |
| + response_headers = [("Content-type", "application/json")] |
| + start_response("200 OK", response_headers) |
| + return [json.dumps({"results": results, "echo": echo, |
| + "total": total, "count": len(results)})] |