-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
78 lines (60 loc) · 1.56 KB
/
app.py
File metadata and controls
78 lines (60 loc) · 1.56 KB
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
import aiohttp
from functools import reduce
from sanic import Sanic
from sanic.response import json
from sanic_cors import cross_origin
import api
import search
from lib import get_common_tags
app = Sanic()
def tag_factory(x):
if not x[1]:
return x[0]
return x[0] + x[1]
def item_factory(item):
# code = search.get_code(item['code'])
# tags = [tag_factory(x) for x in code]
tags = list(
search.get_item_search_tags(item)
)
return {
**item,
'tags': tags,
}
@app.route('/search/tags')
@cross_origin(app)
async def get_search_tags(request):
data = await api.get_data()
tags = [search.get_item_search_tags(x) for x in data]
tags = reduce(lambda tags, x: tags | x, tags, set())
tags = sorted(tags)
return json({
'data': tags,
})
@app.route('/items')
@cross_origin(app)
async def get_items(request):
data = await api.get_data()
data = [item_factory(x) for x in data]
return json({
'data': data,
})
@app.route('/tags/<tag>/children')
@cross_origin(app)
async def get_tag_children(request, tag):
tag = tag.lower()
data = await api.get_data()
items = []
for item in data:
tags = search.get_code(item['code'])
if tags[0][0].lower() == tag:
items.append(item)
children = get_common_tags(items)
children = [x for x in children if x.lower() != tag]
return json({
'data': children,
})
if __name__ == '__main__':
import os
port = os.environ.get('PORT', 5000)
app.run(host='0.0.0.0', port=port)