-
Notifications
You must be signed in to change notification settings - Fork 9
/
quotes.py
63 lines (50 loc) · 1.75 KB
/
quotes.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
from app import app
from commons import *
import random
@app.route('/quotes')
def show_quotes():
return render_template_g('quotes.html.jinja',
page_title="语录",
)
@stale_cache(ttr=10, ttl=900)
def get_quotes():return get_quotes_raw()
@stale_cache(ttr=30, ttl=900)
def get_quotes_slow():return get_quotes_raw()
def get_quotes_raw():
quotes = aql('''
for i in entities
filter i.type=='famous_quotes' or i.type=='famous_quotes_v2'
sort i.t_c desc
let user = (for u in users filter u.uid==i.uid return u)[0]
return merge(i, {user})
//for j in i.doc
//return {quote:j[0], quoting:j[1], user, t_u:(i.t_u or i.t_c)}
''', silent=True)
q = []
for i in quotes:
if i['type']=='famous_quotes':
if isinstance(i['doc'], list):
for j in i['doc']:
if len(j)>=2:
q.append(dict(
quote=j[0],
quoting=j[1],
user=i['user'],
t_u= i['t_e'] if 't_e' in i else i['t_c'],
**{'_key':i['_key']},
))
elif i['type']=='famous_quotes_v2':
if 'quoting' in i['doc'] and 'quotes' in i['doc']:
if isinstance(i['doc']['quotes'], list):
for j in i['doc']['quotes']:
q.append(dict(
quote=j,
quoting=i['doc']['quoting'],
user=i['user'],
t_u= i['t_e'] if 't_e' in i else i['t_c'],
**{'_key':i['_key']},
))
return q
def get_quote():
quotes = get_quotes_slow()
return random.choice(quotes)