Index: sitescripts/filterhits/common.py |
diff --git a/sitescripts/filterhits/common.py b/sitescripts/filterhits/common.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..661c5233a0fa1eb9b28cff45d29c0331ce43933e |
--- /dev/null |
+++ b/sitescripts/filterhits/common.py |
@@ -0,0 +1,48 @@ |
+# coding: utf-8 |
+ |
+# This file is part of the Adblock Plus web scripts, |
+# Copyright (C) 2006-2015 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/>. |
+ |
+from errno import EEXIST |
+import json, tempfile, time, os |
+ |
+def show_error(message, start_response, status="400 Processing Error"): |
+ start_response(status, [("Content-Type", "text/plain; charset=utf-8")]) |
+ return [message.encode("utf-8")] |
+ |
+def log_filterhits(data, basepath, query_string): |
+ """ |
+ This logs the provided filterhits data as JSON to a file named after |
+ the current timestamp in a directory named after the current date. |
+ """ |
+ now = time.gmtime() |
+ |
+ dir_name = time.strftime("%Y-%m-%d", now) |
+ path = os.path.join(basepath, dir_name) |
+ try: |
+ os.makedirs(path) |
+ except OSError as e: |
+ if e.errno != EEXIST: |
+ raise |
+ |
+ with tempfile.NamedTemporaryFile( |
+ prefix = str(int(time.mktime(now))) + "-", |
+ suffix = ".log", |
+ dir = path, |
+ delete = False |
+ ) as f: |
+ f.write("[%s] \"%s\" %s\n" % (time.strftime('%d/%b/%Y:%H:%M:%S', now), |
+ query_string, json.dumps(data))) |
+ return f.name |