Spaces:
Sleeping
Sleeping
Keys commited on
Commit ·
af4e608
1
Parent(s): 314ffaf
Added Cookies to make it smoother
Browse files- .DS_Store +0 -0
- app.py +56 -19
- cookies.txt +20 -0
- docker-compose.yaml +8 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
app.py
CHANGED
|
@@ -11,13 +11,14 @@ from email import encoders
|
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 13 |
import multiprocessing
|
| 14 |
-
from flask import Flask, render_template, request, jsonify
|
| 15 |
from googleapiclient.discovery import build
|
| 16 |
import yt_dlp
|
| 17 |
from pydub import AudioSegment
|
| 18 |
|
| 19 |
-
|
| 20 |
|
|
|
|
| 21 |
|
| 22 |
# Load API key and email credentials from .env file
|
| 23 |
load_dotenv()
|
|
@@ -27,11 +28,13 @@ email_password = os.getenv('EMAIL_PASSWORD')
|
|
| 27 |
|
| 28 |
# Validate environment variables
|
| 29 |
if not all([api_key, sender_email, email_password]):
|
|
|
|
| 30 |
raise ValueError("Missing environment variables. Please check your .env file.")
|
| 31 |
|
| 32 |
# Determine the number of CPU cores available
|
| 33 |
num_cores = multiprocessing.cpu_count()
|
| 34 |
|
|
|
|
| 35 |
# Function to validate email
|
| 36 |
def is_valid_email(email):
|
| 37 |
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
|
|
@@ -64,28 +67,40 @@ def get_youtube_links(api_key, query, max_results=20):
|
|
| 64 |
def download_single_audio(url, index, download_path):
|
| 65 |
ydl_opts = {
|
| 66 |
'format': 'bestaudio/best',
|
| 67 |
-
'outtmpl': f'{download_path}/song_{index}
|
| 68 |
'postprocessors': [{
|
| 69 |
'key': 'FFmpegExtractAudio',
|
| 70 |
'preferredcodec': 'mp3',
|
| 71 |
'preferredquality': '192',
|
| 72 |
}],
|
| 73 |
-
'retries':
|
| 74 |
-
'fragment_retries':
|
|
|
|
| 75 |
}
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
# Function to download all audio files in parallel
|
| 91 |
def download_all_audio(video_urls, download_path):
|
|
@@ -175,6 +190,20 @@ def send_email(sender_email, receiver_email, subject, body, attachment_path, pas
|
|
| 175 |
def index():
|
| 176 |
return render_template('index.html')
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
@app.route('/create_mashup', methods=['POST'])
|
| 179 |
def create_mashup_route():
|
| 180 |
try:
|
|
@@ -190,17 +219,20 @@ def create_mashup_route():
|
|
| 190 |
# Validate required fields
|
| 191 |
if not all([singer_name, num_videos, trim_duration, receiver_email]):
|
| 192 |
missing_fields = [field for field in ['singer_name', 'num_videos', 'trim_duration', 'receiver_email'] if not request.form.get(field)]
|
|
|
|
| 193 |
return jsonify({'status': 'error', 'message': f'Missing required fields: {", ".join(missing_fields)}'})
|
| 194 |
|
| 195 |
# Validate and convert numeric fields
|
| 196 |
try:
|
| 197 |
num_videos = max(int(num_videos), 10)
|
| 198 |
trim_duration = max(int(trim_duration), 20)
|
| 199 |
-
except ValueError:
|
|
|
|
| 200 |
return jsonify({'status': 'error', 'message': 'Invalid numeric values for num_videos or trim_duration'})
|
| 201 |
|
| 202 |
# Validate email
|
| 203 |
if not is_valid_email(receiver_email):
|
|
|
|
| 204 |
return jsonify({'status': 'error', 'message': 'Please enter a valid email address.'})
|
| 205 |
|
| 206 |
# Fetch YouTube links
|
|
@@ -208,6 +240,7 @@ def create_mashup_route():
|
|
| 208 |
videos = get_youtube_links(api_key, singer_name, max_results=num_videos)
|
| 209 |
|
| 210 |
if not videos:
|
|
|
|
| 211 |
return jsonify({'status': 'error', 'message': f'No videos found for {singer_name}. Please try a different singer name.'})
|
| 212 |
|
| 213 |
# Create temporary directory
|
|
@@ -220,6 +253,7 @@ def create_mashup_route():
|
|
| 220 |
audio_files = download_all_audio(video_urls, download_path)
|
| 221 |
|
| 222 |
if not audio_files:
|
|
|
|
| 223 |
return jsonify({'status': 'error', 'message': 'Failed to download audio files. Please try again.'})
|
| 224 |
|
| 225 |
# Create mashup
|
|
@@ -228,6 +262,7 @@ def create_mashup_route():
|
|
| 228 |
mashup_file = create_mashup(audio_files, output_file, trim_duration)
|
| 229 |
|
| 230 |
if not mashup_file:
|
|
|
|
| 231 |
return jsonify({'status': 'error', 'message': 'Failed to create mashup. Please try again.'})
|
| 232 |
|
| 233 |
# Create zip file
|
|
@@ -241,8 +276,10 @@ def create_mashup_route():
|
|
| 241 |
email_sent = send_email(sender_email, receiver_email, subject, body, zip_file, email_password)
|
| 242 |
|
| 243 |
if email_sent:
|
|
|
|
| 244 |
return jsonify({'status': 'success', 'message': 'Mashup created and sent successfully! Check your email.'})
|
| 245 |
else:
|
|
|
|
| 246 |
return jsonify({'status': 'error', 'message': 'Mashup created but failed to send email. Please try again.'})
|
| 247 |
|
| 248 |
except Exception as e:
|
|
@@ -251,4 +288,4 @@ def create_mashup_route():
|
|
| 251 |
|
| 252 |
if __name__ == '__main__':
|
| 253 |
port = int(os.environ.get('PORT', 5000))
|
| 254 |
-
app.run(host='0.0.0.0', port=port)
|
|
|
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 13 |
import multiprocessing
|
| 14 |
+
from flask import Flask, render_template, request, jsonify, send_from_directory
|
| 15 |
from googleapiclient.discovery import build
|
| 16 |
import yt_dlp
|
| 17 |
from pydub import AudioSegment
|
| 18 |
|
| 19 |
+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 20 |
|
| 21 |
+
app = Flask(__name__, static_folder='static')
|
| 22 |
|
| 23 |
# Load API key and email credentials from .env file
|
| 24 |
load_dotenv()
|
|
|
|
| 28 |
|
| 29 |
# Validate environment variables
|
| 30 |
if not all([api_key, sender_email, email_password]):
|
| 31 |
+
logging.error("Missing environment variables. Please check your .env file.")
|
| 32 |
raise ValueError("Missing environment variables. Please check your .env file.")
|
| 33 |
|
| 34 |
# Determine the number of CPU cores available
|
| 35 |
num_cores = multiprocessing.cpu_count()
|
| 36 |
|
| 37 |
+
|
| 38 |
# Function to validate email
|
| 39 |
def is_valid_email(email):
|
| 40 |
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
|
|
|
|
| 67 |
def download_single_audio(url, index, download_path):
|
| 68 |
ydl_opts = {
|
| 69 |
'format': 'bestaudio/best',
|
| 70 |
+
'outtmpl': f'{download_path}/song_{index}.%(ext)s',
|
| 71 |
'postprocessors': [{
|
| 72 |
'key': 'FFmpegExtractAudio',
|
| 73 |
'preferredcodec': 'mp3',
|
| 74 |
'preferredquality': '192',
|
| 75 |
}],
|
| 76 |
+
'retries': 3,
|
| 77 |
+
'fragment_retries': 3,
|
| 78 |
+
'cookies': './cookies.txt', # Add this line, pointing to your cookies.txt file
|
| 79 |
}
|
| 80 |
|
| 81 |
+
max_attempts = 3
|
| 82 |
+
for attempt in range(max_attempts):
|
| 83 |
+
try:
|
| 84 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 85 |
+
ydl.download([url])
|
| 86 |
+
downloaded_files = [f for f in os.listdir(download_path) if f.startswith(f"song_{index}.") and f.endswith(".mp3")]
|
| 87 |
+
if downloaded_files:
|
| 88 |
+
return os.path.join(download_path, downloaded_files[0])
|
| 89 |
+
else:
|
| 90 |
+
logging.error(f"Downloaded file not found for {url}")
|
| 91 |
+
return None
|
| 92 |
+
except Exception as e:
|
| 93 |
+
logging.error(f"Error downloading audio (attempt {attempt + 1}/{max_attempts}): {e}")
|
| 94 |
+
if "Sign in to confirm you're not a bot" in str(e):
|
| 95 |
+
sleep_time = random.uniform(5, 10)
|
| 96 |
+
logging.info(f"Detected anti-bot measure. Waiting for {sleep_time:.2f} seconds before retrying...")
|
| 97 |
+
time.sleep(sleep_time)
|
| 98 |
+
else:
|
| 99 |
+
return None
|
| 100 |
+
|
| 101 |
+
logging.error(f"Failed to download audio after {max_attempts} attempts: {url}")
|
| 102 |
+
return None
|
| 103 |
+
|
| 104 |
|
| 105 |
# Function to download all audio files in parallel
|
| 106 |
def download_all_audio(video_urls, download_path):
|
|
|
|
| 190 |
def index():
|
| 191 |
return render_template('index.html')
|
| 192 |
|
| 193 |
+
@app.route('/static/<path:filename>')
|
| 194 |
+
def serve_static(filename):
|
| 195 |
+
return send_from_directory(app.static_folder, filename)
|
| 196 |
+
|
| 197 |
+
@app.errorhandler(404)
|
| 198 |
+
def page_not_found(e):
|
| 199 |
+
logging.error(f"404 error: {request.url}")
|
| 200 |
+
return jsonify(error=str(e)), 404
|
| 201 |
+
|
| 202 |
+
@app.errorhandler(500)
|
| 203 |
+
def internal_server_error(e):
|
| 204 |
+
logging.error(f"500 error: {str(e)}")
|
| 205 |
+
return jsonify(error="Internal Server Error"), 500
|
| 206 |
+
|
| 207 |
@app.route('/create_mashup', methods=['POST'])
|
| 208 |
def create_mashup_route():
|
| 209 |
try:
|
|
|
|
| 219 |
# Validate required fields
|
| 220 |
if not all([singer_name, num_videos, trim_duration, receiver_email]):
|
| 221 |
missing_fields = [field for field in ['singer_name', 'num_videos', 'trim_duration', 'receiver_email'] if not request.form.get(field)]
|
| 222 |
+
logging.error(f"Missing required fields: {missing_fields}")
|
| 223 |
return jsonify({'status': 'error', 'message': f'Missing required fields: {", ".join(missing_fields)}'})
|
| 224 |
|
| 225 |
# Validate and convert numeric fields
|
| 226 |
try:
|
| 227 |
num_videos = max(int(num_videos), 10)
|
| 228 |
trim_duration = max(int(trim_duration), 20)
|
| 229 |
+
except ValueError as e:
|
| 230 |
+
logging.error(f"Invalid numeric values: {str(e)}")
|
| 231 |
return jsonify({'status': 'error', 'message': 'Invalid numeric values for num_videos or trim_duration'})
|
| 232 |
|
| 233 |
# Validate email
|
| 234 |
if not is_valid_email(receiver_email):
|
| 235 |
+
logging.error(f"Invalid email address: {receiver_email}")
|
| 236 |
return jsonify({'status': 'error', 'message': 'Please enter a valid email address.'})
|
| 237 |
|
| 238 |
# Fetch YouTube links
|
|
|
|
| 240 |
videos = get_youtube_links(api_key, singer_name, max_results=num_videos)
|
| 241 |
|
| 242 |
if not videos:
|
| 243 |
+
logging.warning(f"No videos found for {singer_name}")
|
| 244 |
return jsonify({'status': 'error', 'message': f'No videos found for {singer_name}. Please try a different singer name.'})
|
| 245 |
|
| 246 |
# Create temporary directory
|
|
|
|
| 253 |
audio_files = download_all_audio(video_urls, download_path)
|
| 254 |
|
| 255 |
if not audio_files:
|
| 256 |
+
logging.error("Failed to download audio files")
|
| 257 |
return jsonify({'status': 'error', 'message': 'Failed to download audio files. Please try again.'})
|
| 258 |
|
| 259 |
# Create mashup
|
|
|
|
| 262 |
mashup_file = create_mashup(audio_files, output_file, trim_duration)
|
| 263 |
|
| 264 |
if not mashup_file:
|
| 265 |
+
logging.error("Failed to create mashup")
|
| 266 |
return jsonify({'status': 'error', 'message': 'Failed to create mashup. Please try again.'})
|
| 267 |
|
| 268 |
# Create zip file
|
|
|
|
| 276 |
email_sent = send_email(sender_email, receiver_email, subject, body, zip_file, email_password)
|
| 277 |
|
| 278 |
if email_sent:
|
| 279 |
+
logging.info("Mashup created and sent successfully")
|
| 280 |
return jsonify({'status': 'success', 'message': 'Mashup created and sent successfully! Check your email.'})
|
| 281 |
else:
|
| 282 |
+
logging.error("Failed to send email")
|
| 283 |
return jsonify({'status': 'error', 'message': 'Mashup created but failed to send email. Please try again.'})
|
| 284 |
|
| 285 |
except Exception as e:
|
|
|
|
| 288 |
|
| 289 |
if __name__ == '__main__':
|
| 290 |
port = int(os.environ.get('PORT', 5000))
|
| 291 |
+
app.run(host='0.0.0.0', port=port, debug=True)
|
cookies.txt
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Netscape HTTP Cookie File
|
| 2 |
+
# http://curl.haxx.se/rfc/cookie_spec.html
|
| 3 |
+
# This is a generated file! Do not edit.
|
| 4 |
+
|
| 5 |
+
.youtube.com TRUE / FALSE 1763466624 SID g.a000pAj5F3HMSqcNV6n3F93cDMDDryYxA0lO2ZoyFVmF-dqYiuLAU7zIx5qpxz4s9SC59wtu_gACgYKAT8SARcSFQHGX2MiWarknUe1D4RzVf9ItEYBbhoVAUF8yKq25yLWB1vIzyUjH4WiLdHv0076
|
| 6 |
+
.youtube.com TRUE / TRUE 1763466624 __Secure-1PSID g.a000pAj5F3HMSqcNV6n3F93cDMDDryYxA0lO2ZoyFVmF-dqYiuLAMRgm6O41z0UXjDHEBei_mwACgYKAXISARcSFQHGX2MiNz8Y1lmNhIFIvLGNY9xhGxoVAUF8yKrQGoMb7hPq5rpUhh4OzFI40076
|
| 7 |
+
.youtube.com TRUE / TRUE 1763466624 __Secure-3PSID g.a000pAj5F3HMSqcNV6n3F93cDMDDryYxA0lO2ZoyFVmF-dqYiuLAXGXFb5EmNsSpmmHf1Po50QACgYKARESARcSFQHGX2Mi-cPZ3H5k3A7H6K4vyysLcRoVAUF8yKqI4NfLJDYojdv1owc5_GC00076
|
| 8 |
+
.youtube.com TRUE / TRUE 1763466624 SSID ApIN3EDNCzhRGfY8L
|
| 9 |
+
.youtube.com TRUE / FALSE 1763466624 HSID A_WmzYwOMs-yx2_fP
|
| 10 |
+
.youtube.com TRUE / TRUE 1763466624 SAPISID 06O7J_g6qrvzOajY/A_Flr7w_fQK_UUeii
|
| 11 |
+
.youtube.com TRUE / FALSE 1763466624 APISID 3Wjss_KIj7aQHPcQ/ADddMiReS7AOinmWp
|
| 12 |
+
.youtube.com TRUE / TRUE 1763466624 __Secure-1PAPISID 06O7J_g6qrvzOajY/A_Flr7w_fQK_UUeii
|
| 13 |
+
.youtube.com TRUE / TRUE 1763466624 __Secure-3PAPISID 06O7J_g6qrvzOajY/A_Flr7w_fQK_UUeii
|
| 14 |
+
.youtube.com TRUE / TRUE 1763466624 LOGIN_INFO AFmmF2swRAIgVbh-4W0TdHPfPC5tQhB89naQUbAGjUamJh8g81MLEoMCIHV5ClV2alONeZtTSstSo0Xlp0hDv37JoVWTpQOMb7GF:QUQ3MjNmeG0zb2t3dzdYNEUwVEhYenVzNWVzeXRLdkJEMnFzckx5MF9xNUwzUEVvTWdEQThOSHVlRlRRY2F5QnFUTnRfeEw2TWF3TUVkc2VkXzdxNFdDR0FmeGpLd2Z4VVo0Q3psc25Za3NLZGtYMTNfYnNUUTN5U3RRMnJoMW8yNlZqNUVnRFJ0dDZYYXVrMU5tWmt1ZGh0d2ZqY2VhSXJR
|
| 15 |
+
.youtube.com TRUE / TRUE 1763467212 PREF f4=4000000&f6=40000000&tz=Asia.Calcutta&f7=100
|
| 16 |
+
.youtube.com TRUE / TRUE 1760443832 __Secure-1PSIDTS sidts-CjIBQlrA-P4Wh_1DxGbHXYhfVyyBQJU3skESNKn8lOgnMEH4O09MjEb1dKgxz85yuy5oWBAA
|
| 17 |
+
.youtube.com TRUE / TRUE 1760443832 __Secure-3PSIDTS sidts-CjIBQlrA-P4Wh_1DxGbHXYhfVyyBQJU3skESNKn8lOgnMEH4O09MjEb1dKgxz85yuy5oWBAA
|
| 18 |
+
.youtube.com TRUE / FALSE 1760443896 SIDCC AKEyXzVhchPf_TH3uBpiLLKQb8ml5Uvq3DKRhMq1eS5dHruppzoEV-Qb523g6uJwL5GPlq55Hg
|
| 19 |
+
.youtube.com TRUE / TRUE 1760443896 __Secure-1PSIDCC AKEyXzU2scky0Ub9y-ZUGq0KfuFVwyD2uaMJddtYg5TNFgIPcKur5mr59EVc_8tXZ-B1KQ-dRg
|
| 20 |
+
.youtube.com TRUE / TRUE 1760443896 __Secure-3PSIDCC AKEyXzULjGYZ4EFmZBrYiOiI7jGDiEU1ThcpkbQfi_L8RlQ3yh3ykY9DMfBYykaECxUJlpQEVg
|
docker-compose.yaml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version: "3.8"
|
| 2 |
+
services:
|
| 3 |
+
flask-app:
|
| 4 |
+
build: .
|
| 5 |
+
ports:
|
| 6 |
+
- "7860:7860"
|
| 7 |
+
env_file:
|
| 8 |
+
- .env
|