pay / main.py
Starchik1's picture
Update main.py
cea95e3 verified
raw
history blame
6.76 kB
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
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = Flask(__name__)
# Target URL base
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:
# Get headers from the incoming request
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'
# Forward the request to get the image
resp = requests.get(
image_url,
headers=headers,
cookies=request.cookies,
timeout=30,
allow_redirects=True
)
# Check response status
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):
# Construct target URL with path and query parameters
if path and path.strip():
# If path is provided, use it
url = f"{TARGET_BASE_URL}/{path}"
else:
# Default to wap.php if no path is provided
url = f"{TARGET_BASE_URL}/etka/wap.php"
# If there are query parameters, append them to the URL
if request.query_string:
url = f"{url}?{request.query_string.decode('utf-8')}"
# Log the constructed URL
logger.info(f"Constructed URL: {url}")
# Log the request
logger.info(f"Received request: {request.method} {request.url}")
logger.info(f"Forwarding to: {url}")
# Get headers from the incoming request
headers = {key: value for key, value in request.headers if key.lower() != 'host'}
try:
# Forward the request to the target server
# Don't pass params separately as they're already in the URL
resp = requests.request(
method=request.method,
url=url,
headers=headers,
data=request.get_data(),
cookies=request.cookies,
allow_redirects=False,
timeout=30
)
# Log the response
logger.info(f"Received response from target: {resp.status_code}")
# Check if response is HTML and filter content if needed
content_type = resp.headers.get('Content-Type', '')
if 'text/html' in content_type:
# Parse HTML content
html_content = resp.content.decode('utf-8', errors='ignore')
soup = BeautifulSoup(html_content, 'html.parser')
# Filter out "Полная версия ETKA"
for element in soup.find_all(string=re.compile('Полная версия ETKA')):
# Replace the text with empty string
element.replace_with('')
# Filter out README content
for element in soup.find_all(string=re.compile('README', re.IGNORECASE)):
element.replace_with('')
# Redirect part number links to Google search
# Look for links that contain part numbers (typically in the second column of the table)
part_number_links = soup.select('td:nth-child(2) a')
for link in part_number_links:
# Get the part number from the link text
part_number = link.text.strip()
# Check if it matches a part number pattern (alphanumeric with possible spaces)
if re.match(r'^[A-Z0-9 ]+$', part_number):
# Create a Google search URL for this part number with avto.pro
google_search_url = f"https://www.google.com/search?q={part_number} avto.pro"
# Update the link's href attribute
link['href'] = google_search_url
# Replace images with buttons
for img in soup.find_all('img'):
# Create button element
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 = 'Показать изображение'
# Replace image with button
img.replace_with(button)
# Create a Flask response object with filtered content
response = Response(
soup.encode(),
status=resp.status_code
)
else:
# Create a Flask response object with original content
response = Response(
resp.content,
status=resp.status_code
)
# Copy headers from the target response
for key, value in resp.headers.items():
if key.lower() not in ('transfer-encoding', 'content-encoding', 'content-length'):
response.headers[key] = value
# Copy cookies from target response
for cookie in resp.cookies:
response.set_cookie(
key=cookie.name,
value=cookie.value,
# domain=cookie.domain,
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)