-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
105 lines (97 loc) · 3.03 KB
/
main.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
101
102
103
104
105
import os
import json
from flask import Flask, Response, request as _request
from dateparser import parse
from dateparser.search import search_dates
app = Flask(__name__)
@app.route("/")
def parse_date_route():
try:
try:
date = _request.args["date"]
except KeyError:
return Response(
json.dumps(
{"message": "date query parameter not found", "success": False}
),
mimetype="application/json",
status=400,
)
try:
timestamp = parse(date).timestamp()
except:
return Response(
json.dumps(
{
"message": "Parser was not able to parse the date",
"success": False,
}
),
mimetype="application/json",
status=400,
)
return Response(
json.dumps({"message": timestamp, "success": True}),
mimetype="application/json",
status=200,
)
except Exception as e:
print(e)
return Response(
json.dumps({"message": "Internal Server Error", "success": False}),
mimetype="application/json",
status=500,
)
@app.route("/fromstr")
def parse_from_string_route():
try:
try:
date = _request.args["message"]
except KeyError:
return Response(
json.dumps(
{"message": "Time not supplied", "success": False}
),
mimetype="application/json",
status=400,
)
try:
timestamp = search_dates(date, settings={'PREFER_DATES_FROM': 'future'})
except:
return Response(
json.dumps(
{
"message": "Parser was not able to parse the date",
"success": False,
}
),
mimetype="application/json",
status=400,
)
if len(timestamp) <= 0 or timestamp is None:
return Response(
json.dumps(
{
"message": "Unable to find date in the message",
"success": False,
}
),
mimetype="application/json",
status=400,
)
return Response(
json.dumps({"message": (timestamp[0])[1].timestamp(),"readable_time": (timestamp[0])[0] ,"success": True}),
mimetype="application/json",
status=200,
)
except Exception as e:
print(e)
return Response(
json.dumps({"message": "Internal Server Error", "success": False}),
mimetype="application/json",
status=500,
)
if __name__ == "__main__":
app.run(
host=os.getenv("HOST", "0.0.0.0"), port=os.getenv("PORT", 8000),
)