Starchik1 commited on
Commit
470023d
·
verified ·
1 Parent(s): b2baf4a

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +35 -0
main.py CHANGED
@@ -91,10 +91,34 @@ def proxy(path):
91
  # Check if response is HTML and filter content if needed
92
  content_type = resp.headers.get('Content-Type', '')
93
  if 'text/html' in content_type:
 
 
 
 
 
94
  # Parse HTML content
95
  html_content = resp.content.decode('utf-8', errors='ignore')
96
  soup = BeautifulSoup(html_content, 'html.parser')
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  # Filter out "Полная версия ETKA"
99
  for element in soup.find_all(string=re.compile('Полная версия ETKA')):
100
  # Replace the text with empty string
@@ -248,6 +272,17 @@ def proxy(path):
248
  # Add modal container for fullscreen image view
249
  modal_div = soup.new_tag('div')
250
  modal_div['id'] = 'imageModal'
 
 
 
 
 
 
 
 
 
 
 
251
  modal_div['class'] = 'modal'
252
  modal_div['style'] = 'display: none;'
253
 
 
91
  # Check if response is HTML and filter content if needed
92
  content_type = resp.headers.get('Content-Type', '')
93
  if 'text/html' in content_type:
94
+ # Create response headers
95
+ response_headers = {}
96
+ for key, value in resp.headers.items():
97
+ if key.lower() not in ['content-encoding', 'content-length', 'transfer-encoding']:
98
+ response_headers[key] = value
99
  # Parse HTML content
100
  html_content = resp.content.decode('utf-8', errors='ignore')
101
  soup = BeautifulSoup(html_content, 'html.parser')
102
 
103
+ # Ensure basic HTML structure exists
104
+ if not soup.html:
105
+ new_html = soup.new_tag('html')
106
+ soup.append(new_html)
107
+ soup.html.append(soup.new_tag('head'))
108
+ soup.html.append(soup.new_tag('body'))
109
+ # Move all top-level content into body
110
+ for element in list(soup.contents):
111
+ if element.name != 'html':
112
+ soup.body.append(element)
113
+ elif not soup.head:
114
+ soup.html.insert(0, soup.new_tag('head'))
115
+ elif not soup.body:
116
+ soup.html.append(soup.new_tag('body'))
117
+ # Move all content after head into body
118
+ for element in list(soup.html.contents):
119
+ if element.name != 'head' and element != soup.body:
120
+ soup.body.append(element)
121
+
122
  # Filter out "Полная версия ETKA"
123
  for element in soup.find_all(string=re.compile('Полная версия ETKA')):
124
  # Replace the text with empty string
 
272
  # Add modal container for fullscreen image view
273
  modal_div = soup.new_tag('div')
274
  modal_div['id'] = 'imageModal'
275
+
276
+ # Convert modified HTML back to string
277
+ modified_html = str(soup)
278
+
279
+ # Return modified content with preserved headers
280
+ return Response(
281
+ modified_html,
282
+ status=resp.status_code,
283
+ headers=response_headers,
284
+ mimetype='text/html'
285
+ )
286
  modal_div['class'] = 'modal'
287
  modal_div['style'] = 'display: none;'
288