arcanus commited on
Commit
29996d4
·
verified ·
1 Parent(s): 9f8ef37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -10
app.py CHANGED
@@ -44,7 +44,6 @@ def login():
44
  return redirect(url_for('index'))
45
  return render_template('login.html', error='Nesprávné heslo')
46
  return render_template('login.html')
47
-
48
  @app.route('/logout')
49
  def logout():
50
  session.pop('logged_in', None)
@@ -120,15 +119,20 @@ def translate():
120
  # Handle YouTube URL
121
  url = data['url'].strip()
122
  if 'youtube.com' in url or 'youtu.be' in url:
123
- try:
124
- media_file, thumbnail_url = download_youtube_video(url, project_path ) #PROXY_URL, PROXY_USER, PROXY_PASS
125
- # Copy downloaded file to video.mp4
126
- final_path = os.path.join(project_path, "video.mp4")
127
- shutil.copy(media_file, final_path)
128
- media_file = final_path
129
- print("YouTube video downloaded to:", media_file)
130
- except Exception as e:
131
- return jsonify({'success': False, 'error': f'YouTube download failed: {str(e)}'})
 
 
 
 
 
132
  else:
133
  return jsonify({'success': False, 'error': 'Invalid YouTube URL'})
134
 
@@ -333,6 +337,16 @@ def download_youtube_video(url, project_path, proxy=None, proxy_user=None, proxy
333
  print(f"Status code: {response.status_code}")
334
  print(f"Response headers: {dict(response.headers)}")
335
 
 
 
 
 
 
 
 
 
 
 
336
  if response.status_code != 200:
337
  error_message = f"API request failed with status code {response.status_code}"
338
  try:
 
44
  return redirect(url_for('index'))
45
  return render_template('login.html', error='Nesprávné heslo')
46
  return render_template('login.html')
 
47
  @app.route('/logout')
48
  def logout():
49
  session.pop('logged_in', None)
 
119
  # Handle YouTube URL
120
  url = data['url'].strip()
121
  if 'youtube.com' in url or 'youtu.be' in url:
122
+ # Check if we already have this video downloaded
123
+ if 'video_path' in session and os.path.exists(session['video_path']):
124
+ media_file = session['video_path']
125
+ print("Using already downloaded video:", media_file)
126
+ else:
127
+ try:
128
+ media_file, thumbnail_url = download_youtube_video(url, project_path ) #PROXY_URL, PROXY_USER, PROXY_PASS
129
+ # Copy downloaded file to video.mp4
130
+ final_path = os.path.join(project_path, "video.mp4")
131
+ shutil.copy(media_file, final_path)
132
+ media_file = final_path
133
+ print("YouTube video downloaded to:", media_file)
134
+ except Exception as e:
135
+ return jsonify({'success': False, 'error': f'YouTube download failed: {str(e)}'})
136
  else:
137
  return jsonify({'success': False, 'error': 'Invalid YouTube URL'})
138
 
 
337
  print(f"Status code: {response.status_code}")
338
  print(f"Response headers: {dict(response.headers)}")
339
 
340
+ # First check if the response is JSON (error message)
341
+ content_type = response.headers.get('Content-Type', '').lower()
342
+ if 'application/json' in content_type:
343
+ try:
344
+ error_data = response.json()
345
+ error_message = error_data.get('error', 'Unknown API error')
346
+ raise Exception(f"API returned error: {error_message}")
347
+ except json.JSONDecodeError:
348
+ pass # Not JSON, continue with file download
349
+
350
  if response.status_code != 200:
351
  error_message = f"API request failed with status code {response.status_code}"
352
  try: