-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
153 lines (111 loc) · 4.46 KB
/
main.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
# This Python file uses the following encoding: utf-8
from sounds import Sounds
import json
import os
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QApplication, QDialog, QFileDialog, QMessageBox, QLineEdit
from PyQt4.QtCore import QObject, pyqtSlot, pyqtSignal, QThread
from gui import Ui_Form
from dialog import Ui_Status
#pyuic4 -x sounds.ui -o gui.py
#link gui
#http://stackoverflow.com/a/9526625/3590748
#use code
#http://stackoverflow.com/questions/15362624/how-do-you-execute-pyqt-ui-code-in-python
#url to test resolver
# #http://api.soundcloud.com/resolve.json?url=https://soundcloud.com/jamkins/sets/theabyss&client_id=c585c5f24b092caec68984885cf2b0db
def main():
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
class Window(QtGui.QWidget):
def __init__(self, directory="", url="", readyToGo = False):
QtGui.QWidget.__init__(self)
self.readyToGo = readyToGo
self.directory = directory
self.ui = Ui_Form()
self.ui.setupUi(self)
self.dialog = Dialog(self)
#connect QDialog finished slot, to see if pressed OK or CANCEL
self.dialog.finished.connect(self.dialogFinished)
def getDir(self):
directory = QFileDialog.getExistingDirectory()
directory = str(directory)
print directory
print type(directory)
self.directory = directory
self.ui.dirLine.setText(self.directory)
def urlPasted(self, QString):
string = str(QString)
if "https://soundcloud.com" in string:
self.url = string
self.readyToGo = True
print self.url
return self.url
def downloadButton(self):
if self.readyToGo and self.directory:
self.dialog.show()
sound = Sounds(self.url, self.directory)
# #set as read only
self.dialog.ui.statusTextEdit.setReadOnly(True)
self.dialog.ui.statusTextEdit.insertPlainText("Loading data...")
# status = sound.download(self.dialog_ui)
self.workerThread = WorkerThread(sound, self.dialog)
#connect finished() signal to my own slot
self.workerThread.finished.connect(self.downloadDone)
self.workerThread.start()
#show error
else:
msgBox = QMessageBox()
msgBox.setWindowTitle("error")
msgBox.setText("Please enter a valid SoundCloud url or directory ya noob.")
msgBox.setIcon(3)
msgBox.exec_()
def downloadDone(self):
print "we done boiz"
self.dialog.ui.statusTextEdit.appendPlainText("\n" + "Finished all downloads. ")
#if sound object had errors
num_errors = len(self.workerThread.sound.errors)
if num_errors > 0:
error_msg = "But there were " + str(num_errors) + " error(s)." + "\n" + "Check the errors.txt file" + "\n"
self.dialog.ui.statusTextEdit.appendPlainText(error_msg)
f = open('errors.txt', 'wb')
for error in self.workerThread.sound.errors:
f.write(error["title"].decode('utf-8') +" -> " + error["permalink_url"] + "\n")
f.write(error["error"])
f.write("\n\n")
#after hit "OK" clear dialog statusTextEdit and clear url
def dialogFinished(self, value):
#the finished slot returns 1 for OK
#and 0 for cancel
if value == 1:
#reset url values
self.ui.lineEdit.clear()
self.url = ""
#clear dialog
self.dialog.ui.statusTextEdit.clear()
class Dialog (QDialog):
statusUpdate = pyqtSignal(str)
def __init__(self, parent = None):
self.parent = parent
QDialog.__init__(self, self.parent)
self.ui = Ui_Status()
self.ui.setupUi(self)
#stauts update signal
# Connect the trigger signal to a slot.
self.statusUpdate.connect(self.updateStatus)
def updateStatus(self, string):
self.ui.statusTextEdit.appendPlainText(string)
class WorkerThread(QThread):
def __init__(self, sound = None, dialog = None, parent = None):
super(WorkerThread, self).__init__(parent)
#sound class instance
#and dialog instance
self.sound = sound
self.dialog = dialog
def run(self):
self.sound.download(self.dialog)
if __name__ == "__main__":
main()