Left: | ||
Right: |
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]: | |
Vasily Kuznetsov
2017/10/09 22:29:12
This should be a set and probably should be pre-ca
| |
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 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 } | 105 } |
105 | 106 |
106 | 107 |
107 @url_handler('/notification.json') | 108 @url_handler('/notification.json') |
108 def notification(environ, start_response): | 109 def notification(environ, start_response): |
109 params = urlparse.parse_qs(environ.get('QUERY_STRING', '')) | 110 params = urlparse.parse_qs(environ.get('QUERY_STRING', '')) |
110 version = params.get('lastVersion', [''])[0] | 111 version = params.get('lastVersion', [''])[0] |
111 notifications = load_notifications() | 112 notifications = load_notifications() |
112 groups = _determine_groups(version, notifications) | 113 groups = _determine_groups(version, notifications) |
113 notifications = [x for x in notifications if not x.get('inactive', False)] | 114 notifications = [x for x in notifications if not x.get('inactive', False)] |
114 if not groups: | 115 groups = _assign_groups(groups, notifications) |
wspee
2017/10/09 09:12:03
Ideally I would like to combine _determine_groups
Vasily Kuznetsov
2017/10/09 22:29:11
Since we remove inactive notifications in the midd
wspee
2017/10/10 09:03:53
Acknowledged.
| |
115 groups = _assign_groups(notifications) | |
116 response = _create_response(notifications, groups) | 116 response = _create_response(notifications, groups) |
117 response_headers = [('Content-Type', 'application/json; charset=utf-8'), | 117 response_headers = [('Content-Type', 'application/json; charset=utf-8'), |
118 ('ABP-Notification-Version', response['version'])] | 118 ('ABP-Notification-Version', response['version'])] |
119 response_body = json.dumps(response, ensure_ascii=False, indent=2, | 119 response_body = json.dumps(response, ensure_ascii=False, indent=2, |
120 separators=(',', ': '), | 120 separators=(',', ': '), |
121 sort_keys=True).encode('utf-8') | 121 sort_keys=True).encode('utf-8') |
122 start_response('200 OK', response_headers) | 122 start_response('200 OK', response_headers) |
123 return response_body | 123 return response_body |
OLD | NEW |