File size: 2,474 Bytes
01d0b27 a3fd48d 6d8044f 96f653d e239ad9 6d8044f 15f9b86 cc35151 15f9b86 d7e6a25 25fffdf a442d45 96f653d 15f9b86 4ec4372 96f653d 4ec4372 e239ad9 15f9b86 96f653d 01d0b27 e239ad9 06f8d40 e239ad9 96f653d 742893c 96f653d 25fffdf 6d8044f 8f4e3e0 25d08e0 276f9ea 6d8044f 78cfc41 ee554bb 40bfceb 232d4d1 1f93e9f 6d8044f 0630c4e 276f9ea 78cfc41 6d8044f e0be4fc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
from io import BytesIO
from flask import Flask, request, jsonify, render_template, send_file, Response
from bs4 import BeautifulSoup
from scraper import voicevox
from utils import mp3_to_opus
import requests
app = Flask(__name__)
def isTrue(thing):
return str(thing).lower() == "true"
@app.route('/')
def index():
return '<h1>OK</h1>'
@app.route("/voicevox")
def voicevox_resp():
text = request.args.get("text")
speaker = request.args.get("speaker")
opus = isTrue(request.args.get("opus"))
if text is None:
return jsonify({'error': "Please provided parameter 'text'"}), 400
if speaker is None:
speaker = 1
try:
data = voicevox(text, speaker=speaker)
io = BytesIO(data)
if opus:
io = mp3_to_opus(io)
mime = "audio/ogg" if opus else "audio/mpeg"
ext = ".opus" if opus else ".mp3"
return send_file(io, as_attachment=True, mimetype=mime, download_name=f'rull-pyward_voicevox({text.strip()}){ext}')
except Exception as e:
print(e)
return jsonify({'error': str(e) }), 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,
# headers=headers,
# cookies=cookies,
# data=data,
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) # Use 'adhoc' to enable HTTPS locally
|