Update main.py
Browse files
main.py
CHANGED
@@ -2,6 +2,7 @@ from flask import Flask, request, Response
|
|
2 |
import requests
|
3 |
import logging
|
4 |
from urllib.parse import urljoin
|
|
|
5 |
from bs4 import BeautifulSoup
|
6 |
import re
|
7 |
|
@@ -16,7 +17,8 @@ TARGET_BASE_URL = "https://superetka.com"
|
|
16 |
|
17 |
@app.route('/proxy_image')
|
18 |
def proxy_image():
|
19 |
-
|
|
|
20 |
if not image_url:
|
21 |
return 'No URL provided', 400
|
22 |
|
@@ -122,7 +124,8 @@ def proxy(path):
|
|
122 |
img_src = img['src']
|
123 |
if not img_src.startswith('http'):
|
124 |
img_src = urljoin(TARGET_BASE_URL, img_src)
|
125 |
-
|
|
|
126 |
button['onclick'] = f'window.open("{proxy_url}", "_blank")'
|
127 |
button['style'] = 'margin: 5px;'
|
128 |
button.string = 'Показать изображение'
|
@@ -146,6 +149,18 @@ def proxy(path):
|
|
146 |
if key.lower() not in ('transfer-encoding', 'content-encoding', 'content-length'):
|
147 |
response.headers[key] = value
|
148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
return response
|
150 |
|
151 |
except requests.RequestException as e:
|
|
|
2 |
import requests
|
3 |
import logging
|
4 |
from urllib.parse import urljoin
|
5 |
+
from urllib.parse import quote
|
6 |
from bs4 import BeautifulSoup
|
7 |
import re
|
8 |
|
|
|
17 |
|
18 |
@app.route('/proxy_image')
|
19 |
def proxy_image():
|
20 |
+
from urllib.parse import unquote
|
21 |
+
image_url = unquote(request.args.get('url'))
|
22 |
if not image_url:
|
23 |
return 'No URL provided', 400
|
24 |
|
|
|
124 |
img_src = img['src']
|
125 |
if not img_src.startswith('http'):
|
126 |
img_src = urljoin(TARGET_BASE_URL, img_src)
|
127 |
+
from urllib.parse import quote
|
128 |
+
proxy_url = f"/proxy_image?url={quote(img_src)}"
|
129 |
button['onclick'] = f'window.open("{proxy_url}", "_blank")'
|
130 |
button['style'] = 'margin: 5px;'
|
131 |
button.string = 'Показать изображение'
|
|
|
149 |
if key.lower() not in ('transfer-encoding', 'content-encoding', 'content-length'):
|
150 |
response.headers[key] = value
|
151 |
|
152 |
+
# Copy cookies from target response
|
153 |
+
for cookie in resp.cookies:
|
154 |
+
response.set_cookie(
|
155 |
+
key=cookie.name,
|
156 |
+
value=cookie.value,
|
157 |
+
# domain=cookie.domain,
|
158 |
+
path=cookie.path,
|
159 |
+
expires=cookie.expires,
|
160 |
+
secure=cookie.secure,
|
161 |
+
httponly=cookie.httponly
|
162 |
+
)
|
163 |
+
|
164 |
return response
|
165 |
|
166 |
except requests.RequestException as e:
|