|
from flask import Flask, request, Response |
|
import requests |
|
import logging |
|
from urllib.parse import urljoin |
|
from urllib.parse import quote |
|
from bs4 import BeautifulSoup |
|
import re |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
TARGET_BASE_URL = "https://superetka.com" |
|
|
|
@app.route('/proxy_image') |
|
def proxy_image(): |
|
from urllib.parse import unquote |
|
image_url = unquote(request.args.get('url')) |
|
if not image_url: |
|
return 'No URL provided', 400 |
|
|
|
try: |
|
|
|
headers = {key: value for key, value in request.headers if key.lower() != 'host'} |
|
headers['Referer'] = TARGET_BASE_URL |
|
headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36' |
|
|
|
|
|
resp = requests.get( |
|
image_url, |
|
headers=headers, |
|
cookies=request.cookies, |
|
timeout=30, |
|
allow_redirects=True |
|
) |
|
|
|
|
|
if resp.status_code != 200: |
|
logger.error(f"Error response from target: {resp.status_code}") |
|
return f"Error: {resp.status_code}", resp.status_code |
|
|
|
return Response(resp.content, mimetype=resp.headers.get('Content-Type', 'image/jpeg')) |
|
except Exception as e: |
|
logger.error(f"Error proxying image: {str(e)}") |
|
return str(e), 500 |
|
|
|
@app.route('/', defaults={'path': ''}, methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS']) |
|
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS']) |
|
def proxy(path): |
|
|
|
if path and path.strip(): |
|
|
|
url = f"{TARGET_BASE_URL}/{path}" |
|
else: |
|
|
|
url = f"{TARGET_BASE_URL}/etka/wap.php" |
|
|
|
|
|
if request.query_string: |
|
url = f"{url}?{request.query_string.decode('utf-8')}" |
|
|
|
|
|
logger.info(f"Constructed URL: {url}") |
|
|
|
|
|
logger.info(f"Received request: {request.method} {request.url}") |
|
logger.info(f"Forwarding to: {url}") |
|
|
|
|
|
headers = {key: value for key, value in request.headers if key.lower() != 'host'} |
|
|
|
try: |
|
|
|
|
|
resp = requests.request( |
|
method=request.method, |
|
url=url, |
|
headers=headers, |
|
data=request.get_data(), |
|
cookies=request.cookies, |
|
allow_redirects=False, |
|
timeout=30 |
|
) |
|
|
|
|
|
logger.info(f"Received response from target: {resp.status_code}") |
|
|
|
|
|
content_type = resp.headers.get('Content-Type', '') |
|
if 'text/html' in content_type: |
|
|
|
html_content = resp.content.decode('utf-8', errors='ignore') |
|
soup = BeautifulSoup(html_content, 'html.parser') |
|
|
|
|
|
for element in soup.find_all(string=re.compile('Полная версия ETKA')): |
|
|
|
element.replace_with('') |
|
|
|
|
|
for element in soup.find_all(string=re.compile('README', re.IGNORECASE)): |
|
element.replace_with('') |
|
|
|
|
|
|
|
part_number_links = soup.select('td:nth-child(2) a') |
|
for link in part_number_links: |
|
|
|
part_number = link.text.strip() |
|
|
|
if re.match(r'^[A-Z0-9 ]+$', part_number): |
|
|
|
google_search_url = f"https://www.google.com/search?q={part_number} avto.pro" |
|
|
|
link['href'] = google_search_url |
|
|
|
|
|
for img in soup.find_all('img'): |
|
|
|
button = soup.new_tag('button') |
|
img_src = img['src'] |
|
if not img_src.startswith('http'): |
|
img_src = urljoin(TARGET_BASE_URL, img_src) |
|
from urllib.parse import quote |
|
proxy_url = f"/proxy_image?url={quote(img_src)}" |
|
button['onclick'] = f'window.open("{proxy_url}", "_blank")' |
|
button['style'] = 'margin: 5px;' |
|
button.string = 'Показать изображение' |
|
|
|
img.replace_with(button) |
|
|
|
|
|
response = Response( |
|
soup.encode(), |
|
status=resp.status_code |
|
) |
|
else: |
|
|
|
response = Response( |
|
resp.content, |
|
status=resp.status_code |
|
) |
|
|
|
|
|
for key, value in resp.headers.items(): |
|
if key.lower() not in ('transfer-encoding', 'content-encoding', 'content-length'): |
|
response.headers[key] = value |
|
|
|
|
|
for cookie in resp.cookies: |
|
response.set_cookie( |
|
key=cookie.name, |
|
value=cookie.value, |
|
|
|
path=cookie.path, |
|
expires=cookie.expires, |
|
secure=cookie.secure, |
|
httponly=cookie.httponly |
|
) |
|
|
|
return response |
|
|
|
except requests.RequestException as e: |
|
logger.error(f"Error forwarding request: {str(e)}") |
|
return Response(f"Error forwarding request: {str(e)}", status=500) |
|
|
|
if __name__ == '__main__': |
|
app.run(host='0.0.0.0', port=5000, debug=True) |