OLD | NEW |
1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, |
2 # Copyright (C) 2006-present eyeo GmbH | 2 # Copyright (C) 2006-present eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
(...skipping 16 matching lines...) Expand all Loading... |
27 version_groups = dict(x.split('/') for x in version.split('-')[1:] | 27 version_groups = dict(x.split('/') for x in version.split('-')[1:] |
28 if x.count('/') == 1) | 28 if x.count('/') == 1) |
29 groups = [] | 29 groups = [] |
30 for notification in notifications: | 30 for notification in notifications: |
31 group_id = notification['id'] | 31 group_id = notification['id'] |
32 if group_id in version_groups: | 32 if group_id in version_groups: |
33 groups.append({'id': group_id, 'variant': int(version_groups[group_i
d])}) | 33 groups.append({'id': group_id, 'variant': int(version_groups[group_i
d])}) |
34 return groups | 34 return groups |
35 | 35 |
36 | 36 |
37 def _assign_groups(notifications): | 37 def _assign_groups(groups, notifications): |
38 groups = [] | |
39 selection = random.random() | 38 selection = random.random() |
40 start = 0 | 39 start = 0 |
41 for notification in notifications: | 40 for notification in notifications: |
42 if 'variants' not in notification: | 41 if 'variants' not in notification: |
43 continue | 42 continue |
| 43 if notification['id'] in [g['id'] for g in groups]: |
| 44 continue |
44 group = {'id': notification['id'], 'variant': 0} | 45 group = {'id': notification['id'], 'variant': 0} |
45 groups.append(group) | 46 groups.append(group) |
46 for i, variant in enumerate(notification['variants']): | 47 for i, variant in enumerate(notification['variants']): |
47 sample_size = variant['sample'] | 48 sample_size = variant['sample'] |
48 end = start + sample_size | 49 end = start + sample_size |
49 selected = sample_size > 0 and start <= selection <= end | 50 selected = sample_size > 0 and start <= selection <= end |
50 start = end | 51 start = end |
51 if selected: | 52 if selected: |
52 group['variant'] = i + 1 | 53 group['variant'] = i + 1 |
53 break | 54 break |
54 return groups | |
55 | 55 |
56 | 56 |
57 def _get_active_variant(notifications, groups): | 57 def _get_active_variant(notifications, groups): |
58 for group in groups: | 58 for group in groups: |
59 group_id = group['id'] | 59 group_id = group['id'] |
60 variant = group['variant'] | 60 variant = group['variant'] |
61 if variant == 0: | 61 if variant == 0: |
62 continue | 62 continue |
63 notification = next((x for x in notifications if x['id'] == group_id), N
one) | 63 notification = next((x for x in notifications if x['id'] == group_id), N
one) |
64 if not notification: | 64 if not notification: |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 } | 104 } |
105 | 105 |
106 | 106 |
107 @url_handler('/notification.json') | 107 @url_handler('/notification.json') |
108 def notification(environ, start_response): | 108 def notification(environ, start_response): |
109 params = urlparse.parse_qs(environ.get('QUERY_STRING', '')) | 109 params = urlparse.parse_qs(environ.get('QUERY_STRING', '')) |
110 version = params.get('lastVersion', [''])[0] | 110 version = params.get('lastVersion', [''])[0] |
111 notifications = load_notifications() | 111 notifications = load_notifications() |
112 groups = _determine_groups(version, notifications) | 112 groups = _determine_groups(version, notifications) |
113 notifications = [x for x in notifications if not x.get('inactive', False)] | 113 notifications = [x for x in notifications if not x.get('inactive', False)] |
114 if not groups: | 114 _assign_groups(groups, notifications) |
115 groups = _assign_groups(notifications) | |
116 response = _create_response(notifications, groups) | 115 response = _create_response(notifications, groups) |
117 response_headers = [('Content-Type', 'application/json; charset=utf-8'), | 116 response_headers = [('Content-Type', 'application/json; charset=utf-8'), |
118 ('ABP-Notification-Version', response['version'])] | 117 ('ABP-Notification-Version', response['version'])] |
119 response_body = json.dumps(response, ensure_ascii=False, indent=2, | 118 response_body = json.dumps(response, ensure_ascii=False, indent=2, |
120 separators=(',', ': '), | 119 separators=(',', ': '), |
121 sort_keys=True).encode('utf-8') | 120 sort_keys=True).encode('utf-8') |
122 start_response('200 OK', response_headers) | 121 start_response('200 OK', response_headers) |
123 return response_body | 122 return response_body |
OLD | NEW |