1
# Copyright 2013 Facundo Batista
3
# This program is free software: you can redistribute it and/or modify it
4
# under the terms of the GNU General Public License version 3, as published
5
# by the Free Software Foundation.
7
# This program is distributed in the hope that it will be useful, but
8
# WITHOUT ANY WARRANTY; without even the implied warranties of
9
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
10
# PURPOSE. See the GNU General Public License for more details.
12
# You should have received a copy of the GNU General Public License along
13
# with this program. If not, see <http://www.gnu.org/licenses/>.
15
# For further info, check https://launchpad.net/encuentro
17
"""Some useful functions."""
21
from PyQt4 import QtNetwork, QtCore
23
_qt_network_manager = QtNetwork.QNetworkAccessManager()
27
"""Deferredly download an URL, non blocking."""
28
deferred = defer.Deferred()
31
"""Send data through the deferred, if wasn't fired before."""
32
data = req.read(req.bytesAvailable())
33
if not deferred.called:
34
deferred.callback(data)
36
request = QtNetwork.QNetworkRequest()
37
request.setUrl(QtCore.QUrl(url))
39
req = _qt_network_manager.get(request)
40
req.error.connect(deferred.errback)
41
req.finished.connect(end)
46
if __name__ == "__main__":
48
app = QtCore.QCoreApplication(sys.argv)
49
url = "http://www.taniquetil.com.ar/facundo/imgs/felu-camagrande.jpg"
51
@defer.inline_callbacks
54
data = yield download(url)
55
print "All done!", len(data), type(data)