Migrated mirroring code into i2p2www

This commit is contained in:
str4d
2012-12-05 01:21:34 +00:00
parent 8b2a381288
commit bedc34b009
8 changed files with 64 additions and 115 deletions

View File

@@ -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('/<string:lang>/download')
def downloads_list():
@@ -257,16 +284,29 @@ def downloads_list():
# Specific file downloader
@app.route('/<string:lang>/download/<path:file>')
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/<string:protocol>/any/<path:file>')
@app.route('/download/<string:protocol>/<string:mirror>/<path:file>')
@app.route('/download/<string:protocol>/<int:mirror>/<path:file>')
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)
#####################

View File

@@ -0,0 +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"}

View File

@@ -0,0 +1,17 @@
{% extends "global/layout.html" %}
{% block title %}Mirror selection{% endblock %}
{% block content %}
<h1>Mirror selection</h1>
<h2>File: /{{ file }}</h2>
{% for protocol in mirrors -%}
<div class="protocol">
<h3>{{ protocol.name | upper }}</h3>
<ul>
<li><a href="{{ url_for('downloads_redirect', protocol=protocol.name, file=file) }}">Any mirror</a></li>
{% for mirror in protocol.mirrors -%}
<li><img src="{{ url_for('static', filename='images/'+mirror.country+'.png') }}" />&nbsp;{% if mirror.org_url %}<a href="{{ mirror.org_url }}">{% endif %}{{ mirror.org }}{% if mirror.org_url %}</a>{% endif %} <a href="{{ url_for('downloads_redirect', protocol=protocol.name, file=file, mirror=loop.index-1) }}">[Download]</a></li>
{%- endfor %}
</ul>
</div>
{%- endfor %}
{% endblock %}