-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_server.py
More file actions
62 lines (52 loc) · 2.02 KB
/
api_server.py
File metadata and controls
62 lines (52 loc) · 2.02 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
from flask import Flask, make_response
from flask_restx import Api, Resource
from dateutil.parser import parse as date_parse
import subprocess
import datetime
import os
import json
from pytz import timezone
app = Flask(__name__)
app.config["JSON_AS_ASCII"] = False
api = Api(app)
@api.route("/bob")
class Bob(Resource):
def get(self):
today = datetime.datetime.now(timezone('Asia/Seoul'))
today -= datetime.timedelta(days=today.weekday())
if not os.path.exists("datas/" + today.strftime("%m-%d") + ".json"):
subprocess.call(["python", "parser_date.py", today.strftime("%Y%m%d")])
print('called subprocess')
with open(
"datas/" + today.strftime("%m-%d") + ".json", "r", encoding="utf-8"
) as f:
result = json.loads(f.read())
result = json.dumps(result, ensure_ascii=False, indent=4)
res = make_response(result)
return res
@api.route("/bob/<date>")
class Bob_date(Resource):
def get(self, date):
today = date_parse(date)
temp_day = today - datetime.timedelta(days=today.weekday())
if not os.path.exists("datas/" + temp_day.strftime("%m-%d") + ".json"):
subprocess.call(["python", "parser_date.py", date])
with open(
"datas/" + temp_day.strftime("%m-%d") + ".json", "r", encoding="utf-8"
) as f:
res = ""
result = json.loads(f.read())
for meal in result["meals"]:
if meal["date"] == today.strftime("%Y-%m-%d"):
res = json.dumps(meal, ensure_ascii=False, indent=4)
if res == "":
res = make_response(
json.dumps({"error": "No data"}, ensure_ascii=False, indent=4)
)
f.close()
os.remove("datas/" + temp_day.strftime("%m-%d") + ".json")
else:
res = make_response(res)
return res
if __name__ == "__main__":
app.run(host="localhost", port=5000, debug=True)