Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 # coding: utf-8 | |
2 | |
3 # This file is part of the Adblock Plus web scripts, | |
4 # Copyright (C) 2006-2014 Eyeo GmbH | |
5 # | |
6 # Adblock Plus is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU General Public License version 3 as | |
8 # published by the Free Software Foundation. | |
9 # | |
10 # Adblock Plus is distributed in the hope that it will be useful, | |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 # GNU General Public License for more details. | |
14 # | |
15 # You should have received a copy of the GNU General Public License | |
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
17 | |
18 from datetime import datetime | |
19 import json, tempfile, os | |
20 | |
21 def showError(message, start_response, status="400 Processing Error"): | |
22 start_response(status, [("Content-Type", "text/plain; charset=utf-8")]) | |
23 return [message.encode("utf-8")] | |
24 | |
25 def valid_filter_hit(filter_hit): | |
26 return ( | |
27 isinstance(filter_hit, dict) and | |
28 "thirdParty" in filter_hit and | |
29 isinstance(filter_hit["thirdParty"], dict) and | |
30 "firstParty" in filter_hit and | |
31 isinstance(filter_hit["firstParty"], dict) and | |
32 "subscriptions" in filter_hit and | |
33 isinstance(filter_hit["subscriptions"], (list, tuple)) | |
34 ) | |
35 | |
36 def valid_log_data(data): | |
37 """ | |
38 This returns True if the filterhits data passed is structured | |
39 roughly OK. Used as a quick check, it's not comprehensive. | |
40 """ | |
41 return ( | |
Sebastian Noack
2015/02/11 16:00:12
I wonder whether we should rather properly check f
kzar
2015/02/17 10:52:24
Done.
| |
42 isinstance(data, dict) and | |
43 "version" in data and | |
44 "timeSincePush" in data and | |
45 "addonName" in data and | |
46 "addonVersion" in data and | |
47 "application" in data and | |
48 "applicationVersion" in data and | |
49 "platform" in data and | |
50 "platformVersion" in data and | |
51 "filters" in data and | |
52 isinstance(data["filters"], dict) and | |
53 (not len(data["filters"]) or | |
54 valid_filter_hit(data["filters"].itervalues().next())) | |
55 ) | |
56 | |
57 def datetime_to_timestamp(dt): | |
58 return int((dt - datetime(1970, 1, 1)).total_seconds()) | |
Sebastian Noack
2015/02/11 16:00:12
Hardcoding the epoch looks like a hack to me. Did
kzar
2015/02/17 10:52:24
Done.
| |
59 | |
60 def log_filterhits(data, basepath, query_string): | |
61 """ | |
62 This logs the provided filterhits data as JSON to a file named after | |
63 the current timestamp in a directory named after the current date. | |
64 """ | |
65 now = datetime.now() | |
66 | |
67 dir_name = now.strftime("%Y-%m-%d") | |
68 path = os.path.join(basepath, dir_name) | |
69 if not os.path.exists(path): | |
Sebastian Noack
2015/02/11 16:00:12
I'd rather catch the OSError, checking for e.errno
kzar
2015/02/17 10:52:24
Done.
| |
70 os.makedirs(path) | |
71 | |
72 with tempfile.NamedTemporaryFile( | |
73 prefix = str(datetime_to_timestamp(now)) + "-", | |
74 suffix = ".log", | |
75 dir = path, | |
76 delete = False | |
77 ) as f: | |
78 f.write("[%s] \"%s\" %s\n" % (now.strftime('%d/%b/%Y:%H:%M:%S %z'), | |
79 query_string, json.dumps(data))) | |
80 return f.name | |
OLD | NEW |