-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
153 lines (125 loc) · 6.28 KB
/
index.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QLineEdit, QPushButton, QVBoxLayout, QWidget, QMessageBox, QComboBox, QDesktopWidget, QFileDialog, QMenuBar, QAction
from PyQt5.QtCore import QFile, QTextStream, Qt, QSettings
from pytube import YouTube
import os
import json
class YouTubeDownloaderApp(QMainWindow):
def __init__(self):
super().__init__()
self.settings = QSettings("MyApp", "YouTubeDownloader")
self.messages = {}
self.init_ui()
self.load_style_sheet()
self.center_window()
self.setWindowFlag(Qt.WindowMaximizeButtonHint, False)
self.create_menu()
self.load_language_setting()
def init_ui(self):
self.setWindowTitle("")
self.setGeometry(0, 0, 400, 250)
self.label = QLabel("", self)
self.link_input = QLineEdit(self)
self.format_label = QLabel("", self)
self.format_video_combo = QComboBox(self)
self.format_audio_label = QLabel("", self)
self.format_audio_combo = QComboBox(self)
self.format_video_combo.addItems(["Seleccionar...", "MP4", "MKV"])
self.format_audio_combo.addItems(["Seleccionar...", "MP3"])
self.download_button = QPushButton("", self)
self.download_button.clicked.connect(self.download_video)
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(self.link_input)
layout.addWidget(self.format_label)
layout.addWidget(self.format_video_combo)
layout.addWidget(self.format_audio_label)
layout.addWidget(self.format_audio_combo)
layout.addWidget(self.download_button)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
def load_style_sheet(self):
style_sheet = QFile("style.css")
if style_sheet.open(QFile.ReadOnly | QFile.Text):
stream = QTextStream(style_sheet)
self.setStyleSheet(stream.readAll())
def center_window(self):
screen_geometry = QDesktopWidget().screenGeometry()
x = (screen_geometry.width() - self.width()) // 2
y = (screen_geometry.height() - self.height()) // 2
self.move(int(x), int(y))
def load_messages(self, file_name):
try:
with open(file_name, 'r', encoding='utf-8') as file:
return json.load(file)
except FileNotFoundError:
print(f"Archivo de idioma '{file_name}' no encontrado.")
return {}
def create_menu(self):
menu_bar = self.menuBar()
config_menu = menu_bar.addMenu('Lenguages')
self.english_action = QAction('English', self)
self.english_action.triggered.connect(lambda: self.change_language_to('languages/messages_en.json'))
config_menu.addAction(self.english_action)
self.spanish_action = QAction('Spanish', self)
self.spanish_action.triggered.connect(lambda: self.change_language_to('languages/messages_es.json'))
config_menu.addAction(self.spanish_action)
self.japan_action = QAction('Japan', self)
self.japan_action.triggered.connect(lambda: self.change_language_to('languages/messages_jp.json'))
config_menu.addAction(self.japan_action)
def change_language_to(self, language_file):
self.save_language_setting(language_file)
self.load_language_setting()
def change_language(self):
self.setWindowTitle(self.messages.get('title', ''))
self.label.setText(self.messages.get('enter_link', ''))
self.format_label.setText(self.messages.get('select_video_format', ''))
self.format_audio_label.setText(self.messages.get('select_audio_format', ''))
self.download_button.setText(self.messages.get('download_button', ''))
def download_video(self):
video_url = self.link_input.text()
download_video_format = self.format_video_combo.currentText()
download_audio_format = self.format_audio_combo.currentText()
try:
if download_video_format == "Seleccionar..." and download_audio_format == "Seleccionar...":
QMessageBox.warning(self, 'Error', self.messages['select_at_least_one_format'])
return
yt = YouTube(video_url)
video = None
audio = None
if download_video_format == "MP4":
video = yt.streams.get_highest_resolution()
elif download_video_format == "MKV":
video = yt.streams.filter(file_extension='webm').first()
if download_audio_format == "MP3":
audio = yt.streams.filter(only_audio=True).first()
if video or audio:
default_location = os.path.join(os.path.expanduser("~"), "Desktop")
save_location, _ = QFileDialog.getSaveFileName(self, self.messages['download_button'], default_location)
if save_location:
if video:
video.download(output_path=os.path.dirname(save_location))
elif audio:
audio.download(output_path=os.path.dirname(save_location))
QMessageBox.information(self, self.messages['download_completed'], self.messages['file_downloaded_success'])
else:
QMessageBox.warning(self, 'Error', self.messages['select_valid_location'])
else:
QMessageBox.warning(self, 'Error', self.messages['file_not_found'])
except Exception as e:
error_message = self.messages['error_download'].format(error_message=str(e))
QMessageBox.critical(self, 'Error', error_message)
def save_language_setting(self, language_file):
self.settings.setValue("language", language_file)
def load_language_setting(self):
language = self.settings.value("language", "languages/messages_en.json")
self.messages = self.load_messages(language)
self.change_language()
def run_app():
app = QApplication(sys.argv)
downloader_app = YouTubeDownloaderApp()
downloader_app.show()
sys.exit(app.exec_())
if __name__ == '__main__':
run_app()