| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 variant = group["variant"] | 61 variant = group["variant"] |
| 62 if variant == 0: | 62 if variant == 0: |
| 63 continue | 63 continue |
| 64 notification = next(x for x in notifications if x["id"] == group_id) | 64 notification = next(x for x in notifications if x["id"] == group_id) |
| 65 notification = copy.deepcopy(notification) | 65 notification = copy.deepcopy(notification) |
| 66 notification.update(notification["variants"][variant - 1]) | 66 notification.update(notification["variants"][variant - 1]) |
| 67 for key_to_remove in ("sample", "variants"): | 67 for key_to_remove in ("sample", "variants"): |
| 68 notification.pop(key_to_remove, None) | 68 notification.pop(key_to_remove, None) |
| 69 return notification | 69 return notification |
| 70 | 70 |
| 71 def _can_be_shown(notification): |
| 72 return "title" in notification and "message" in notification |
| 73 |
| 71 def _generate_version(groups): | 74 def _generate_version(groups): |
| 72 version = time.strftime("%Y%m%d%H%M", time.gmtime()) | 75 version = time.strftime("%Y%m%d%H%M", time.gmtime()) |
| 73 for group in groups: | 76 for group in groups: |
| 74 version += "-%s/%s" % (group["id"], group["variant"]) | 77 version += "-%s/%s" % (group["id"], group["variant"]) |
| 75 return version | 78 return version |
| 76 | 79 |
| 77 def _create_response(notifications, groups): | 80 def _get_notifications_to_send(notifications, groups): |
| 78 active_variant = _get_active_variant(notifications, groups) | 81 active_variant = _get_active_variant(notifications, groups) |
| 79 if active_variant: | 82 if active_variant: |
| 80 notifications = [active_variant] | 83 return [active_variant] if _can_be_shown(active_variant) else [] |
| 81 else: | 84 |
| 82 notifications = [x for x in notifications if "variants" not in x] | 85 notifications_to_send = [] |
| 83 response = { | 86 for notification in notifications: |
| 87 if not _can_be_shown(notification): |
| 88 continue |
| 89 if "variants" in notification: |
| 90 notification = copy.deepcopy(notification) |
| 91 del notification["variants"] |
| 92 notifications_to_send.append(notification) |
| 93 return notifications_to_send |
| 94 |
| 95 def _create_response(notifications, groups): |
| 96 return { |
| 84 "version": _generate_version(groups), | 97 "version": _generate_version(groups), |
| 85 "notifications": notifications | 98 "notifications": _get_notifications_to_send(notifications, groups) |
| 86 } | 99 } |
| 87 return response | |
| 88 | 100 |
| 89 @url_handler("/notification.json") | 101 @url_handler("/notification.json") |
| 90 def notification(environ, start_response): | 102 def notification(environ, start_response): |
| 91 params = urlparse.parse_qs(environ.get("QUERY_STRING", "")) | 103 params = urlparse.parse_qs(environ.get("QUERY_STRING", "")) |
| 92 version = params.get("lastVersion", [""])[0] | 104 version = params.get("lastVersion", [""])[0] |
| 93 notifications = load_notifications() | 105 notifications = load_notifications() |
| 94 groups = _determine_groups(version, notifications) | 106 groups = _determine_groups(version, notifications) |
| 95 if not groups: | 107 if not groups: |
| 96 groups = _assign_groups(notifications) | 108 groups = _assign_groups(notifications) |
| 97 response = _create_response(notifications, groups) | 109 response = _create_response(notifications, groups) |
| 98 response_headers = [("Content-Type", "application/json; charset=utf-8"), | 110 response_headers = [("Content-Type", "application/json; charset=utf-8"), |
| 99 ("ABP-Notification-Version", response["version"])] | 111 ("ABP-Notification-Version", response["version"])] |
| 100 response_body = json.dumps(response, ensure_ascii=False, indent=2, | 112 response_body = json.dumps(response, ensure_ascii=False, indent=2, |
| 101 separators=(",", ": "), | 113 separators=(",", ": "), |
| 102 sort_keys=True).encode("utf-8") | 114 sort_keys=True).encode("utf-8") |
| 103 start_response("200 OK", response_headers) | 115 start_response("200 OK", response_headers) |
| 104 return response_body | 116 return response_body |
| OLD | NEW |