diff --git a/i2p2www/__init__.py b/i2p2www/__init__.py index 20477fb6..72775575 100644 --- a/i2p2www/__init__.py +++ b/i2p2www/__init__.py @@ -4,11 +4,6 @@ from werkzeug.routing import BaseConverter from docutils.core import publish_parts import os.path import os -from random import randint -try: - import json -except ImportError: - import simplejson as json from helpers import LazyView, Pagination @@ -79,6 +74,11 @@ url('//meetings/.log', 'meetings.views.meetings_show_log') url('//meetings/.rst', 'meetings.views.meetings_show_rst') url('//feed/meetings/atom', 'meetings.views.meetings_atom') +url('//download', 'downloads.downloads_list') +url('//download/', 'downloads.downloads_select') +url('/download//any/', 'downloads.downloads_redirect', defaults={'mirror': None}) +url('/download///', 'downloads.downloads_redirect') + url('/meeting', 'legacy.legacy_meeting') url('/meeting.html', 'legacy.legacy_meeting') url('/status---', 'legacy.legacy_status') @@ -276,70 +276,5 @@ def page_not_found(error): def server_error(error): return render_template('global/error_500.html'), 500 - -################### -# 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(): - # TODO: read mirror list or list of available files - return render_template('downloads/list.html') - -# Specific file downloader -@app.route('//download/') -def downloads_select(file): - if (file == 'debian'): - return render_template('downloads/debian.html') - mirrors=read_mirrors() - data = { - 'version': CURRENT_I2P_VERSION, - 'file': file, - } - obj=[] - for protocol in mirrors.keys(): - a={} - a['name']=protocol - a['mirrors']=mirrors[protocol] - for mirror in a['mirrors']: - mirror['url']=mirror['url'] % data - obj.append(a) - return render_template('downloads/select.html', mirrors=obj, file=file) - -@app.route('/download//any/', defaults={'mirror': None}) -@app.route('/download///') -def downloads_redirect(protocol, file, mirror): - mirrors=read_mirrors() - if not protocol in mirrors: - abort(404) - mirrors=mirrors[protocol] - data = { - 'version': CURRENT_I2P_VERSION, - 'file': file, - } - if mirror: - return redirect(mirrors[mirror]['url'] % data) - return redirect(mirrors[randint(0, len(mirrors) - 1)]['url'] % data) - if __name__ == '__main__': app.run(debug=True) diff --git a/i2p2www/downloads.py b/i2p2www/downloads.py new file mode 100644 index 00000000..a9b32592 --- /dev/null +++ b/i2p2www/downloads.py @@ -0,0 +1,69 @@ +from flask import redirect, render_template +try: + import json +except ImportError: + import simplejson as json +from random import randint + +from i2p2www import CURRENT_I2P_VERSION, MIRRORS_FILE + + +################### +# 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 +def downloads_list(): + # TODO: read mirror list or list of available files + return render_template('downloads/list.html') + +# Specific file downloader +def downloads_select(file): + if (file == 'debian'): + return render_template('downloads/debian.html') + mirrors=read_mirrors() + data = { + 'version': CURRENT_I2P_VERSION, + 'file': file, + } + obj=[] + for protocol in mirrors.keys(): + a={} + a['name']=protocol + a['mirrors']=mirrors[protocol] + for mirror in a['mirrors']: + mirror['url']=mirror['url'] % data + obj.append(a) + return render_template('downloads/select.html', mirrors=obj, file=file) + +def downloads_redirect(protocol, file, mirror): + mirrors=read_mirrors() + if not protocol in mirrors: + abort(404) + mirrors=mirrors[protocol] + data = { + 'version': CURRENT_I2P_VERSION, + 'file': file, + } + if mirror: + return redirect(mirrors[mirror]['url'] % data) + return redirect(mirrors[randint(0, len(mirrors) - 1)]['url'] % data)