-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.py
86 lines (64 loc) · 2.22 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from flask import Flask, render_template, Response
from flask.ext.bootstrap import Bootstrap
import re
import requests
import logging
import functools
log = logging.getLogger('freepto-web')
from discovery import lang_dirs
app = Flask(__name__)
app.config['DEBUG'] = True
Bootstrap(app)
@app.route('/')
def index():
return render('it', 'index')
@app.route('/<lang>/')
def page_index(lang):
return render(lang, 'index')
@app.route('/<lang>/<title>/')
def page(lang, title):
return render(lang, title)
@app.route('/.htaccess')
def htaccess():
DEFAULT = 'en'
return Response(
render_template('htaccess.html',
languages=[(l, l) for l in lang_dirs if l != DEFAULT],
default_dir=DEFAULT
), content_type='application/octet-stream')
def render(lang, title):
template = "%s/%s.html" % (lang, title)
return render_template(template)
def memoize(obj):
'''decorator to memoize things'''
cache = obj.cache = {}
@functools.wraps(obj)
def memoizer(*args, **kwargs):
key = str(args) + str(kwargs)
if key not in cache:
cache[key] = obj(*args, **kwargs)
return cache[key]
return memoizer
@memoize
def get_images_data():
log.info('getting images...')
base_url = 'http://download.freepto.mx/latest/'
latest = requests.get(base_url)
if latest.status_code != 200:
raise Exception('Can\'t download http://download.freepto.mx/latest/')
latest_text = latest.text.replace('\n', '')
locales = re.findall('(freepto-[\-0-9\.]*_[a-zA-Z]*)', latest_text)
images_data = {}
for locale in locales:
sha512_url = '%s/%s/%s.sha512sum.txt' % (base_url, locale, locale)
sha512 = requests.get(sha512_url)
if sha512.status_code != 200:
raise Exception('Can\'t download %s' % sha512_url)
images_data[locale] = {
'http_download': '%s%s/%s.img' % (base_url, locale, locale),
'torrent_download': '%s%s/%s.torrent' % (base_url, locale, locale),
'sha512': sha512.text.split()[0],
'sha512sig': sha512_url + '.asc'
}
return images_data
app.jinja_env.globals.update(get_images_data=get_images_data)