-
Notifications
You must be signed in to change notification settings - Fork 0
/
myDownload.py
executable file
·234 lines (196 loc) · 8.89 KB
/
myDownload.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env python3
from PyQt5.QtCore import QDir, QFile, QFileInfo, QIODevice, QUrl
from PyQt5.QtWidgets import (QApplication, QDialog, QDialogButtonBox,
QHBoxLayout, QLabel, QLineEdit, QMessageBox, QProgressDialog,
QPushButton, QVBoxLayout, QAction, QMainWindow, QTextEdit, QSizePolicy)
from PyQt5.QtGui import QIcon, QTextCursor
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(600, 400)
sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.MainWindow.sizePolicy().hasHeightForWidth())
self.MainWindow.setSizePolicy(sizePolicy)
class HttpWindow(QMainWindow):
def __init__(self, parent=None):
super(HttpWindow, self).__init__(parent)
self.title = "OraDocs by littleT"
self.left = 10
self.top = 10
self.width = 600
self.height = 400
self.docRoot = "https://oradocs-corp.documents.us2.oraclecloud.com/documents/"
self.url = QUrl()
self.qnam = QNetworkAccessManager()
self.reply = None
self.outFile = None
self.httpGetId = 0
self.httpRequestAborted = False
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.statusBar().showMessage('Starting ...')
# for the menu first create some actions to be added like
# File - Exit action
exitAction = QAction(QIcon('exit24.png'), 'Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit Application')
exitAction.triggered.connect(self.close)
# File - open
openAction = QAction(QIcon(''), '&Open', self)
openAction.setShortcut('Ctrl+O')
openAction.setStatusTip('Opens a file')
openAction.triggered.connect(self.openCall)
# the help actions
helpAction = QAction(QIcon(''), 'Help', self)
helpAction.triggered.connect(self.openHelp)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(openAction)
fileMenu.addAction(exitAction)
helpMenu = mainMenu.addMenu('&Help')
helpMenu.addAction(helpAction)
self.urlLineEdit = QLineEdit(self)
self.urlLineEdit.setGeometry(5, 250, 590, 20)
self.urlLineEdit.setText(self.docRoot)
self.downloadButton = QPushButton('Click Me', self)
self.downloadButton.move(10, 275)
self.downloadButton.clicked.connect(self.onDownloadClick)
self.quitButton = QPushButton("Quit", self)
self.quitButton.setAutoDefault(False)
self.quitButton.clicked.connect(self.close)
self.quitButton.move(120, 275)
self.logOutput = QTextEdit(self)
self.logOutput.setReadOnly(True)
# self.logOutput.setLineWrapMode(QTextEdit.NoWrap)
self.logOutput.setGeometry(5, 320, 590, 60)
self.logOutput.setSizePolicy(self.sizePolicy)
self.statusBar().showMessage('waiting for click ...')
def openCall(self):
QMessageBox.information(self, "Info",
"File Open Menu, work in progress")
def openHelp(self):
QMessageBox.information(self, "Info",
"Help Menu, work in progress")
def onDownloadClick(self):
self.url = QUrl(self.docRoot)
self.logOutput.insertPlainText("onclick before startReq -> url %s\n"
% self.url.toString())
# start the request
self.startRequest(self.url)
self.logOutput.insertPlainText("onclick after startReq -> url %s\n"
% self.url.toString())
# do we need authorization
self.qnam.authenticationRequired.connect(
self.slotAuthenticationRequired)
# do we go ssl errors
self.qnam.sslErrors.connect(self.sslErrors)
def httpFinished(self):
self.logOutput.insertPlainText("httpFinished ...\n")
self.logOutput.moveCursor(QTextCursor.End)
if self.httpRequestAborted:
# if self.outFile is not None:
# self.outFile.close()
# self.outFile.remove()
# self.outFile = None
self.logOutput.insertPlainText("httpFinished, httpReqAbr ...\n")
self.logOutput.moveCursor(QTextCursor.End)
self.reply.deleteLater()
# self.outFile.flush()
# self.outFile.close()
return
# maybe updating progress or so
# self.progressDialog.hide()
# self.outFile.flush()
# self.outFile.close()
# check for redirection
redirectionTarget = self.reply.attribute(
QNetworkRequest.RedirectionTargetAttribute)
if self.reply.error():
# are we got an error
# self.outFile.remove()
QMessageBox.information(self, "HTTP", "Download failed %s"
% self.reply.errorString)
elif redirectionTarget is not None:
# self.logOutput.insertPlainText("httpFinished, redirect from %s\n"
# % str(self.url))
self.logOutput.moveCursor(QTextCursor.End)
# do we got redirected
newUrl = self.url.resolved(redirectionTarget)
self.logOutput.insertPlainText("httpFinished, redirect \n")
# % newUrl.toString())
self.logOutput.moveCursor(QTextCursor.End)
#ret = QMessageBox.question(self, "HTTP", "Redirect to %s." %
# newUrl.toString(),
# QMessageBox.Yes | QMessageBox.No)
#if ret == QMessageBox.Yes:
# # if we saied yes to redirect set the new URL to redirected one
# self.url = newUrl
# # the reply needs to be nothing
# self.reply = None
# # self.outFile.open(QIODevice.WriteOnly)
# # self.outFile.resize(0)
# # and start over
# self.startRequest(self.url)
# return
# the new url is the redirected one
self.url = newUrl
# the reply needs to be nothing
self.reply = None
# and start over
self.startRequest(self.url)
return
else:
# fileName = QFileInfo(QUrl(self.urlLineEdit.text()).path()).fileName()
# self.statusLabel.setText("Downloaded %s to %s." % (fileName, QDir.currentPath()))
# self.statusLabel.setText("Done")
self.statusBar().showMessage("Done")
self.reply.deleteLater()
self.reply = None
# self.outFile = None
def startRequest(self, url):
self.logOutput.insertPlainText("startRequest ...\n")
self.logOutput.moveCursor(QTextCursor.End)
# get the requested page
self.reply = self.qnam.get(QNetworkRequest(url))
# if we are finished
self.reply.finished.connect(self.httpFinished)
# are we ready to read the stuff
# self.reply.readyRead.connect(self.httpReadyRead)
# downloading
# self.reply.downloadProgress.connect(self.updateDataReadProgress)
def slotAuthenticationRequired(self, authenticator):
import os
from PyQt5 import uic
self.logOutput.insertPlainText("Authentication ...\n")
self.logOutput.moveCursor(QTextCursor.End)
self.logOutput.moveCursor(QTextCursor.End)
self.logOutput.insertPlainText(str(authenticator))
ui = os.path.join(os.path.dirname(__file__), 'authenticationdialog.ui')
self.logOutput.moveCursor(QTextCursor.End)
self.logOutput.insertPlainText(str(ui))
dlg = uic.loadUi(ui)
dlg.adjustSize()
dlg.siteDescription.setText("%s at %s" % (authenticator.realm(), self.url.host()))
dlg.userEdit.setText(self.url.userName())
dlg.passwordEdit.setText(self.url.password())
if dlg.exec_() == QDialog.Accepted:
authenticator.setUser(dlg.userEdit.text())
authenticator.setPassword(dlg.passwordEdit.text())
def sslErrors(self, reply, errors):
errorString = ", ".join([str(error.errorString()) for error in errors])
ret = QMessageBox.warning(self, "HTTP Example",
"One or more SSL errors has occurred: %s" % errorString,
QMessageBox.Ignore | QMessageBox.Abort)
if ret == QMessageBox.Ignore:
self.reply.ignoreSslErrors()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
httpWin = HttpWindow()
httpWin.show()
sys.exit(app.exec_())