OLD | NEW |
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
4 # Copyright (C) 2006-2015 Eyeo GmbH | 4 # Copyright (C) 2006-2015 Eyeo GmbH |
5 # | 5 # |
6 # Adblock Plus is free software: you can redistribute it and/or modify | 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 | 7 # it under the terms of the GNU General Public License version 3 as |
8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
9 # | 9 # |
10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 if variant > 0: | 54 if variant > 0: |
55 notification = next(x for x in notifications if x["id"] == group_id) | 55 notification = next(x for x in notifications if x["id"] == group_id) |
56 notification = copy.deepcopy(notification) | 56 notification = copy.deepcopy(notification) |
57 notification.update(notification["variants"][variant - 1]) | 57 notification.update(notification["variants"][variant - 1]) |
58 for key_to_remove in ("sample", "variants"): | 58 for key_to_remove in ("sample", "variants"): |
59 notification.pop(key_to_remove, None) | 59 notification.pop(key_to_remove, None) |
60 response["notifications"] = [notification] | 60 response["notifications"] = [notification] |
61 if "notifications" not in response: | 61 if "notifications" not in response: |
62 response["notifications"] = [x for x in notifications | 62 response["notifications"] = [x for x in notifications |
63 if "variants" not in x] | 63 if "variants" not in x] |
64 return json.dumps(response, ensure_ascii=False, indent=2, | 64 return response |
65 separators=(",", ": "), sort_keys=True) | |
66 | 65 |
67 @url_handler("/notification.json") | 66 @url_handler("/notification.json") |
68 def notification(environ, start_response): | 67 def notification(environ, start_response): |
69 params = parse_qs(environ.get("QUERY_STRING", "")) | 68 params = parse_qs(environ.get("QUERY_STRING", "")) |
70 version = params.get("lastVersion", [""])[0] | 69 version = params.get("lastVersion", [""])[0] |
71 current_groups = dict(x.split("/") for x in version.split("-")[1:] | 70 current_groups = dict(x.split("/") for x in version.split("-")[1:] |
72 if x.count("/") == 1) | 71 if x.count("/") == 1) |
73 notifications = load_notifications() | 72 notifications = load_notifications() |
74 groups = [] | 73 groups = [] |
75 for notification in notifications: | 74 for notification in notifications: |
76 if "variants" not in notification: | 75 if "variants" not in notification: |
77 continue | 76 continue |
78 group_id = notification["id"] | 77 group_id = notification["id"] |
79 if group_id in current_groups: | 78 if group_id in current_groups: |
80 groups.append({"id": group_id, "variant": int(current_groups[group_id])}) | 79 groups.append({"id": group_id, "variant": int(current_groups[group_id])}) |
81 if not groups: | 80 if not groups: |
82 groups = _assign_groups(notifications) | 81 groups = _assign_groups(notifications) |
83 response = _create_response(notifications, groups) | 82 response = _create_response(notifications, groups) |
84 start_response("200 OK", | 83 response_headers = [("Content-Type", "application/json; charset=utf-8"), |
85 [("Content-Type", "application/json; charset=utf-8")]) | 84 ("ABP-Notification-Version", response["version"])] |
86 return response.encode("utf-8") | 85 response_body = json.dumps(response, ensure_ascii=False, indent=2, |
| 86 separators=(",", ": "), |
| 87 sort_keys=True).encode("utf-8") |
| 88 start_response("200 OK", response_headers) |
| 89 return response_body |
OLD | NEW |