OLD | NEW |
(Empty) | |
| 1 # coding: utf-8 |
| 2 |
| 3 # This file is part of the Adblock Plus web scripts, |
| 4 # Copyright (C) 2006-2014 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 re |
| 19 import time |
| 20 import posixpath |
| 21 import urlparse |
| 22 import multiprocessing |
| 23 from ConfigParser import SafeConfigParser |
| 24 from sitescripts.web import url_handler |
| 25 from sitescripts.extensions.utils import getDownloadLinks |
| 26 |
| 27 links = {} |
| 28 |
| 29 @url_handler('/latest/') |
| 30 def handle_request(environ, start_response): |
| 31 update_links() |
| 32 |
| 33 request = urlparse.urlparse(environ.get('REQUEST_URI', '')) |
| 34 basename = posixpath.splitext(posixpath.basename(request.path))[0] |
| 35 if basename in links: |
| 36 start_response('302 Found', [('Location', links[basename].encode("utf-8"))]) |
| 37 else: |
| 38 start_response('404 Not Found', []) |
| 39 return [] |
| 40 |
| 41 UPDATE_INTERVAL = 10 * 60 # 10 minutes |
| 42 update_queue = multiprocessing.Queue() |
| 43 last_update = -float('Inf') |
| 44 |
| 45 def update_links(): |
| 46 global links, last_update |
| 47 while not update_queue.empty(): |
| 48 links = update_queue.get() |
| 49 |
| 50 now = time.time() |
| 51 if now - last_update > UPDATE_INTERVAL: |
| 52 last_update = now |
| 53 |
| 54 process = multiprocessing.Process(target=_update_links, args=(update_queue,)
) |
| 55 process.daemon = True |
| 56 process.start() |
| 57 |
| 58 def _update_links(queue): |
| 59 parser = SafeConfigParser() |
| 60 getDownloadLinks(parser) |
| 61 result = {} |
| 62 for section in parser.sections(): |
| 63 result[section] = parser.get(section, "downloadURL") |
| 64 queue.put(result) |
| 65 |
| 66 update_links() |
OLD | NEW |