# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.
import sys

from PyQt5 import QtCore, QtGui, QtWidgets
import cloudmusic
import os


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        MainWindow.setMaximumSize(QtCore.QSize(800, 600))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        MainWindow.setFont(font)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("icon/logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        MainWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
        MainWindow.setIconSize(QtCore.QSize(30, 30))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label_Banner = QtWidgets.QLabel(self.centralwidget)
        self.label_Banner.setGeometry(QtCore.QRect(0, 0, 801, 211))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        font.setBold(False)
        font.setWeight(50)
        self.label_Banner.setFont(font)
        self.label_Banner.setAcceptDrops(False)
        self.label_Banner.setText("")
        self.label_Banner.setPixmap(QtGui.QPixmap("icon/网易云音乐-01.png"))
        self.label_Banner.setScaledContents(False)
        self.label_Banner.setAlignment(QtCore.Qt.AlignCenter)
        self.label_Banner.setObjectName("label_Banner")
        self.pushButton_download = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_download.setGeometry(QtCore.QRect(680, 220, 71, 41))
        self.pushButton_download.setText("")
        self.pushButton_download.setIcon(icon)
        self.pushButton_download.setIconSize(QtCore.QSize(36, 36))
        self.pushButton_download.setObjectName("pushButton_download")
        self.label_Music_Id = QtWidgets.QLabel(self.centralwidget)
        self.label_Music_Id.setGeometry(QtCore.QRect(30, 220, 171, 41))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_Music_Id.setFont(font)
        self.label_Music_Id.setTextFormat(QtCore.Qt.AutoText)
        self.label_Music_Id.setObjectName("label_Music_Id")
        self.lineEdit_Music_Id = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_Music_Id.setGeometry(QtCore.QRect(220, 220, 431, 41))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit_Music_Id.setFont(font)
        self.lineEdit_Music_Id.setObjectName("lineEdit_Music_Id")
        self.pushButton_Select_Folder = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_Select_Folder.setGeometry(QtCore.QRect(680, 280, 71, 41))
        self.pushButton_Select_Folder.setText("")
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("icon/文件夹.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_Select_Folder.setIcon(icon1)
        self.pushButton_Select_Folder.setIconSize(QtCore.QSize(36, 36))
        self.pushButton_Select_Folder.setObjectName("pushButton_Select_Folder")
        self.label_Music_Info = QtWidgets.QLabel(self.centralwidget)
        self.label_Music_Info.setGeometry(QtCore.QRect(390, 340, 231, 41))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_Music_Info.setFont(font)
        self.label_Music_Info.setTextFormat(QtCore.Qt.AutoText)
        self.label_Music_Info.setObjectName("label_Music_Info")
        self.label_Music_Name = QtWidgets.QLabel(self.centralwidget)
        self.label_Music_Name.setGeometry(QtCore.QRect(290, 400, 101, 41))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_Music_Name.setFont(font)
        self.label_Music_Name.setTextFormat(QtCore.Qt.AutoText)
        self.label_Music_Name.setObjectName("label_Music_Name")
        self.label_Music_Singer = QtWidgets.QLabel(self.centralwidget)
        self.label_Music_Singer.setGeometry(QtCore.QRect(290, 460, 101, 41))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_Music_Singer.setFont(font)
        self.label_Music_Singer.setTextFormat(QtCore.Qt.AutoText)
        self.label_Music_Singer.setObjectName("label_Music_Singer")
        self.label_Music_Name_1 = QtWidgets.QLabel(self.centralwidget)
        self.label_Music_Name_1.setGeometry(QtCore.QRect(420, 400, 231, 41))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_Music_Name_1.setFont(font)
        self.label_Music_Name_1.setText("")
        self.label_Music_Name_1.setTextFormat(QtCore.Qt.AutoText)
        self.label_Music_Name_1.setObjectName("label_Music_Name_1")
        self.label_Music_Album_1 = QtWidgets.QLabel(self.centralwidget)
        self.label_Music_Album_1.setGeometry(QtCore.QRect(420, 520, 231, 41))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_Music_Album_1.setFont(font)
        self.label_Music_Album_1.setText("")
        self.label_Music_Album_1.setTextFormat(QtCore.Qt.AutoText)
        self.label_Music_Album_1.setObjectName("label_Music_Album_1")
        self.label_Music_Singer_1 = QtWidgets.QLabel(self.centralwidget)
        self.label_Music_Singer_1.setGeometry(QtCore.QRect(420, 460, 231, 41))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_Music_Singer_1.setFont(font)
        self.label_Music_Singer_1.setText("")
        self.label_Music_Singer_1.setTextFormat(QtCore.Qt.AutoText)
        self.label_Music_Singer_1.setObjectName("label_Music_Singer_1")
        self.label_Music_Album = QtWidgets.QLabel(self.centralwidget)
        self.label_Music_Album.setGeometry(QtCore.QRect(290, 520, 101, 41))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_Music_Album.setFont(font)
        self.label_Music_Album.setTextFormat(QtCore.Qt.AutoText)
        self.label_Music_Album.setObjectName("label_Music_Album")
        self.label_Software_Info = QtWidgets.QLabel(self.centralwidget)
        self.label_Software_Info.setGeometry(QtCore.QRect(20, 430, 241, 121))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_Software_Info.setFont(font)
        self.label_Software_Info.setAutoFillBackground(False)
        self.label_Software_Info.setTextFormat(QtCore.Qt.AutoText)
        self.label_Software_Info.setWordWrap(True)
        self.label_Software_Info.setOpenExternalLinks(False)
        self.label_Software_Info.setObjectName("label_Software_Info")
        self.label_Software_Info_1 = QtWidgets.QLabel(self.centralwidget)
        self.label_Software_Info_1.setGeometry(QtCore.QRect(90, 340, 101, 41))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_Software_Info_1.setFont(font)
        self.label_Software_Info_1.setTextFormat(QtCore.Qt.AutoText)
        self.label_Software_Info_1.setObjectName("label_Software_Info_1")
        self.lineEdit_Save_Directory = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_Save_Directory.setGeometry(QtCore.QRect(220, 280, 431, 41))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit_Save_Directory.setFont(font)
        self.lineEdit_Save_Directory.setObjectName("lineEdit_Save_Directory")
        self.label_Software_Info_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_Software_Info_2.setGeometry(QtCore.QRect(50, 280, 131, 41))
        font = QtGui.QFont()
        font.setFamily("Consolas")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.label_Software_Info_2.setFont(font)
        self.label_Software_Info_2.setTextFormat(QtCore.Qt.AutoText)
        self.label_Software_Info_2.setObjectName("label_Software_Info_2")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.pushButton_download.clicked.connect(self.StartDownload)
        self.pushButton_Select_Folder.clicked.connect(self.SelectFolder)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "网抑云下载"))
        self.label_Music_Id.setText(_translate("MainWindow", " 请输入歌曲ID:"))
        self.label_Music_Info.setText(_translate("MainWindow", " 该ID所对应的歌曲信息"))
        self.label_Music_Name.setText(_translate("MainWindow", " 歌曲名:"))
        self.label_Music_Singer.setText(_translate("MainWindow", " 歌手:"))
        self.label_Music_Album.setText(_translate("MainWindow", " 专辑:"))
        self.label_Software_Info.setText(_translate("MainWindow", "   本软件基于python库——cloudmusic与pyQT5打造，目前功能仅限于下载单个的网易云歌曲。"))
        self.label_Software_Info_1.setText(_translate("MainWindow", " 软件信息"))
        self.label_Software_Info_2.setText(_translate("MainWindow", " 保存文件夹:"))


    def StartDownload(self):
        # 找到文件夹位置
        flag = 0
        try:
            aim_position = "{0}".format(self.lineEdit_Save_Directory.text().replace("/", "\\"))
            os.chdir(aim_position)
        except Exception as err:
            QtWidgets.QMessageBox.warning(None, "文件夹错误", "请检查您的文件夹后再次输入!错误信息:{0}".format(err))

        # 下载音乐
        try:
            MusicId = self.lineEdit_Music_Id.text()
            music = cloudmusic.getMusic(MusicId)
            music.download(level="lossless")
            flag = 1
        except Exception as err:
            flag = 0
            QtWidgets.QMessageBox.warning(None, "MusicId错误", "请检查您的MusicId后再次输入!错误信息:{0}".format(err))


        if flag == 1:
            QtWidgets.QMessageBox.about(None, "下载完成", "文件下载完成，位置为:{0}\cloudmusic".format(aim_position))




        '''
        显示歌曲信息代码
        '''
        self.label_Music_Name_1.setText(str(music.name))
        self.label_Music_Singer_1.setText(str(music.artist).replace("['", " ").replace("']", " "))
        self.label_Music_Album_1.setText(str(music.album))

    def SelectFolder(self, Filepath):
        directory = QtWidgets.QFileDialog.getExistingDirectory(None, "选取文件夹", "C:/")  # 起始路径
        self.lineEdit_Save_Directory.setText(directory)





class Start(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


if __name__ == '__main__':
    # import pics_ui_rc # 导入添加的资源（根据实际情况填写文件名）
    app = QtWidgets.QApplication(sys.argv)
    window = Start()
    window.show()
    sys.exit(app.exec_())