imseldrith commited on
Commit
f457163
·
verified ·
1 Parent(s): 683deed

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
+
5
+ app = Flask(__name__)
6
+
7
+ def text_to_speech(msg, voice):
8
+ # Start a session
9
+ session = requests.Session()
10
+
11
+ # Get the initial page to retrieve CSRF token and initial cookies
12
+ initial_url = "https://texttospeech.online/home/tryme"
13
+ response = session.get(initial_url)
14
+
15
+ # Parse the response to extract the CSRF token
16
+ soup = BeautifulSoup(response.text, 'html.parser')
17
+ csrf_token = soup.find('input', {'name': 'csrf_test_name'})['value']
18
+
19
+ # Define payload with dynamic CSRF token
20
+ payload = {
21
+ "csrf_test_name": csrf_token,
22
+ "front_tryme_language": "en-IN", # Assuming the language is fixed
23
+ "front_tryme_voice": voice,
24
+ "front_tryme_text": msg
25
+ }
26
+
27
+ # Define headers
28
+ headers = {
29
+ "Accept": "*/*",
30
+ "Accept-Encoding": "gzip, deflate, br, zstd",
31
+ "Accept-Language": "en-US,en;q=0.9",
32
+ "Connection": "keep-alive",
33
+ "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
34
+ "Host": "texttospeech.online",
35
+ "Origin": "https://texttospeech.online",
36
+ "Referer": "https://texttospeech.online/",
37
+ "Sec-Ch_Ua": '"Not/A)Brand";v="8", "Chromium";v="126", "Microsoft Edge";v="126"',
38
+ "Sec-Ch_Ua-Mobile": "?0",
39
+ "Sec-Ch_Ua-Platform": '"Windows"',
40
+ "Sec-Fetch-Dest": "empty",
41
+ "Sec-Fetch-Mode": "cors",
42
+ "Sec-Fetch-Site": "same-origin",
43
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0",
44
+ "X-Requested-With": "XMLHttpRequest"
45
+ }
46
+
47
+ # Make the POST request with the session
48
+ post_url = "https://texttospeech.online/home/tryme_action/"
49
+ response = session.post(post_url, headers=headers, data=payload)
50
+
51
+ # Parse the JSON response
52
+ response_data = response.json()
53
+
54
+ if response_data["result"]:
55
+ tts_uri = response_data["tts_uri"]
56
+ return tts_uri
57
+ else:
58
+ return None
59
+
60
+ @app.route('/text_to_speech', methods=['POST'])
61
+ def api_text_to_speech():
62
+ # Extract parameters from URL query
63
+ url_msg = request.args.get('msg')
64
+ url_voice = request.args.get('voice')
65
+
66
+ # Extract parameters from JSON body
67
+ json_data = request.json
68
+ body_msg = json_data.get('msg') if json_data else None
69
+ body_voice = json_data.get('voice') if json_data else None
70
+
71
+ # Prioritize URL parameters over JSON body parameters
72
+ msg = url_msg if url_msg else body_msg
73
+ voice = url_voice if url_voice else body_voice
74
+
75
+ if not msg or not voice:
76
+ return jsonify({"error": "Please provide both 'msg' and 'voice' parameters."}), 400
77
+
78
+ tts_uri = text_to_speech(msg, voice)
79
+ if tts_uri:
80
+ return jsonify({"tts_uri": tts_uri})
81
+ else:
82
+ return jsonify({"error": "Failed to generate text-to-speech audio."}), 500
83
+
84
+ if __name__ == '__main__':
85
+ app.run(debug=True)