# -*- coding: utf-8 -*-
from PyQt4 import QtCore
from PyQt4.QtCore import QUrl, QFile, QIODevice, QObject, QThread, QEventLoop
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkCookieJar, QNetworkRequest, QNetworkReply
"""
QNAM library for downloading/uploading data.
Redirections will be handled automatically.
###TODO: Upload methods
###TODO: Be able to do GET and POST (maybe an option in methods)
###TODO: Handle errors
###Examples of usage are available at the end of this file.
"""
guserAgent = "Wget/1.12 (linux-gnu)"
guser = ""
gpassword = ""
"""
This class will download URL and return it like raw data.
Calling download() will block until finished. (synchronous)
Note that for this to work files will be stored in RAM.
Downloading big amount of data this way is certainly a bad idea.
Object will emit downloadProgress(int, int) signal in order
to let you track the progress.
"""
class Sync(QObject):
def download(self, url, userAgent = guserAgent, user = guser, password = gpassword):
self.jar = QNetworkCookieJar()
self.manager = QNetworkAccessManager()
self.manager.setCookieJar(self.jar)
self.manager.finished.connect(self.downloadFinished)
self.manager.authenticationRequired.connect(self.authenticationRequired)
self.url = QUrl(url)
self.userAgent = userAgent
self.user = user
self.password = password
self.returnData = None
self.request = QNetworkRequest(self.url)
self.request.setRawHeader("User-Agent", self.userAgent)
self.reply = self.manager.get(self.request)
self.reply.downloadProgress.connect(self.progressCallback)
self.eventloop = QEventLoop()
self.eventloop.exec_()
return self.returnData
def authenticationRequired(self, reply, authenticator):
authenticator.setUser(self.user)
authenticator.setPassword(self.password)
def progressCallback(self, done, total):
self.emit(QtCore.SIGNAL('downloadProgress(int, int)'), done, total)
def downloadFinished(self, reply):
self.redirect = reply.attribute(QNetworkRequest.RedirectionTargetAttribute).toUrl()
if not self.redirect.isEmpty():
self.request = QNetworkRequest(self.redirect)
self.request.setRawHeader("User-Agent", self.userAgent)
self.reply = self.manager.get(self.request)
self.reply.downloadProgress.connect(self.progressCallback)
else:
self.returnData = self.reply.readAll()
self.eventloop.quit()
"""
This class will download URL and will write it to disk.
Calling download() will block until finished. (synchoronous)
Note that even if the download() method is syncronous, data
will be writen in asynchronous mode. This way data won't be stored
in RAM avoiding huge RAM usage for big amount of data.
Object will emit downloadProgress(int, int) signal in order
to let you track the progress.
"""
class SyncToFile(QObject):
def download(self, url, path, userAgent = guserAgent, user = guser, password = gpassword):
self.jar = QNetworkCookieJar()
self.manager = QNetworkAccessManager()
self.manager.setCookieJar(self.jar)
self.manager.finished.connect(self.downloadFinished)
self.manager.authenticationRequired.connect(self.authenticationRequired)
self.url = QUrl(url)
self.path = QFile(path)
self.path.open(QIODevice.WriteOnly)
self.userAgent = userAgent
self.user = user
self.password = password
self.request = QNetworkRequest(self.url)
self.request.setRawHeader("User-Agent", self.userAgent)
self.reply = self.manager.get(self.request)
self.reply.readyRead.connect(self.readyRead)
self.reply.downloadProgress.connect(self.progressCallback)
self.eventloop = QEventLoop()
self.eventloop.exec_()
return
def authenticationRequired(self, reply, authenticator):
authenticator.setUser(self.user)
authenticator.setPassword(self.password)
def readyRead(self):
self.path.write(self.reply.readAll())
def progressCallback(self, done, total):
self.emit(QtCore.SIGNAL('downloadProgress(int, int)'), done, total)
def downloadFinished(self, reply):
self.redirect = reply.attribute(QNetworkRequest.RedirectionTargetAttribute).toUrl()
if not self.redirect.isEmpty():
self.request = QNetworkRequest(self.redirect)
self.request.setRawHeader("User-Agent", self.userAgent)
self.reply = self.manager.get(self.request)
try:
self.path.close()
self.path.open(QIODevice.WriteOnly)
except:
pass
self.reply.readyRead.connect(self.readyRead)
self.reply.downloadProgress.connect(self.progressCallback)
else:
self.path.close()
self.eventloop.quit()
"""
This class will download URL and will emit an event with raw data when finished.
Calling download() won't block (asynchronous). Object will emit downloadFinished(PyQt_PyObject)
signal in order to let you know that the download has finished, and to let you
get raw data from reply. Note that for this to work data will be stored in RAM.
Downloading big amount of data this way is certainly a bad idea.
Object will emit downloadProgress(int, int) signal in order
to let you track the progress.
"""
class Async(QThread):
def run(self):
self.jar = QNetworkCookieJar()
self.manager = QNetworkAccessManager()
self.manager.setCookieJar(self.jar)
self.manager.finished.connect(self.downloadFinished)
self.manager.authenticationRequired.connect(self.authenticationRequired)
self.request = QNetworkRequest(self.url)
self.request.setRawHeader("User-Agent", self.userAgent)
self.reply = self.manager.get(self.request)
self.reply.downloadProgress.connect(self.progressCallback)
self.exec_()
def download(self, url, userAgent = guserAgent, user = guser, password = gpassword):
self.url = QUrl(url)
self.userAgent = userAgent
self.user = user
self.password = password
self.returnData = None
self.moveToThread(self)
self.start()
def authenticationRequired(self, reply, authenticator):
authenticator.setUser(self.user)
authenticator.setPassword(self.password)
def progressCallback(self, done, total):
self.emit(QtCore.SIGNAL('downloadProgress(int, int)'), done, total)
def downloadFinished(self, reply):
self.redirect = reply.attribute(QNetworkRequest.RedirectionTargetAttribute).toUrl()
if not self.redirect.isEmpty():
self.request = QNetworkRequest(self.redirect)
self.request.setRawHeader("User-Agent", self.userAgent)
self.reply = self.manager.get(self.request)
self.reply.downloadProgress.connect(self.progressCallback)
else:
self.emit(QtCore.SIGNAL('downloadFinished(PyQt_PyObject)'), str(self.reply.readAll()))
self.quit()
"""
This class will download URL and will write it to disk.
Calling download() won't block (asynchronous). Object will emit downloadFinished()
signal in order to let you know that the download has finished.
Note that data will be writen in asynchronous mode. This way data
won't be stored in RAM avoiding huge RAM usage for big amount of data.
Object will emit downloadProgress(int, int) signal in order
to let you track the progress.
"""
class AsyncToFile(QThread):
def run(self):
self.jar = QNetworkCookieJar()
self.manager = QNetworkAccessManager()
self.manager.setCookieJar(self.jar)
self.manager.finished.connect(self.downloadFinished)
self.manager.authenticationRequired.connect(self.authenticationRequired)
self.request = QNetworkRequest(self.url)
self.request.setRawHeader("User-Agent", self.userAgent)
self.reply = self.manager.get(self.request)
self.reply.readyRead.connect(self.readyRead)
self.reply.downloadProgress.connect(self.progressCallback)
self.exec_()
def download(self, url, path, userAgent = guserAgent, user = guser, password = gpassword):
self.url = QUrl(url)
self.path = QFile(path)
self.path.open(QIODevice.WriteOnly)
self.userAgent = userAgent
self.user = user
self.password = password
self.returnData = None
self.moveToThread(self)
self.start()
def authenticationRequired(self, reply, authenticator):
authenticator.setUser(self.user)
authenticator.setPassword(self.password)
def readyRead(self):
self.path.write(self.reply.readAll())
def progressCallback(self, done, total):
self.emit(QtCore.SIGNAL('downloadProgress(int, int)'), done, total)
def downloadFinished(self, reply):
self.redirect = reply.attribute(QNetworkRequest.RedirectionTargetAttribute).toUrl()
if not self.redirect.isEmpty():
self.request = QNetworkRequest(self.redirect)
self.request.setRawHeader("User-Agent", self.userAgent)
self.reply = self.manager.get(self.request)
try:
self.path.close()
self.path.open(QIODevice.WriteOnly)
except:
pass
self.reply.readyRead.connect(self.readyRead)
self.reply.downloadProgress.connect(self.progressCallback)
else:
self.path.close()
self.emit(QtCore.SIGNAL('downloadFinished()'))
self.quit()
###Examples of usage###
"""
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import QEventLoop
from QNAM import Sync, SyncToFile, Async, AsyncToFile
class Test():
def syncProgress(self, done, total):
print "\r", "Sync progress: " + str(done) + " of " + str(total),
def syncToFileProgress(self, done, total):
print "\r", "SyncToFile progress: " + str(done) + " of " + str(total),
def asyncProgress(self, done, total):
print "\r", "Async progress: " + str(done) + " of " + str(total),
def asyncToFileProgress(self, done, total):
print "\r", "AsyncToFile progress: " + str(done) + " of " + str(total),
def asyncFinished(self, data):
print "\nAsync finished!"
self.eventloop.quit()
def asyncToFileFinished(self):
print "\nAsyncToFile finished!"
self.eventloop.quit()
def run(self):
self.eventloop = QEventLoop()
print "Sync test..."
self.objsync = Sync()
QtCore.QObject.connect(self.objsync, QtCore.SIGNAL("downloadProgress(int, int)"), self.syncProgress)
self.objsync.download("http://sourceforge.net/projects/python-jake/files/betas/Jake-PyQT4-0.1.zip/download")
print "\nSync finished!"
print "SyncToFile test..."
self.objsynctofile = SyncToFile()
QtCore.QObject.connect(self.objsynctofile, QtCore.SIGNAL("downloadProgress(int, int)"), self.syncToFileProgress)
self.objsynctofile.download("http://sourceforge.net/projects/python-jake/files/betas/Jake-PyQT4-0.1.zip/download", "/tmp/something")
print "\nSyncToFile finished!"
print "Async test..."
self.objasync = Async()
QtCore.QObject.connect(self.objasync, QtCore.SIGNAL("downloadProgress(int, int)"), self.asyncProgress)
QtCore.QObject.connect(self.objasync, QtCore.SIGNAL("downloadFinished(PyQt_PyObject)"), self.asyncFinished)
self.objasync.download("http://sourceforge.net/projects/python-jake/files/betas/Jake-PyQT4-0.1.zip/download")
print "After download() start..."
self.eventloop.exec_()
print "AsyncToFile test..."
self.objasynctofile = AsyncToFile()
QtCore.QObject.connect(self.objasynctofile, QtCore.SIGNAL("downloadProgress(int, int)"), self.asyncToFileProgress)
QtCore.QObject.connect(self.objasynctofile, QtCore.SIGNAL("downloadFinished()"), self.asyncToFileFinished)
self.objasynctofile.download("http://sourceforge.net/projects/python-jake/files/betas/Jake-PyQT4-0.1.zip/download", "/tmp/something")
print "After download() start..."
self.eventloop.exec_()
print "Everything is OK!"
sys.exit(0)
app = QtGui.QApplication(sys.argv)
a = Test()
a.run()
app.exec_()
"""