Files
i2p.www/i2p2www/downloads.py

82 lines
2.3 KiB
Python
Raw Normal View History

from flask import abort, redirect, render_template
2012-12-19 12:52:05 +00:00
try:
import json
except ImportError:
import simplejson as json
from random import randint
from i2p2www import CURRENT_I2P_VERSION, MIRRORS_FILE
DEFAULT_MIRROR = {
'protocol': 'https',
'domain': 'i2p.googlecode.com',
'org': 'Google Code',
}
2012-12-19 12:52:05 +00:00
###################
# 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 or 'domain' not in obj or 'path' not in obj:
2012-12-19 12:52:05 +00:00
continue
protocol=obj['protocol']
domain=obj['domain']
path=obj['path']
obj['url']='%s://%s%s' % (protocol, domain, path)
2012-12-19 12:52:05 +00:00
if protocol not in ret:
ret[protocol]={}
ret[protocol][domain]=obj
2012-12-19 12:52:05 +00:00
return ret
# List of downloads
def downloads_list():
# TODO: read mirror list or list of available files
return render_template('downloads/list.html', def_mirror=DEFAULT_MIRROR)
2012-12-19 12:52:05 +00:00
2013-07-18 04:40:31 +00:00
# Debian-specific page
def downloads_debian():
return render_template('downloads/debian.html')
2012-12-19 12:52:05 +00:00
# Specific file downloader
def downloads_select(version, file):
2012-12-19 12:52:05 +00:00
mirrors=read_mirrors()
obj=[]
for protocol in mirrors.keys():
a={}
a['name']=protocol
a['mirrors']=mirrors[protocol]
obj.append(a)
return render_template('downloads/select.html', mirrors=obj, version=version, file=file)
2012-12-19 12:52:05 +00:00
def downloads_redirect(version, protocol, domain, file):
2012-12-19 12:52:05 +00:00
mirrors=read_mirrors()
if not protocol in mirrors:
abort(404)
mirrors=mirrors[protocol]
data = {
'version': version,
2012-12-19 12:52:05 +00:00
'file': file,
}
if domain:
if not domain in mirrors:
abort(404)
return render_template('downloads/redirect.html',
version=version, protocol=protocol, domain=domain, file=file,
url=mirrors[domain]['url'] % data)
2013-08-30 13:27:39 +00:00
randomain = mirrors.keys()[randint(0, len(mirrors) - 1)]
return render_template('downloads/redirect.html',
version=version, protocol=protocol, domain=domain, file=file,
url=mirrors[randomain]['url'] % data)