if name == 'main':
app = QApplication(sys.argv)
ex = YouTubeDownloader()
ex.show()
app.aboutToQuit.connect(ex.cleanup_threads)
sys.exit(app.exec())
Assistant
The provided code implements a YouTube downloader application using PyQt6 and the pytube library. Below is a breakdown of the main components, functionalities, and improvements that can be considered.
Key Components
UI Components:
QLineEdit: For inputting the YouTube video URL.
QLabel: To display the title of the video.
QComboBox: To select the video quality.
QPushButton: For selecting the save path and starting/canceling the download.
QProgressBar: To visually indicate download progress.
QTextEdit: For logging messages and errors.
Logging:
A custom logging handler (LogHandler) directs log messages to the QTextEdit for user feedback.
Threads:
VideoInfoThread: Fetches video information asynchronously to avoid blocking the GUI.
DownloadThread: Handles the actual video downloading process in the background.
Debouncing:
To optimize the URL input process, a debounce mechanism is implemented, which delays checks for video information until the user has stopped typing for a specified period.
Progress Updates:
The DownloadThread provides ongoing updates on download progress, speed, and estimated remaining time using signals to update the UI.
Functionality
Users can input a YouTube video URL, select a desired quality, and specify the download directory.
The application retrieves video information and presents it to the user.
The download progress is displayed and can be canceled by the user.
Errors are logged and presented to the user via message boxes.
Possible Improvements
User Experience:
Allow users to select a specific video track or audio track, if applicable.
Provide more feedback during the analysis of video information, such as showing a loading indicator.
Error Handling:
Enhance error handling for specific scenarios (e.g., network issues, invalid URLs).
User
Ошибка
Ошибка: HTTP Error 429: Too Many Requests
OK
Assistant
The error message "HTTP Error 429: Too Many Requests" indicates that the application is being rate-limited by YouTube. This means that requests to fetch video details or download videos are being made too frequently, and YouTube's servers are temporarily blocking further requests from your IP address.
How to Handle Error 429
Implement a Retry Mechanism:
When you encounter an HTTP 429 error, you can implement a wait-and-retry mechanism to pause the execution for a certain duration before trying the request again.
Backoff Strategy:
Use an exponential backoff strategy where the wait time increases with each subsequent error. This helps in reducing the load on the server and increases the chances of a successful request after being rate-limited.
Reduce Request Frequency:
Introduce delays between requests to the YouTube server. This can be particularly useful when updating video information or performing multiple downloads in a short period.
Inform the User:
Update the user interface with a proper message when this error occurs, indicating that they should wait before retrying.
Example of Handling Error 429
Here's an example of how you could implement a retry mechanism in your VideoInfoThread and DownloadThread to handle the 429 error. You would need to modify the run method of both classes.
Example Modification for VideoInfoThread
python
User
Исправь весь код полностью
Assistant
Certainly! Below, I’ve provided the complete modified code for the YouTube downloader application, incorporating a retry mechanism with exponential backoff to handle HTTP 429 errors. This will allow your application to pause and retry requests if YouTube is rate-limiting you.
python
User
matched regex search: (?:v=|/)([0-9A-Za-z_-]{11}).*
HTTP 429: Too many requests. Retrying in 2 seconds...
matched regex search: (?:v=|/)([0-9A-Za-z_-]{11}).*
HTTP 429: Too many requests. Retrying in 4 seconds...
matched regex search: (?:v=|/)([0-9A-Za-z_-]{11}).*
HTTP 429: Too many requests. Retrying in 8 seconds...
Assistant
The repeated messages regarding "HTTP 429: Too many requests" indicate that your application is still facing issues with YouTube's rate limiting. This typically happens when the application is making multiple requests in a short period of time, leading to the server temporarily blocking further requests from your IP address.
Enhancing the Retry Logic
To further address this issue, you can enhance the retry logic by implementing the following strategies:
Increasing Backoff Duration: Instead of doubling the wait time, you can use a more aggressive exponential backoff strategy, or include a random jitter to help avoid synchronized retries from multiple users hitting the server at the same time.
Reducing Frequency of Requests: Make sure that requests aren’t being made too frequently in the UI. For example, instead of checking video information on every keystroke in the URL input field, you can lengthen the debounce time.
Handling Other Errors: Ensure that you gracefully handle and log other issues that do not produce a 429 error.
Example Code Adjustments
Below are some adjustments you could make to improve your handling of HTTP 429 errors:
Enhanced Backoff Strategy
You can improve the backoff strategy in your retry logic. Here is how you might modify the existing retry mechanism:
python
User
На русском
Assistant
Повторяющиеся сообщения об ошибке "HTTP 429: Слишком много запросов" указывают на то, что ваше приложение по-прежнему сталкивается с проблемами ограничения частоты запросов со стороны YouTube. Это происходит, когда приложение делает несколько запросов за короткий промежуток времени, что приводит к временной блокировке дальнейших запросов с вашего IP-адреса.
Улучшение Логики Повтора
Чтобы устранить эту проблему, вы можете усилить логику повтора, внедрив следующие стратегии:
Увеличение Времени Ожидания (Backoff): Вместо удвоения времени ожидания вы можете использовать более агрессивную стратегию экспоненциального ожидания или добавить рандомизированный "джиттер", чтобы избежать синхронизации повторных запросов от нескольких пользователей.
Сокращение Частоты Запросов: Убедитесь, что запросы не отправляются слишком часто в пользовательском интерфейсе. Например, вместо того чтобы проверять информацию о видео при каждом нажатии клавиши в поле URL, вы можете увеличить время ожидания (debounce time).
Обработка Других Ошибок: Убедитесь, что вы корректно обрабатываете и регистрируете другие проблемы, которые не приводят к ошибке 429.
Пример Изменений в Коде
Ниже приведены некоторые изменения, которые вы могли бы внести, чтобы улучшить обработку ошибок HTTP 429:
Улучшенная Стратегия Повтора
Вы можете улучшить логику повтора в своем коде, используя следующие изменения:
python
User
Полностью код обнови
Assistant
Конечно! Вот обновлённый код для приложения загрузки видео с YouTube с улучшенной обработкой ошибки "HTTP 429: Слишком много запросов", а также увеличенным временем ожидания для дебаунса:
Clearly distinguish between different error types (user errors vs. library errors).
User Interface:
Add icons to the buttons for better visual appeal.
Organize the layout more intuitively, for example, consider putting all controls related to downloading in one section.
Functionality:
Add support for downloading playlists or channel content.
Implement a settings configuration for default download paths and formats.
Code Maintenance:
Consider breaking the application into modules, like separating UI components and logic handling, to improve maintainability.
Testing:
Add unit tests for the core functionality, especially the thread handling and downloader logic.
Complete Context in Code
Here’s a summary of the context and design choices made in the code:
This application effectively combines a user-friendly GUI with background processing to download YouTube videos. The implementation of threading avoids freezing the user interface, providing a smooth experience. Logging vital information and errors helps users troubleshoot and understand what actions are being taken by the application.
Overall, it's a solid starting point for a YouTube downloader application with potential for further enhancements to improve user experience and expand functionality. If needed, further assistance with specific features or improvements can be provided.
import time # Import time for sleep function
class VideoInfoThread(QThread):
# ... existing code ...
def run(self):
retry_count = 0
while retry_count < 5: # Allow a maximum of 5 retries
try:
yt = YouTube(self.url)
title = yt.title
self.info_signal.emit(title)
return # Exit the loop if successful
except exceptions.PytubeError as e:
self.error_signal.emit(f'Ошибка Pytube: {e}')
return
except Exception as e:
if "429" in str(e): # Check for the 429 error
retry_count += 1
wait_time = 2 ** retry_count # Exponential backoff
logging.warning(f'HTTP 429: Too many requests. Retrying in {wait_time} seconds...')
time.sleep(wait_time) # Wait before retrying
else:
self.error_signal.emit(f'Ошибка: {e}')
return
self.error_signal.emit('Не удалось получить информацию о видео после нескольких попыток.')
Example Modification for DownloadThread
Similarly, update the DownloadThread class to handle 429 errors:
python
class DownloadThread(QThread):
# ... existing code ...
def run(self):
retry_count = 0
while retry_count < 5:
try:
yt = YouTube(self.url, on_progress_callback=self.on_progress)
if self.quality == 'Выберите качество':
stream = yt.streams.get_highest_resolution()
else:
stream = yt.streams.filter(res=self.quality).first()
if stream is None:
self.error_signal.emit(f'Не удалось найти видео с качеством {self.quality}')
return
stream.download(self.path)
self.finished_signal.emit()
return # Exit the loop if successful
except exceptions.PytubeError as e:
self.error_signal.emit(f'Ошибка Pytube: {e}')
return
except Exception as e:
if "429" in str(e):
retry_count += 1
wait_time = 2 ** retry_count
logging.warning(f'HTTP 429: Too many requests. Retrying in {wait_time} seconds...')
time.sleep(wait_time) # Wait before retrying
else:
self.error_signal.emit(f'Ошибка: {e}')
return
self.error_signal.emit('Не удалось загрузить видео после нескольких попыток.')
Summary
Handling HTTP 429 errors gracefully improves user experience by allowing the application to recover from temporary rate-limiting issues. By implementing a retry mechanism with an exponential backoff strategy, your application will be more resilient to transient network issues or API rate limits.
import sys
import datetime
import logging
import time
from PyQt6.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QLineEdit, QPushButton, QLabel,
QComboBox, QFileDialog, QMessageBox, QProgressBar, QTextEdit, QProgressDialog
)
from PyQt6.QtCore import Qt, QThread, pyqtSignal, QTimer
from pytube import YouTube, exceptions
# Настройка логирования
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
class LogHandler(logging.Handler):
def __init__(self, log_widget):
super().__init__()
self.log_widget = log_widget
def emit(self, record):
msg = self.format(record)
self.log_widget.append(msg)
class VideoInfoThread(QThread):
info_signal = pyqtSignal(str)
error_signal = pyqtSignal(str)
def __init__(self, url):
super().__init__()
self.url = url
def run(self):
retry_count = 0
while retry_count < 5: # Allow a maximum of 5 retries
try:
yt = YouTube(self.url)
title = yt.title
self.info_signal.emit(title)
return # Exit loop if successful
except Exception as e:
if "429" in str(e): # Check for HTTP 429 error
retry_count += 1
wait_time = 2 ** retry_count # Exponential backoff
logging.warning(f'HTTP 429: Too many requests. Retrying in {wait_time} seconds...')
time.sleep(wait_time) # Wait before retrying
else:
self.error_signal.emit(f'Ошибка: {e}')
return
self.error_signal.emit('Не удалось получить информацию о видео после нескольких попыток.')
class DownloadThread(QThread):
progress_signal = pyqtSignal(int)
speed_signal = pyqtSignal(str)
time_signal = pyqtSignal(str)
error_signal = pyqtSignal(str)
finished_signal = pyqtSignal()
def __init__(self, url, quality, path):
super().__init__()
self.url = url
self.quality = quality
self.path = path
self._is_running = True
def run(self):
retry_count = 0
while retry_count < 5:
try:
yt = YouTube(self.url, on_progress_callback=self.on_progress)
if self.quality == 'Выберите качество':
stream = yt.streams.get_highest_resolution()
else:
stream = yt.streams.filter(res=self.quality).first()
if stream is None:
self.error_signal.emit(f'Не удалось найти видео с качеством {self.quality}')
return
stream.download(self.path)
self.finished_signal.emit()
return # Exit loop if successful
except exceptions.PytubeError as e:
self.error_signal.emit(f'Ошибка Pytube: {e}')
return
except Exception as e:
if "429" in str(e):
retry_count += 1
wait_time = 2 ** retry_count
logging.warning(f'HTTP 429: Too many requests. Retrying in {wait_time} seconds...')
time.sleep(wait_time) # Wait before retrying
else:
self.error_signal.emit(f'Ошибка: {e}')
return
self.error_signal.emit('Не удалось загрузить видео после нескольких попыток.')
def on_progress(self, stream, chunk, bytes_remaining):
if not self._is_running:
return
total_size = stream.filesize
bytes_downloaded = total_size - bytes_remaining
percentage = (bytes_downloaded / total_size) * 100
self.progress_signal.emit(int(percentage))
speed = bytes_downloaded / (self.elapsed_time() or 1)
speed_str = f"{speed / (1024 * 1024):.2f} MB/s"
self.speed_signal.emit(speed_str)
time_remaining = bytes_remaining / speed if speed > 0 else 0
time_str = f"{time_remaining / 60:.2f} мин"
self.time_signal.emit(time_str)
def elapsed_time(self):
return (datetime.datetime.now() - self.start_time).total_seconds()
def start(self):
self.start_time = datetime.datetime.now()
super().start()
def stop(self):
self._is_running = False
class YouTubeDownloader(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.debounce_timer = QTimer(self)
self.debounce_timer.timeout.connect(self.on_debounce_timeout)
self.last_url = None
def initUI(self):
self.setWindowTitle('YouTube Downloader')
self.setGeometry(100, 100, 800, 600)
layout = QVBoxLayout()
# URL input
self.url_input = QLineEdit(self)
self.url_input.setPlaceholderText('Введите URL видео с YouTube')
self.url_input.textChanged.connect(self.update_video_info_debounced)
layout.addWidget(self.url_input)
# Title Label
self.title_label = QLabel(self)
layout.addWidget(self.title_label)
# Quality selection
self.quality_combo = QComboBox(self)
self.quality_combo.addItems(['Выберите качество'])
layout.addWidget(self.quality_combo)
# Path selection
self.path_button = QPushButton('Выбрать путь сохранения', self)
self.path_button.clicked.connect(self.select_path)
layout.addWidget(self.path_button)
self.path_label = QLabel(self)
self.path_label.setText('Путь для сохранения: ')
layout.addWidget(self.path_label)
# Download button
self.download_button = QPushButton('Начать загрузку', self)
self.download_button.clicked.connect(self.start_download)
layout.addWidget(self.download_button)
# Cancel download button
self.cancel_button = QPushButton('Отмена загрузки', self)
self.cancel_button.clicked.connect(self.cancel_download)
layout.addWidget(self.cancel_button)
# Progress bar
self.progress_bar = QProgressBar(self)
layout.addWidget(self.progress_bar)
# Speed and time labels
self.speed_label = QLabel(self)
self.speed_label.setText('Скорость: ')
layout.addWidget(self.speed_label)
self.time_label = QLabel(self)
self.time_label.setText('Оставшееся время: ')
layout.addWidget(self.time_label)
# Log output
self.log_output = QTextEdit(self)
self.log_output.setReadOnly(True)
layout.addWidget(self.log_output)
self.setLayout(layout)
# Apply CSS
self.setStyleSheet("""
QWidget {
font-family: 'Arial', sans-serif;
font-size: 14px;
background-color: #f0f0f0;
}
QLineEdit, QComboBox {
padding: 10px;
margin: 10px;
border: 2px solid #ccc;
border-radius: 5px;
background-color: #fff;
color: #333;
}
QLineEdit:focus, QComboBox:focus {
border-color: #e74c3c;
}
QPushButton {
padding: 10px 20px;
margin: 10px;
border: none;
border-radius: 5px;
background-color: #e74c3c;
color: #fff;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 1px;
}
QPushButton:hover {
background-color: #c0392b;
}
QLabel {
padding: 10px;
color: #333;
}
QProgressBar {
border: 2px solid #ccc;
border-radius: 5px;
text-align: center;
background-color: #fff;
}
QProgressBar::chunk {
background-color: #e74c3c;
border-radius: 5px;
}
QTextEdit {
border: 2px solid #ccc;
border-radius: 5px;
background-color: #fff;
color: #333;
padding: 10px;
}
""")
# Setup logging to UI
log_handler = LogHandler(self.log_output)
logging.getLogger().addHandler(log_handler)
def update_video_info_debounced(self):
if self.debounce_timer.isActive():
self.debounce_timer.stop()
self.debounce_timer.start(500) # 500 ms delay
def on_debounce_timeout(self):
url = self.url_input.text()
if url and url != self.last_url:
self.last_url = url
self.title_label.clear()
self.video_info_thread = VideoInfoThread(url)
self.video_info_thread.info_signal.connect(self.set_video_info)
self.video_info_thread.error_signal.connect(self.set_error_info)
self.video_info_thread.start()
else:
self.title_label.clear()
def set_video_info(self, title):
self.title_label.setText(f'Название: {title}')
def set_error_info(self, error_message):
logging.error(error_message)
QMessageBox.warning(self, 'Ошибка', error_message)
def select_path(self):
path = QFileDialog.getExistingDirectory(self, "Выберите папку для сохранения")
if path:
self.path_label.setText(f'Путь для сохранения: {path}')
def start_download(self):
url = self.url_input.text()
quality = self.quality_combo.currentText()
path = self.path_label.text().replace('Путь для сохранения: ', '')
if not url or not path:
QMessageBox.warning(self, 'Ошибка', 'Пожалуйста, заполните все поля')
return
try:
yt = YouTube(url)
available_qualities = [stream.resolution for stream in yt.streams.filter(progressive=True, file_extension='mp4')]
if not available_qualities:
QMessageBox.warning(self, 'Ошибка', 'Нет доступных потоков для этого видео')
return
self.quality_combo.clear()
self.quality_combo.addItems(['Выберите качество'] + available_qualities)
if quality == 'Выберите качество':
stream = yt.streams.get_highest_resolution()
else:
stream = yt.streams.filter(res=quality, progressive=True, file_extension='mp4').first()
if not stream:
QMessageBox.warning(self, 'Ошибка', f'Выбранное качество {quality} недоступно для этого видео')
return
self.download_thread = DownloadThread(url, quality, path)
self.download_thread.progress_signal.connect(self.update_progress)
self.download_thread.speed_signal.connect(self.update_speed)
self.download_thread.time_signal.connect(self.update_time)
self.download_thread.error_signal.connect(self.handle_download_error)
self.download_thread.finished_signal.connect(self.download_finished)
self.download_thread.start()
self.progress_dialog = QProgressDialog("Загрузка видео...", "Отмена", 0, 100, self)
self.progress_dialog.setWindowModality(Qt.WindowModality.WindowModal)
self.progress_dialog.setAutoClose(False)
self.progress_dialog.setAutoReset(False)
self.progress_dialog.canceled.connect(self.cancel_download)
self.progress_dialog.setValue(0)
except Exception as e:
self.log_output.append(f'Ошибка при загрузке видео: {e}')
QMessageBox.warning(self, 'Ошибка', f'Произошла ошибка: {e}')
def update_progress(self, value):
logging.debug(f'Обновление прогресса: {value}%')
self.progress_bar.setValue(value)
self.progress_dialog.setValue(value)
def update_speed(self, speed):
logging.debug(f'Обновление скорости: {speed}')
self.speed_label.setText(f'Скорость: {speed}')
def update_time(self, time):
logging.debug(f'Обновление оставшегося времени: {time}')
self.time_label.setText(f'Оставшееся время: {time}')
def handle_download_error(self, error_message):
self.log_output.append(error_message)
QMessageBox.warning(self, 'Ошибка', error_message)
def download_finished(self):
self.log_output.append('Видео успешно скачано!')
QMessageBox.information(self, 'Успех', 'Видео успешно скачано!')
self.progress_dialog.reset()
def cancel_download(self):
if hasattr(self, 'download_thread') and self.download_thread.isRunning():
self.download_thread.stop()
self.download_thread.quit()
self.download_thread.wait()
self.log_output.append('Загрузка отменена пользователем.')
def cleanup_threads(self):
if hasattr(self, 'video_info_thread') and self.video_info_thread.isRunning():
self.video_info_thread.quit()
self.video_info_thread.wait()
if hasattr(self, 'download_thread') and self.download_thread.isRunning():
self.download_thread.stop()
self.download_thread.quit()
self.download_thread.wait()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = YouTubeDownloader()
ex.show()
app.aboutToQuit.connect(ex.cleanup_threads)
sys.exit(app.exec())
Key Changes Made:
Retries with Exponential Backoff: Both VideoInfoThread and DownloadThread have been modified to handle HTTP 429 errors. Each thread will automatically retry up to 5 times with an increasing wait time (exponential backoff).
Improved Error Handling: The code displays informative messages to the user when an error occurs and logs these messages for debugging.
Instructions to Run the Code:
Make sure you have Python installed along with the required libraries:
PyQt6: pip install PyQt6
Pytube: pip install pytube
Run the code in your Python environment.
Use the application to input YouTube URLs and test the download functionality.
This setup should improve the reliability of video downloads, especially under conditions where YouTube is rate-limiting requests. Let me know if you have any other questions or need further modifications!
import random # Import random for jitter
class VideoInfoThread(QThread):
info_signal = pyqtSignal(str)
error_signal = pyqtSignal(str)
def __init__(self, url):
super().__init__()
self.url = url
def run(self):
retry_count = 0
while retry_count < 5: # Allow a maximum of 5 retries
try:
yt = YouTube(self.url)
title = yt.title
self.info_signal.emit(title)
return # Exit loop if successful
except Exception as e:
if "429" in str(e): # Check for HTTP 429 error
retry_count += 1
wait_time = (2 ** retry_count) + random.uniform(0, 1) # Exponential backoff with jitter
logging.warning(f'HTTP 429: Too many requests. Retrying in {wait_time:.2f} seconds...')
time.sleep(wait_time) # Wait before retrying
else:
self.error_signal.emit(f'Ошибка: {e}')
return
self.error_signal.emit('Не удалось получить информацию о видео после нескольких попыток.')
class DownloadThread(QThread):
# ... [Unmodified part of the code above] ...
def run(self):
retry_count = 0
while retry_count < 5:
try:
yt = YouTube(self.url, on_progress_callback=self.on_progress)
# ... [Unmodified part of the code above] ...
except exceptions.PytubeError as e:
self.error_signal.emit(f'Ошибка Pytube: {e}')
return
except Exception as e:
if "429" in str(e):
retry_count += 1
wait_time = (2 ** retry_count) + random.uniform(0, 1) # Exponential backoff with jitter
logging.warning(f'HTTP 429: Too many requests. Retrying in {wait_time:.2f} seconds...')
time.sleep(wait_time) # Wait before retrying
else:
self.error_signal.emit(f'Ошибка: {e}')
return
self.error_signal.emit('Не удалось загрузить видео после нескольких попыток.')
# The rest of your YouTubeDownloader class remains unchanged
Adjusting Debounce Time
To minimize requests when typing the URL, consider increasing the debounce time in the update_video_info_debounced method:
python
def update_video_info_debounced(self):
if self.debounce_timer.isActive():
self.debounce_timer.stop()
self.debounce_timer.start(1000) # Increase to 1000 ms (1 second) delay
Final Recommendations:
Monitor the application's behavior, especially the frequency of requests being made to YouTube.
If the issue persists, you may want to consider implementing a more robust method of online checking, such as allowing only an initial check followed by user-triggered updates rather than automatic updates on text changes.
In extreme cases (with persistent 429 errors), you might want to implement a "cooldown" period after a certain number of retries before allowing users to make new requests.
By implementing these changes, you should see an improvement in how your application handles requests to YouTube and reduce the frequency of "Too Many Requests" errors.
import random # Импортируем random для джиттера
class VideoInfoThread(QThread):
info_signal = pyqtSignal(str)
error_signal = pyqtSignal(str)
def __init__(self, url):
super().__init__()
self.url = url
def run(self):
retry_count = 0
while retry_count < 5: # Разрешить максимум 5 попыток
try:
yt = YouTube(self.url)
title = yt.title
self.info_signal.emit(title)
return # Выход из цикла, если успешно
except Exception as e:
if "429" in str(e): # Проверяем наличие ошибки 429
retry_count += 1
wait_time = (2 ** retry_count) + random.uniform(0, 1) # Экспоненциальное ожидание с джиттером
logging.warning(f'HTTP 429: Слишком много запросов. Повторная попытка через {wait_time:.2f} секунд...')
time.sleep(wait_time) # Ждем перед повторной попыткой
else:
self.error_signal.emit(f'Ошибка: {e}')
return
self.error_signal.emit('Не удалось получить информацию о видео после нескольких попыток.')
class DownloadThread(QThread):
# ... [Неизменённые части кода выше] ...
def run(self):
retry_count = 0
while retry_count < 5:
try:
yt = YouTube(self.url, on_progress_callback=self.on_progress)
# ... [Неизменённые части кода выше] ...
except exceptions.PytubeError as e:
self.error_signal.emit(f'Ошибка Pytube: {e}')
return
except Exception as e:
if "429" in str(e):
retry_count += 1
wait_time = (2 ** retry_count) + random.uniform(0, 1) # Экспоненциальное ожидание с джиттером
logging.warning(f'HTTP 429: Слишком много запросов. Повторная попытка через {wait_time:.2f} секунд...')
time.sleep(wait_time) # Ждем перед повторной попыткой
else:
self.error_signal.emit(f'Ошибка: {e}')
return
self.error_signal.emit('Не удалось загрузить видео после нескольких попыток.')
# Остальная часть вашего класса YouTubeDownloader остается без изменений
Настройка Времени Ожидания
Чтобы минимизировать количество запросов при вводе URL, вы можете увеличить время ожидания в методе update_video_info_debounced:
python
def update_video_info_debounced(self):
if self.debounce_timer.isActive():
self.debounce_timer.stop()
self.debounce_timer.start(1000) # Увеличьте до 1000 мс (1 секунды)
Окончательные Рекомендации:
Наблюдайте за поведением приложения, особенно за частотой запросов к YouTube.
Если проблемы сохраняются, подумайте о том, чтобы добавить "период охлаждения" после определенного числа неудачных попыток, прежде чем позволять пользователям отправлять новые запросы.
Избегайте автоматической проверки информации о видео по каждой букве, вместо этого предоставьте пользователям возможность делать это по своему запросу.
Внедряя эти изменения, вы должны увидеть улучшение в том, как ваше приложение обрабатывает запросы к YouTube и уменьшить количество ошибок "Слишком много запросов". Если у вас есть дополнительные вопросы или нужна дополнительная помощь, дайте знать!
import sys
import datetime
import logging
import time
import random
from PyQt6.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QLineEdit, QPushButton, QLabel,
QComboBox, QFileDialog, QMessageBox, QProgressBar, QTextEdit, QProgressDialog
)
from PyQt6.QtCore import Qt, QThread, pyqtSignal, QTimer
from pytube import YouTube, exceptions
# Настройка логирования
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
class LogHandler(logging.Handler):
def __init__(self, log_widget):
super().__init__()
self.log_widget = log_widget
def emit(self, record):
msg = self.format(record)
self.log_widget.append(msg)
class VideoInfoThread(QThread):
info_signal = pyqtSignal(str)
error_signal = pyqtSignal(str)
def __init__(self, url):
super().__init__()
self.url = url
def run(self):
retry_count = 0
while retry_count < 5: # Разрешить максимум 5 попыток
try:
yt = YouTube(self.url)
title = yt.title
self.info_signal.emit(title)
return # Выход из цикла, если успешно
except Exception as e:
if "429" in str(e): # Проверяем наличие ошибки 429
retry_count += 1
wait_time = (2 ** retry_count) + random.uniform(0, 1) # Экспоненциальное ожидание с джиттером
logging.warning(f'HTTP 429: Слишком много запросов. Повторная попытка через {wait_time:.2f} секунд...')
time.sleep(wait_time) # Ждем перед повторной попыткой
else:
self.error_signal.emit(f'Ошибка: {e}')
return
self.error_signal.emit('Не удалось получить информацию о видео после нескольких попыток.')
class DownloadThread(QThread):
progress_signal = pyqtSignal(int)
speed_signal = pyqtSignal(str)
time_signal = pyqtSignal(str)
error_signal = pyqtSignal(str)
finished_signal = pyqtSignal()
def __init__(self, url, quality, path):
super().__init__()
self.url = url
self.quality = quality
self.path = path
self._is_running = True
def run(self):
retry_count = 0
while retry_count < 5:
try:
yt = YouTube(self.url, on_progress_callback=self.on_progress)
if self.quality == 'Выберите качество':
stream = yt.streams.get_highest_resolution()
else:
stream = yt.streams.filter(res=self.quality).first()
if stream is None:
self.error_signal.emit(f'Не удалось найти видео с качеством {self.quality}')
return
stream.download(self.path)
self.finished_signal.emit()
return # Выход из цикла, если успешно
except exceptions.PytubeError as e:
self.error_signal.emit(f'Ошибка Pytube: {e}')
return
except Exception as e:
if "429" in str(e):
retry_count += 1
wait_time = (2 ** retry_count) + random.uniform(0, 1) # Экспоненциальное ожидание с джиттером
logging.warning(f'HTTP 429: Слишком много запросов. Повторная попытка через {wait_time:.2f} секунд...')
time.sleep(wait_time) # Ждем перед повторной попыткой
else:
self.error_signal.emit(f'Ошибка: {e}')
return
self.error_signal.emit('Не удалось загрузить видео после нескольких попыток.')
def on_progress(self, stream, chunk, bytes_remaining):
if not self._is_running:
return
total_size = stream.filesize
bytes_downloaded = total_size - bytes_remaining
percentage = (bytes_downloaded / total_size) * 100
self.progress_signal.emit(int(percentage))
speed = bytes_downloaded / (self.elapsed_time() or 1)
speed_str = f"{speed / (1024 * 1024):.2f} MB/s"
self.speed_signal.emit(speed_str)
time_remaining = bytes_remaining / speed if speed > 0 else 0
time_str = f"{time_remaining / 60:.2f} мин"
self.time_signal.emit(time_str)
def elapsed_time(self):
return (datetime.datetime.now() - self.start_time).total_seconds()
def start(self):
self.start_time = datetime.datetime.now()
super().start()
def stop(self):
self._is_running = False
class YouTubeDownloader(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.debounce_timer = QTimer(self)
self.debounce_timer.timeout.connect(self.on_debounce_timeout)
self.last_url = None
def initUI(self):
self.setWindowTitle('YouTube Downloader')
self.setGeometry(100, 100, 800, 600)
layout = QVBoxLayout()
# URL input
self.url_input = QLineEdit(self)
self.url_input.setPlaceholderText('Введите URL видео с YouTube')
self.url_input.textChanged.connect(self.update_video_info_debounced)
layout.addWidget(self.url_input)
# Title Label
self.title_label = QLabel(self)
layout.addWidget(self.title_label)
# Quality selection
self.quality_combo = QComboBox(self)
self.quality_combo.addItems(['Выберите качество'])
layout.addWidget(self.quality_combo)
# Path selection
self.path_button = QPushButton('Выбрать путь сохранения', self)
self.path_button.clicked.connect(self.select_path)
layout.addWidget(self.path_button)
self.path_label = QLabel(self)
self.path_label.setText('Путь для сохранения: ')
layout.addWidget(self.path_label)
# Download button
self.download_button = QPushButton('Начать загрузку', self)
self.download_button.clicked.connect(self.start_download)
layout.addWidget(self.download_button)
# Cancel download button
self.cancel_button = QPushButton('Отмена загрузки', self)
self.cancel_button.clicked.connect(self.cancel_download)
layout.addWidget(self.cancel_button)
# Progress bar
self.progress_bar = QProgressBar(self)
layout.addWidget(self.progress_bar)
# Speed and time labels
self.speed_label = QLabel(self)
self.speed_label.setText('Скорость: ')
layout.addWidget(self.speed_label)
self.time_label = QLabel(self)
self.time_label.setText('Оставшееся время: ')
layout.addWidget(self.time_label)
# Log output
self.log_output = QTextEdit(self)
self.log_output.setReadOnly(True)
layout.addWidget(self.log_output)
self.setLayout(layout)
# Apply CSS
self.setStyleSheet("""
QWidget {
font-family: 'Arial', sans-serif;
font-size: 14px;
background-color: #f0f0f0;
}
QLineEdit, QComboBox {
padding: 10px;
margin: 10px;
border: 2px solid #ccc;
border-radius: 5px;
background-color: #fff;
color: #333;
}
QLineEdit:focus, QComboBox:focus {
border-color: #e74c3c;
}
QPushButton {
padding: 10px 20px;
margin: 10px;
border: none;
border-radius: 5px;
background-color: #e74c3c;
color: #fff;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 1px;
}
QPushButton:hover {
background-color: #c0392b;
}
QLabel {
padding: 10px;
color: #333;
}
QProgressBar {
border: 2px solid #ccc;
border-radius: 5px;
text-align: center;
background-color: #fff;
}
QProgressBar::chunk {
background-color: #e74c3c;
border-radius: 5px;
}
QTextEdit {
border: 2px solid #ccc;
border-radius: 5px;
background-color: #fff;
color: #333;
padding: 10px;
}
""")
# Настройка логирования в UI
log_handler = LogHandler(self.log_output)
logging.getLogger().addHandler(log_handler)
def update_video_info_debounced(self):
if self.debounce_timer.isActive():
self.debounce_timer.stop()
self.debounce_timer.start(1000) # Увеличьте до 1000 мс (1 секунды)
def on_debounce_timeout(self):
url = self.url_input.text()
if url and url != self.last_url:
self.last_url = url
self.title_label.clear()
self.video_info_thread = VideoInfoThread(url)
self.video_info_thread.info_signal.connect(self.set_video_info)
self.video_info_thread.error_signal.connect(self.set_error_info)
self.video_info_thread.start()
else:
self.title_label.clear()
def set_video_info(self, title):
self.title_label.setText(f'Название: {title}')
def set_error_info(self, error_message):
logging.error(error_message)
QMessageBox.warning(self, 'Ошибка', error_message)
def select_path(self):
path = QFileDialog.getExistingDirectory(self, "Выберите папку для сохранения")
if path:
self.path_label.setText(f'Путь для сохранения: {path}')
def start_download(self):
url = self.url_input.text()
quality = self.quality_combo.currentText()
path = self.path_label.text().replace('Путь для сохранения: ', '')
if not url or not path:
QMessageBox.warning(self, 'Ошибка', 'Пожалуйста, заполните все поля')
return
try:
yt = YouTube(url)
available_qualities = [stream.resolution for stream in yt.streams.filter(progressive=True, file_extension='mp4')]
if not available_qualities:
QMessageBox.warning(self, 'Ошибка', 'Нет доступных потоков для этого видео')
return
self.quality_combo.clear()
self.quality_combo.addItems(['Выберите качество'] + available_qualities)
if quality == 'Выберите качество':
stream = yt.streams.get_highest_resolution()
else:
stream = yt.streams.filter(res=quality, progressive=True, file_extension='mp4').first()
if not stream:
QMessageBox.warning(self, 'Ошибка', f'Выбранное качество {quality} недоступно для этого видео')
return
self.download_thread = DownloadThread(url, quality, path)
self.download_thread.progress_signal.connect(self.update_progress)
self.download_thread.speed_signal.connect(self.update_speed)
self.download_thread.time_signal.connect(self.update_time)
self.download_thread.error_signal.connect(self.handle_download_error)
self.download_thread.finished_signal.connect(self.download_finished)
self.download_thread.start()
self.progress_dialog = QProgressDialog("Загрузка видео...", "Отмена", 0, 100, self)
self.progress_dialog.setWindowModality(Qt.WindowModality.WindowModal)
self.progress_dialog.setAutoClose(False)
self.progress_dialog.setAutoReset(False)
self.progress_dialog.canceled.connect(self.cancel_download)
self.progress_dialog.setValue(0)
except Exception as e:
self.log_output.append(f'Ошибка при загрузке видео: {e}')
QMessageBox.warning(self, 'Ошибка', f'Произошла ошибка: {e}')
def update_progress(self, value):
logging.debug(f'Обновление прогресса: {value}%')
self.progress_bar.setValue(value)
self.progress_dialog.setValue(value)
def update_speed(self, speed):
logging.debug(f'Обновление скорости: {speed}')
self.speed_label.setText(f'Скорость: {speed}')
def update_time(self, time):
logging.debug(f'Обновление оставшегося времени: {time}')
self.time_label.setText(f'Оставшееся время: {time}')
def handle_download_error(self, error_message):
self.log_output.append(error_message)
QMessageBox.warning(self, 'Ошибка', error_message)
def download_finished(self):
self.log_output.append('Видео успешно скачано!')
QMessageBox.information(self, 'Успех', 'Видео успешно скачано!')
self.progress_dialog.reset()
def cancel_download(self):
if hasattr(self, 'download_thread') and self.download_thread.isRunning():
self.download_thread.stop()
self.download_thread.quit()
self.download_thread.wait()
self.log_output.append('Загрузка отменена пользователем.')
def cleanup_threads(self):
if hasattr(self, 'video_info_thread') and self.video_info_thread.isRunning():
self.video_info_thread.quit()
self.video_info_thread.wait()
if hasattr(self, 'download_thread') and self.download_thread.isRunning():
self.download_thread.stop()
self.download_thread.quit()
self.download_thread.wait()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = YouTubeDownloader()
ex.show()
app.aboutToQuit.connect(ex.cleanup_threads)
sys.exit(app.exec())
Ключевые Изменения
Улучшенная Обработка Ошибок 429: Я добавил рандомизированный джиттер к времени ожидания при повторной попытке запроса, что уменьшает вероятность повторного столкновения с ограничениями YouTube.
Увеличение Времени Ожидания (Debounce): Время ожидания для проверки информации о видео увеличено до 1000 мс (1 секунды), чтобы уменьшить количество запросов к YouTube при вводе текста.
Управление Графическим Интерфейсом: В интерфейсе все элементы остались без изменений, он по-прежнему прост и понятен для пользователя.
Установка и Запуск Код
Убедитесь, что у вас установлены необходимые библиотеки:
PyQt6 и pytube (можно установить через pip):
bash
pip install PyQt6 pytube
Сохраните код в файл (например, youtube_downloader.py).
Запустите файл в вашей среде Python:
bash
python youtube_downloader.py
Этот код должен улучшить надежность работы вашего приложения загрузки видео с YouTube и уменьшить количество возникновений ошибок HTTP 429. Если у вас есть дополнительные вопросы или нужны доработки, пожалуйста, дайте знать!