-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
160 lines (138 loc) · 4.13 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import os
import re
import requests as req
from flask import Flask, request, jsonify,render_template
from flask_cors import CORS
from bs4 import BeautifulSoup
from art import *
import markdownify
app = Flask(__name__, template_folder='templates', static_folder='assets')
CORS(app)
def convertResponseToText(response):
soup = BeautifulSoup(response.text, 'html.parser')
# get title
title = soup.title.string.strip()
links = []
# get text from website, but keep newline
for a in soup.find_all('a'):
if a.get("href") != None and a.get_text().strip() != "" and a.get("href").strip() != "" and not a.get("href").startswith("#"):
links.append({
"href": a.get("href"),
"text": a.get_text().strip()
})
# a.replace_with("")
forms = []
for form in soup.find_all('form'):
inputs = []
for input in form.find_all('input'):
inputs.append({
"type": input.get("type"),
"name": input.get("name"),
"value": input.get("value"),
"required": input.get("required") == "required"
})
forms.append({
"action": form.get("action"),
"method": form.get("method") if form.get("method") != None else "GET",
"inputs": inputs
})
form.replace_with(" ")
# remove a and nav tag
for nav in soup.find_all('nav'):
nav.decompose()
for header in soup.find_all('header'):
header.decompose()
for footer in soup.find_all('footer'):
footer.decompose()
for a in soup.find_all('a'):
# if child not div
if a.find("div") == None:
a.decompose()
text = soup.get_text()
# # replace newline with <br> tag
# text = text.replace("\n", "<br>")
titleArt=text2art(title)
# titleArt = titleArt.replace("\n", "<br>")
url = response.url
m = re.search('https?://([A-Za-z_0-9.-]+).*', url)
if m:
host = m.group(1).replace("www.", "")
else:
host = url
text = markdownify.markdownify(text, heading_style="ATX")
text = text.replace("\n\n\n", "")
return {
"url": url,
"host": host,
"title" : title,
"titleArt" : titleArt,
"text": text,
"links": links,
"forms": forms
}
def requestUrl(url):
# if url does not start with http
if not url.startswith("http"):
url = "http://" + url
# request to url and convert to text
try:
response = req.get(url)
return convertResponseToText(response)
except Exception as e:
return {
"error": str(e)+" "+str(url)
}
@app.route('/')
def index():
# get sc query
sc = request.args.get("sc")
data = None
error = None
if sc is not None:
# convert sc to text
res = requestUrl(sc)
if "error" in res:
error = res["error"]
else:
data = res
return render_template('index.html',data=data,error=error)
@app.route('/open-link')
def openLink():
url = request.args.get("url")
res = requestUrl(url)
if "error" in res:
return jsonify({
"error": res["error"]
}), 500
return jsonify(res)
@app.route('/submit-form', methods=['POST'])
def submitForm():
target = request.args.get("target")
data = request.form.to_dict()
print(data)
# action = data["action"]
method = data["method"]
del data["action"]
del data["method"]
if method == "GET":
response = req.get(target, params=data)
else:
response = req.post(target, data=data)
if response.status_code != 200:
return jsonify({
"error": "response status code is not 200"
}), 500
else:
res = convertResponseToText(response)
if "error" in res:
return jsonify({
"error": res["error"]
}), 500
return jsonify(res)
if __name__ == '__main__':
config = {
'host': '0.0.0.0',
'port': os.getenv("PORT", default=5001),
'debug': True
}
app.run(**config)