This repository has been archived by the owner on Aug 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
184 lines (149 loc) · 5.69 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import os
import re
import sys
import urllib.request
from os import path
import pafy
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMainWindow, QFileDialog, QApplication, QMessageBox
from PyQt5.uic import loadUiType
FORM_CLASS, _ = loadUiType(path.join(path.dirname(__file__), "main.ui"))
class MainApp(QMainWindow, FORM_CLASS):
def __init__(self):
super(MainApp, self).__init__()
QMainWindow.__init__(self)
self.setupUi(self)
self.setWindowTitle("DM")
self.setWindowIcon(QIcon("Images\if_dashboard_basic_blue_69481.ico"))
self.handleButtons()
def handleUi(self):
pass
def handleButtons(self):
self.pushButton.clicked.connect(self.handleDownload)
self.actionExit.triggered.connect(self.handleDownload)
self.pushButton_3.clicked.connect(self.endMyLife)
self.actionExit_2.triggered.connect(self.endMyLife)
self.pushButton_2.clicked.connect(self.handleBrowse)
def downloadRequirementsCheck(self):
# TODO check link if standard check if it have a file not html get the file name or ask the user to provide
# i think we should use Requests lib for th e check
# TODO ask for location if the default not set then set
# check for dubs
pass
def handleBrowse(self):
select = QFileDialog.getExistingDirectory(self, "Select Download Directory")
self.plainTextEdit_2.setPlainText(select)
def setDefaultDownloadLocation(self):
# TODO should be set as the last used or downloads
pass
def handleProgressBar(self, blocknum, blocksize, totalsize):
read = blocknum * blocksize
if totalsize > 0:
percent = read * 100 / totalsize
self.progressBar.setValue(percent)
QApplication.processEvents()
def handleDownload(self):
url = self.getUrl()
path = self.getPath()
try:
if self.checkIfYouTube(url):
if "playlist" in url:
self.playlistDownload(url)
self.youtubeDownload(url,path)
else:
self.standardDownload(url, path)
except Exception:
QMessageBox.information(self, "ERROR", "Sorry, Check Your Internet Connection")
else :
QMessageBox.information(self, "Downloading", "Please WAIT")
def standardDownload(self, url, standardPath):
urllib.request.urlretrieve(url, standardPath, self.handleProgressBar)
def youtubeDownload(self, url,path):
v = pafy.new(url)
for s in v.allstreams:
size = s.get_filesize()
data = "{}{}{}{}".format(s.mediatype, s.extension, s.quality, size)
self.comboBox.addItem(data)
quality = self.comboBox.currentIndex()
v.allstreams[quality].download(filepath=path)
# TODO video name, ETA, Received Bytes, Speed
# def playlistDownload(self, url):
#
# os.chdir(path)
#
# if os.path.exists(str(playlist["title"])) :
# os.chdir(str(playlist["title"]))
# else:
# os.mkdir(str(playlist["title"]))
# os.chdir(str(playlist["title"]))
def videoDataRetrieve(self, url):
pass
# def playlistDataRetrieve(self, url):
# playlist = pafy.get_playlist(url)
def pauseContinue(self):
pass
def getUrl(self):
# TODO get url from clipboard
if self.plainTextEdit.toPlainText() == "":
self.plainTextEdit.setPlainText("TEST")
return "http://www.tutorialspoint.com/python/python_tutorial.pdf"
elif self.plainTextEdit.toPlainText() == "y":
self.plainTextEdit.setPlainText("YOUTUBE TEST")
return "https://youtube.com/watch?v=XJGiS83eQLk"
else:
return self.plainTextEdit.toPlainText()
def getPath(self):
if self.plainTextEdit_2.toPlainText() == "":
self.plainTextEdit_2.setPlainText("TEST")
downloads = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Downloads')
return downloads + "\\" + "test.pdf"
else:
return self.plainTextEdit_2.toPlainText()
# TODO takes a url ==> return file name
def getFileName(self):
pass
# remotefile = urlopen(url)
# blah = remotefile.info()['Content-Disposition']
# value, params = cgi.parse_header(blah)
# filename = params["filename"]
# urlretrieve(url, filename)
# return filename
def dublicatesCheck(self):
# check if the given dir exists and if the file already exists if it does check if it is complete or the same
# as the new one may offer multiple options to deal
try:
pass
except FileExistsError:
print("DUB")
pass
# gather errors and notify
def notifier(self):
pass
def downloadCompleted(self):
self.message()
self.startOver()
pass
def message(self):
QMessageBox.information(self, "Completed", "Done")
def endMyLife(self):
QCoreApplication.quit()
def startOver(self):
self.plainTextEdit.setPlainText("")
self.plainTextEdit_2.setPlainText("")
self.progressBar.setValue(0)
def checkIfYouTube(self, url):
youTube = re.compile(
"http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?[\w\?=]*)?")
if re.match(youTube, url):
# TODO check if a playlist
return True
else:
return False
def main():
app = QApplication(sys.argv)
window = MainApp()
window.show()
app.exec_()
if __name__ == "__main__":
main()