-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.py
78 lines (60 loc) · 1.85 KB
/
web.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
# -*- coding: UTF-8 -*-
import os
from flask import Flask, render_template
import glob
import json
import random
urls = set()
ban_words = ['爪', '舌']
dish_list = []
def is_baned(ingredient_list):
for ingredient in ingredient_list:
for word in ban_words:
if word in ingredient.encode('utf8'):
return True
return False
def download_img(img_url, download_path):
import requests
r = requests.get(img_url)
with open(download_path, 'wb') as f:
f.write(r.content)
def path_name_ext(path):
(dir_path, tempfilename) = os.path.split(path)
(file_name, extension) = os.path.splitext(tempfilename)
return dir_path, file_name, extension
def exists(path):
return os.path.exists(path)
for file in glob.glob('recipe/*.json')[:]:
with open(file, 'rb') as f:
dish_map = json.load(f)
for dish in dish_map:
if dish['url'] in urls:
continue
if is_baned(dish['ingredient']):
continue
urls.add(dish['url'])
dish_list.append(dish)
app = Flask(__name__)
@app.route('/')
def daily_order():
selected = []
for i in range(7):
dish = dish_list[random.randint(1, len(dish_list))]
if '?' in dish['pic']:
dish['pic'] = dish['pic'].split('?')[0]
if '@' in dish['pic']:
dish['pic'] = dish['pic'].split('@')[0]
path, name, ext = path_name_ext(dish['pic'])
pic_path = './static/' + name + ext
if not exists(pic_path):
download_img(dish['pic'], pic_path)
dish['pic'] = pic_path
dish['ingredient'] = ','.join(dish['ingredient'])
selected.append(dish)
return render_template('home.html', dish_list=selected)
if __name__ == '__main__':
app.run(
host='0.0.0.0',
port=80,
debug=False
)