-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
100 lines (74 loc) · 2.45 KB
/
server.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# Copyright (C) 2013 CeibalJAM
from flask import Flask
from flask import render_template
import os
import json
NEWS = "noticias"
images = "static/covers"
TAGS_CACHE = {}
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html', images=os.listdir(images))
def _getnoticia(n):
path = os.path.join(NEWS, n)
if os.path.exists(path):
try:
return json.load(open(path))
except ValueError:
return None
def _getnoticias():
_noticias = []
for x in os.listdir(NEWS):
if x.endswith(".json"):
noticia_d = _getnoticia(x)
noticia_d['name'] = x[:-5]
if noticia_d:
_noticias.append(noticia_d)
return _noticias
def _gen_tags_cache_for_tag(tag):
cache = []
for noticia in _getnoticias():
if tag in noticia['tags']:
del noticia['content']
del noticia['tags']
cache.append(noticia)
return cache
def _gen_tags_cache():
for noticia in _getnoticias():
for tag in noticia['tags']:
if not tag in TAGS_CACHE:
TAGS_CACHE[tag] = []
TAGS_CACHE[tag].append({'name': noticia['name'],
'title': noticia['title'],
'desc': noticia['desc']})
print TAGS_CACHE[tag]
@app.route('/noticias/')
def noticias():
return render_template("noticias.html", noticias=_getnoticias())
@app.route('/noticias/<noticia>')
def noticias_(noticia=None):
if noticia:
noticia_d = _getnoticia('%s.json' % noticia)
if noticia_d:
return render_template('noticia.html', noticia=noticia_d)
else:
return render_template('noticia404.html', noticia=noticia)
@app.route('/noticias/tags/')
def tags():
_gen_tags_cache()
return render_template("tags.html", tags=TAGS_CACHE.keys())
@app.route('/noticias/tags/<_tag>')
def tags_(_tag):
if not _tag in TAGS_CACHE:
cache = _gen_tags_cache_for_tag(_tag)
if cache != []:
TAGS_CACHE[_tag] = cache
if _tag in TAGS_CACHE and TAGS_CACHE[_tag] != []:
return render_template('tag.html', tag=_tag, noticias=TAGS_CACHE[_tag])
else:
return render_template('tag404.html', tag=_tag)
if __name__ == "__main__":
app.debug = True
app.run(host="0.0.0.0")