OLD | NEW |
(Empty) | |
| 1 # coding: utf-8 |
| 2 |
| 3 # This file is part of the Adblock Plus web scripts, |
| 4 # Copyright (C) 2006-2015 Eyeo GmbH |
| 5 # |
| 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 |
| 8 # published by the Free Software Foundation. |
| 9 # |
| 10 # Adblock Plus is distributed in the hope that it will be useful, |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 # GNU General Public License for more details. |
| 14 # |
| 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/>. |
| 17 |
| 18 import json |
| 19 import random |
| 20 import time |
| 21 from urlparse import parse_qs |
| 22 |
| 23 from sitescripts.notifications.parser import load_notifications |
| 24 from sitescripts.web import url_handler |
| 25 |
| 26 def _assign_groups(notifications): |
| 27 groups = [] |
| 28 selection = random.random() |
| 29 base = 0 |
| 30 for notification in filter(lambda notification: "variants" in notification, |
| 31 notifications): |
| 32 group = {"id": notification["id"], "variant": 0} |
| 33 groups.append(group) |
| 34 for i, variant in enumerate(notification["variants"]): |
| 35 sample_size = variant["sample"] |
| 36 if (group["variant"] == 0 and sample_size > 0 and selection >= base and |
| 37 selection <= sample_size + base): |
| 38 group["variant"] = i + 1 |
| 39 base += sample_size |
| 40 return groups |
| 41 |
| 42 def _create_response(notifications, groups): |
| 43 response = { |
| 44 "version": time.strftime("%Y%m%d%H%M", time.gmtime()) |
| 45 } |
| 46 for group in groups: |
| 47 group_id = group["id"] |
| 48 variant = group["variant"] |
| 49 response["version"] += "-%s/%s" % (group_id, variant) |
| 50 if variant > 0: |
| 51 notification = next(notification for notification in notifications |
| 52 if notification["id"] == group_id) |
| 53 notification = notification.copy() |
| 54 notification.update(notification["variants"][variant - 1]) |
| 55 for key_to_remove in ("sample", "variants"): |
| 56 notification.pop(key_to_remove, None) |
| 57 response["notifications"] = [notification] |
| 58 if not "notifications" in response: |
| 59 response["notifications"] = \ |
| 60 filter(lambda notification: not "variants" in notification, notifications) |
| 61 return json.dumps(response, ensure_ascii=False, indent=2, |
| 62 separators=(",", ": "), sort_keys=True) |
| 63 |
| 64 @url_handler("/notification.json") |
| 65 def notification(environ, start_response): |
| 66 params = parse_qs(environ.get("QUERY_STRING", "")) |
| 67 version = params.get("lastVersion", [""])[0] |
| 68 current_groups = dict(component.split("/") |
| 69 for component in version.split("-")[1:] |
| 70 if "/" in component) |
| 71 notifications = load_notifications() |
| 72 groups = [] |
| 73 for notification in notifications: |
| 74 if "variants" in notification: |
| 75 group_id = notification["id"] |
| 76 if group_id in current_groups: |
| 77 groups.append({"id": group_id, |
| 78 "variant": int(current_groups[group_id])}) |
| 79 if not groups: |
| 80 groups = _assign_groups(notifications) |
| 81 response = _create_response(notifications, groups) |
| 82 start_response("200 OK", [("Content-Type", "application/json")]) |
| 83 return response.encode("utf-8") |
OLD | NEW |