diff --git a/i2p2www/__init__.py b/i2p2www/__init__.py index 24277843..7fc86431 100644 --- a/i2p2www/__init__.py +++ b/i2p2www/__init__.py @@ -6,6 +6,11 @@ import os.path import os import fileinput import codecs +from random import randint +try: + import json +except ImportError: + import simplejson as json TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'pages') @@ -14,6 +19,8 @@ STATIC_DIR = os.path.join(os.path.dirname(__file__), 'static') BLOG_DIR = os.path.join(os.path.dirname(__file__), 'blog') MEETINGS_DIR = os.path.join(os.path.dirname(__file__), 'meetings') +MIRRORS_FILE = os.path.join(TEMPLATE_DIR, 'downloads/mirrors') + app = application = Flask('i2p2www', template_folder=TEMPLATE_DIR, static_url_path='/_static', static_folder=STATIC_DIR) app.debug = bool(os.environ.get('APP_DEBUG', 'False')) babel = Babel(app) @@ -248,6 +255,26 @@ def meetings_show_rst(id): ################### # Download handlers +# Read in mirrors from file +def read_mirrors(): + file = open(MIRRORS_FILE, 'r') + dat = file.read() + file.close() + lines=dat.split('\n') + ret={} + for line in lines: + try: + obj=json.loads(line) + except ValueError: + continue + if 'protocol' not in obj: + continue + protocol=obj['protocol'] + if protocol not in ret: + ret[protocol]=[] + ret[protocol].append(obj) + return ret + # List of downloads @app.route('//download') def downloads_list(): @@ -257,16 +284,29 @@ def downloads_list(): # Specific file downloader @app.route('//download/') def downloads_select(file): - # TODO: implement if (file == 'debian'): return render_template('downloads/debian.html') - pass + mirrors=read_mirrors() + obj=[] + for protocol in mirrors.keys(): + a={} + a['name']=protocol + a['mirrors']=mirrors[protocol] + for mirror in a['mirrors']: + mirror['url']=mirror['url'] % file + obj.append(a) + return render_template('downloads/select.html', mirrors=obj, file=file) @app.route('/download//any/') -@app.route('/download///') +@app.route('/download///') def downloads_redirect(protocol, file, mirror=None): - # TODO: implement - pass + mirrors=read_mirrors() + if not protocol in mirrors: + abort(404) + mirrors=mirrors[protocol] + if mirror: + return redirect(mirrors[mirror]['url'] % file) + return redirect(mirrors[randint(0, len(mirrors) - 1)]['url'] % file) ##################### diff --git a/mirror.i2p2/mirrors b/i2p2www/pages/downloads/mirrors similarity index 53% rename from mirror.i2p2/mirrors rename to i2p2www/pages/downloads/mirrors index 5afeaf67..82ab9b31 100644 --- a/mirror.i2p2/mirrors +++ b/i2p2www/pages/downloads/mirrors @@ -1,2 +1,3 @@ +{"url": "http://i2p.googlecode.com/files/%s", "org": "Google Code", "org_url": "http://code.google.com", "protocol": "http", "country": "us"} {"url": "http://golden.mtveurope.org/~yang/i2p_mirror/%s", "org": "VServer.si", "org_url": "http://www.vserver.si", "protocol": "http", "country": "lu"} -{"url": "http://a.mirror.geti2p.net/releases/current/%s", "org": "welterde", "protocol": "http", "country": "de"} \ No newline at end of file +{"url": "http://a.mirror.geti2p.net/releases/current/%s", "org": "welterde", "protocol": "http", "country": "de"} diff --git a/i2p2www/pages/downloads/select.html b/i2p2www/pages/downloads/select.html new file mode 100644 index 00000000..3eaf46a9 --- /dev/null +++ b/i2p2www/pages/downloads/select.html @@ -0,0 +1,17 @@ +{% extends "global/layout.html" %} +{% block title %}Mirror selection{% endblock %} +{% block content %} +

Mirror selection

+

File: /{{ file }}

+{% for protocol in mirrors -%} +
+

{{ protocol.name | upper }}

+ +
+{%- endfor %} +{% endblock %} diff --git a/mirror.i2p2/mirror.god b/mirror.god.old similarity index 100% rename from mirror.i2p2/mirror.god rename to mirror.god.old diff --git a/mirror.i2p2/app.py b/mirror.i2p2/app.py deleted file mode 100644 index 8c26e237..00000000 --- a/mirror.i2p2/app.py +++ /dev/null @@ -1,80 +0,0 @@ -from flask import Flask, redirect, request, render_template, abort -from random import randint -from sys import argv -try: - import json -except ImportError: - import simplejson as json - - -# try to create an memcache client -if len(argv[3:]) > 0: - try: - try: - from cmemcache import Client - except ImportError: - from memcache import Client - client=Client(argv[3:]) - except ImportError: - client=None - -# create application -app=Flask(__name__) - -# extract domain -domain=argv[1] - -# extract port -port=int(argv[2]) - -def read_mirrors(): - file = open('mirrors', 'r') - dat = file.read() - file.close() - lines=dat.split('\n') - ret={} - for line in lines: - try: - obj=json.loads(line) - except ValueError: - pass - if 'protocol' not in obj: - continue - protocol=obj['protocol'] - if protocol not in ret: - ret[protocol]=[] - ret[protocol].append(obj) - return ret - - -@app.route('/') -def index(): - return redirect('http://www.%s/download' % domain) - -@app.route('/select/') -def select(f): - mirrors=read_mirrors() - obj=[] - for protocol in mirrors.keys(): - a={} - a['name']=protocol.upper() - a['mirrors']=mirrors[protocol] - for mirror in a['mirrors']: - mirror['url']=mirror['url'] % f - obj.append(a) - return render_template('select.html', mirrors=obj, file=f, domain=domain) - -@app.route('//') -def get(protocol, f): - mirrors=read_mirrors() - if not protocol in mirrors: - abort(404) - mirrors=mirrors[protocol] - return redirect(mirrors[randint(0, len(mirrors) - 1)]['url'] % f) - -@app.route('/') -def old_get(f): - return redirect('http://i2p.googlecode.com/files/%s' % f) - -if __name__ == '__main__': - app.run(port=port) diff --git a/mirror.i2p2/static/images/i2plogo.png b/mirror.i2p2/static/images/i2plogo.png deleted file mode 100644 index f7beff26..00000000 Binary files a/mirror.i2p2/static/images/i2plogo.png and /dev/null differ diff --git a/mirror.i2p2/static/style.css b/mirror.i2p2/static/style.css deleted file mode 100644 index e69de29b..00000000 diff --git a/mirror.i2p2/templates/select.html b/mirror.i2p2/templates/select.html deleted file mode 100644 index fc031ca1..00000000 --- a/mirror.i2p2/templates/select.html +++ /dev/null @@ -1,29 +0,0 @@ - - - - - I2P - Mirror selection - /{{ file }} - - - - -
-

Mirror selection

-

File: /{{ file }}

-
- {% for protocol in mirrors -%} -
-

{{ protocol.name }}

- -
- {%- endfor %} - - -