|
| 1 | +from PyQt5.QtWidgets import (QWidget, QLineEdit, QPushButton, QSystemTrayIcon |
| 2 | + QFileDialog, QFormLayout, QApplication) |
| 3 | +from PyQt5.QtCore import QThread, QRegExp |
| 4 | +import requests |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +class DownloaderMusic(QThread): |
| 9 | + def __init__(self,name,url,path): |
| 10 | + super(DownloaderMusic,self).__init__() |
| 11 | + self.path = path |
| 12 | + self.url = url |
| 13 | + self.name = name |
| 14 | + |
| 15 | + def run(self): |
| 16 | + mescl = self.path+"/"+self.name |
| 17 | + with open(mescl+'.mp3', 'wb') as f: |
| 18 | + f.write(requests.get(self.url).content) |
| 19 | + |
| 20 | + |
| 21 | +class Main(QWidget): |
| 22 | + def __init__(self, parent=None): |
| 23 | + super(Main, self).__init__() |
| 24 | + self.path = None |
| 25 | + self.settings() |
| 26 | + self.create_widgets() |
| 27 | + self.create_layout() |
| 28 | + |
| 29 | + def settings(self): |
| 30 | + self.resize(300,120) |
| 31 | + self.setWindowTitle("Mp3 Downloader") |
| 32 | + |
| 33 | + def create_widgets(self): |
| 34 | + self.edit_url = QLineEdit() |
| 35 | + self.edit_name = QLineEdit() |
| 36 | + self.btn_select_path = QPushButton("Select path",self) |
| 37 | + self.btn_select_path.clicked.connect(self.select_path) |
| 38 | + self.btn_down = QPushButton("Download mp3",self) |
| 39 | + self.btn_down.clicked.connect(self.download) |
| 40 | + |
| 41 | + def create_layout(self): |
| 42 | + self.layout = QFormLayout() |
| 43 | + self.layout.addRow("Nome:",self.edit_name) |
| 44 | + self.layout.addRow("Url:",self.edit_url) |
| 45 | + self.layout.addRow("Selecionar destino:",self.btn_select_path) |
| 46 | + self.layout.addRow(self.btn_down) |
| 47 | + self.setLayout(self.layout) |
| 48 | + |
| 49 | + def select_path(self): |
| 50 | + self.path = QFileDialog.getExistingDirectory(self,"Selecionar Pasta") |
| 51 | + |
| 52 | + def download(self): |
| 53 | + if self.verify_fields(): |
| 54 | + self.manage_interface(False) |
| 55 | + self.thread_qt() |
| 56 | + |
| 57 | + def thread_qt(self): |
| 58 | + url = self.edit_url.text() |
| 59 | + name = self.edit_name.text() |
| 60 | + path = self.path |
| 61 | + self.thre = DownloaderMusic(name, url, path) |
| 62 | + self.thre.finished.connect(self.downfin) |
| 63 | + self.thre.start() |
| 64 | + |
| 65 | + def manage_interface(self,state): |
| 66 | + self.btn_down.setEnabled(state) |
| 67 | + self.edit_name.setEnabled(state) |
| 68 | + self.edit_url.setEnabled(state) |
| 69 | + self.btn_select_path.setEnabled(state) |
| 70 | + |
| 71 | + def verify_fields(self): |
| 72 | + if self.path == None: |
| 73 | + return False |
| 74 | + else: |
| 75 | + strings = [self.edit_url.text(),self.edit_name.text(), self.path] |
| 76 | + regex_validate = QRegExp("*.mp3") |
| 77 | + regex_validate.setPatternSyntax(QRegExp.Wildcard) |
| 78 | + emptys = 0 |
| 79 | + for string in strings: |
| 80 | + if len(string.split()) == 0: |
| 81 | + emptys+=1 |
| 82 | + if emptys == 0 and regex_validate.exactMatch(self.editUrl.text()): |
| 83 | + return True |
| 84 | + |
| 85 | + def downfin(self): |
| 86 | + self.notifyIcon = QSystemTrayIcon() |
| 87 | + self.notifyIcon.setVisible(True) |
| 88 | + self.notifyIcon.showMessage( |
| 89 | + "Download Finalizado", |
| 90 | + u"O download da sua música foi realizado com sucesso.", |
| 91 | + QSystemTrayIcon.Information,3000) |
| 92 | + self.manageInterface(True) |
| 93 | + |
| 94 | +root = QApplication([]) |
| 95 | +app = Main() |
| 96 | +app.show() |
| 97 | +sys.exit(root.exec_()) |
0 commit comments