-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
128 lines (107 loc) · 3.18 KB
/
app.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import sys
from flask import Flask, request, Response
from crawler import Crawler
app = Flask(__name__)
c = Crawler()
@app.post('/category/')
def get_category():
request_data = request.get_json()
urls = request_data.get("links")
print(urls)
if urls is None:
return Response("You must pass a valid url", status=500)
output = []
for url in urls:
try:
content = c.start(url)
category = c.categorize(content)
split_cat = category.split(";")
formatted = [i.strip() for i in split_cat]
output_dict = {}
for i in formatted:
k_v = i.split(":")
# print(k_v)
key = k_v[0]
val = k_v[1].strip().strip('.')
output_dict.update({key: None if val == "None" else val})
# print(item)
output.append(output_dict)
except Exception as err:
print(err)
return {"response": f"{err}"}, 500
return {
"categories": output
}, 200
@app.post('/summary/')
def get_summary():
request_data = request.get_json()
url = request_data.get("link")
print(url)
if url is None:
return Response("You must pass a valid url", status=500)
try:
content = c.start(url)
# print(content)
summary = c.make_summary(content).strip()
except Exception as err:
print(err)
return {"response": f"{err}"}, 500
return {
"summary": summary
}, 200
@app.post('/rewrite/tv/')
def rewrite_tv():
request_data = request.get_json()
url = request_data.get("link")
print(url)
if url is None:
return Response("You must pass a valid url", status=500)
try:
content = c.start(url)
# print(content)
response = c.tv_rewrite(content).replace('\n', '')
except Exception as err:
print(err)
return {"response": f"{err}"}, 500
return {
"response": response
}, 200
@app.post('/rewrite/radio/')
def rewrite_radio():
request_data = request.get_json()
url = request_data.get("link")
print(url)
if url is None:
return Response("You must pass a valid url", status=500)
try:
content = c.start(url)
# print(content)
response = c.radio_rewrite(content).replace('\n', '')
# print("response:", response)
# print("len_response:", len(response))
except Exception as err:
print(err)
return {"response": f"{err}"}, 500
return {
"response": response
}, 200
@app.post('/rewrite/online/')
def rewrite_online():
request_data = request.get_json()
url = request_data.get("link")
print(url)
if url is None:
return Response("You must pass a valid url", status=500)
try:
content = c.start(url)
# print(content)
response = c.online_rewrite(content).replace('\n', '')
except Exception as err:
print(err)
return {"response": f"{err}"}, 500
return {
"response": response
}, 200
if __name__ == "__main__":
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)), debug=True)