|
from io import BytesIO |
|
|
|
from flask import Flask, request, jsonify, render_template, send_file, Response |
|
from bs4 import BeautifulSoup |
|
from scraper import voicevox |
|
import requests |
|
|
|
app = Flask(__name__) |
|
|
|
@app.route('/') |
|
def index(): |
|
return '<h1>OK</h1>' |
|
|
|
@app.route("/voicevox") |
|
def voicevox_resp(): |
|
text = request.args.get("text") |
|
speaker = request.args.get("speaker") |
|
if not text: |
|
return jsonify({'error': "Please provided parameter 'text'"}), 400 |
|
|
|
try: |
|
data = voicevox(text, speaker=speaker) |
|
io = BytesIO(data) |
|
|
|
return send_file(io, as_attachment=True, mimetype="audio/mp3", download_name=f'rull-pyward_voicevox({text.strip()}).mp3') |
|
except Exception as e: |
|
return jsonify({'error': "Internal server error"}), 500 |
|
|
|
@app.route('/proxy') |
|
def proxy(): |
|
url = request.args.get('url') |
|
if not url: |
|
return jsonify({'error': 'URL parameter is missing'}), 400 |
|
|
|
try: |
|
headers = {key: value for (key, value) in request.headers if key != 'Host'} |
|
cookies = request.cookies |
|
method = request.method |
|
data = request.get_data() |
|
|
|
response = requests.request( |
|
method=method, |
|
url=url, |
|
|
|
|
|
|
|
allow_redirects=False |
|
) |
|
|
|
print(response.headers.items()) |
|
if response.headers.get('content-type', '').startswith('text/html'): |
|
|
|
soup = BeautifulSoup(response.text, 'html.parser') |
|
for tag in soup.find_all(href=True): |
|
tag['href'] = '/proxy?url=' + tag['href'] |
|
for tag in soup.find_all(src=True): |
|
tag['src'] = '/proxy?url=' + tag['src'] |
|
content = soup.prettify(formatter="html") |
|
return content, response.status_code |
|
else: |
|
return Response(response.content, status=response.status_code) |
|
except requests.RequestException as e: |
|
return jsonify({'error': str(e)}), 500 |
|
|
|
if __name__ == '__main__': |
|
app.run(host="0.0.0.0", port=7860) |
|
|