LEFT | RIGHT |
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, |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
14 # | 14 # |
15 # You should have received a copy of the GNU General Public License | 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/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 |
18 import copy | 18 import copy |
19 import json | 19 import json |
20 import random | 20 import random |
21 import time | 21 import time |
22 import urlparse | 22 import urlparse |
23 | 23 |
24 from sitescripts.notifications.parser import load_notifications | 24 from sitescripts.notifications.parser import load_notifications |
25 from sitescripts.web import url_handler | 25 from sitescripts.web import url_handler |
| 26 |
| 27 def _determine_groups(version, notifications): |
| 28 version_groups = dict(x.split("/") for x in version.split("-")[1:] |
| 29 if x.count("/") == 1) |
| 30 groups = [] |
| 31 for notification in notifications: |
| 32 if "variants" not in notification: |
| 33 continue |
| 34 group_id = notification["id"] |
| 35 if group_id in version_groups: |
| 36 groups.append({"id": group_id, "variant": int(version_groups[group_id])}) |
| 37 return groups |
26 | 38 |
27 def _assign_groups(notifications): | 39 def _assign_groups(notifications): |
28 groups = [] | 40 groups = [] |
29 selection = random.random() | 41 selection = random.random() |
30 start = 0 | 42 start = 0 |
31 for notification in notifications: | 43 for notification in notifications: |
32 if "variants" not in notification: | 44 if "variants" not in notification: |
33 continue | 45 continue |
34 group = {"id": notification["id"], "variant": 0} | 46 group = {"id": notification["id"], "variant": 0} |
35 groups.append(group) | 47 groups.append(group) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 "version": _generate_version(groups), | 84 "version": _generate_version(groups), |
73 "notifications": notifications | 85 "notifications": notifications |
74 } | 86 } |
75 return json.dumps(response, ensure_ascii=False, indent=2, | 87 return json.dumps(response, ensure_ascii=False, indent=2, |
76 separators=(",", ": "), sort_keys=True) | 88 separators=(",", ": "), sort_keys=True) |
77 | 89 |
78 @url_handler("/notification.json") | 90 @url_handler("/notification.json") |
79 def notification(environ, start_response): | 91 def notification(environ, start_response): |
80 params = urlparse.parse_qs(environ.get("QUERY_STRING", "")) | 92 params = urlparse.parse_qs(environ.get("QUERY_STRING", "")) |
81 version = params.get("lastVersion", [""])[0] | 93 version = params.get("lastVersion", [""])[0] |
82 current_groups = dict(x.split("/") for x in version.split("-")[1:] | |
83 if x.count("/") == 1) | |
84 notifications = load_notifications() | 94 notifications = load_notifications() |
85 groups = [] | 95 groups = _determine_groups(version, notifications) |
86 for notification in notifications: | |
87 if "variants" not in notification: | |
88 continue | |
89 group_id = notification["id"] | |
90 if group_id in current_groups: | |
91 groups.append({"id": group_id, "variant": int(current_groups[group_id])}) | |
92 if not groups: | 96 if not groups: |
93 groups = _assign_groups(notifications) | 97 groups = _assign_groups(notifications) |
94 response = _create_response(notifications, groups) | 98 response = _create_response(notifications, groups) |
95 start_response("200 OK", | 99 start_response("200 OK", |
96 [("Content-Type", "application/json; charset=utf-8")]) | 100 [("Content-Type", "application/json; charset=utf-8")]) |
97 return response.encode("utf-8") | 101 return response.encode("utf-8") |
LEFT | RIGHT |