[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [ooni-probe/master] Simplify the code for downloading resources.



commit 8545423373dee1f4b801375922b67bc2417cb426
Author: Arturo Filastò <art@xxxxxxxxx>
Date:   Mon Aug 18 13:28:36 2014 +0200

    Simplify the code for downloading resources.
    
    Use downloadPage instead of our own class.
---
 ooni/resources/update.py |   30 +++---------------------------
 1 file changed, 3 insertions(+), 27 deletions(-)

diff --git a/ooni/resources/update.py b/ooni/resources/update.py
index 4cb0f49..a2919fc 100644
--- a/ooni/resources/update.py
+++ b/ooni/resources/update.py
@@ -1,32 +1,11 @@
 import os
 
-from twisted.internet import reactor, defer, protocol
-from twisted.web.client import RedirectAgent, Agent
+from twisted.internet import defer
+from twisted.web.client import downloadPage
 
 from ooni.settings import config
 from ooni.resources import inputs, geoip
 
-agent = RedirectAgent(Agent(reactor))
-
-
-class SaveToFile(protocol.Protocol):
-    def __init__(self, finished, filesize, filename):
-        self.finished = finished
-        self.remaining = filesize
-        self.outfile = open(filename, 'wb')
-
-    def dataReceived(self, bytes):
-        if self.remaining:
-            display = bytes[:self.remaining]
-            self.outfile.write(display)
-            self.remaining -= len(display)
-        else:
-            self.outfile.close()
-
-    def connectionLost(self, reason):
-        self.outfile.close()
-        self.finished.callback(None)
-
 
 @defer.inlineCallbacks
 def download_resource(resources):
@@ -34,10 +13,7 @@ def download_resource(resources):
         print "Downloading %s" % filename
 
         filename = os.path.join(config.resources_directory, filename)
-        response = yield agent.request("GET", resource['url'])
-        finished = defer.Deferred()
-        response.deliverBody(SaveToFile(finished, response.length, filename))
-        yield finished
+        yield downloadPage(resource['url'], filename)
 
         if resource['action'] is not None:
             yield defer.maybeDeferred(resource['action'],



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits