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

@ -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"}
{"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 %}

View File

@ -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/<path:f>')
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('/<protocol>/<path:f>')
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('/<f>')
def old_get(f):
return redirect('http://i2p.googlecode.com/files/%s' % f)
if __name__ == '__main__':
app.run(port=port)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.5 KiB

View File

@ -1,29 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<title>I2P - Mirror selection - /{{ file }}</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" type="text/css">
</head>
<body>
<div class="logo">
<a href="{{ domain }}" class="fade"><img src="{{ url_for('static', filename='images/i2plogo.png') }}" alt="I2P Logo" title="Invisible Internet Project (I2P)" /></a>
</div>
<div class="header">
<h1>Mirror selection</h1>
<h2>File: /{{ file }}</h2>
</div>
{% for protocol in mirrors -%}
<div class="protocol">
<h3>{{ protocol.name }}</h3>
<ul>
{% for mirror in protocol.mirrors -%}
<li><a href="{{ mirror.url }}">{{ mirror.url }}</a> {% if mirror.org_url %}<a href="{{ mirror.org_url }}">{% endif %}{{ mirror.org }}{% if mirror.org_url %}</a>{% endif %}</li>
{% endfor %}
</ul>
</div>
{%- endfor %}
</body>
</html>