Пример #1
0
class DownloadManager:
    def __init__(self, chunks):
        self.workers = [DownloadWorker(self) for i in range(WORKERS)]
        self.pending_tasks = []
        self.chunks = chunks
        self.content = [None for i in self.chunks]
        self.canceled = False
        self.db = VodstokDB()

    def getPendingTask(self):
        if len(self.pending_tasks) > 0:
            return self.pending_tasks.pop()
        else:
            return None

    def chunkDownloaded(self, task, chunk):
        self.content[task.pos] = chunk
        self.db.addEndpoint("http://" + task.url)
        sys.stdout.write(
            "\r[+] Downloading ... %0.2f%%"
            % (float(len(self.chunks) - len(self.pending_tasks)) * 100.0 / len(self.chunks))
        )
        sys.stdout.flush()

    def chunkError(self):
        self.canceled = True

    def shouldStop(self):
        return self.canceled

    def download(self):
        # add everything as pending tasks
        for i in range(len(self.chunks)):
            self.pending_tasks.append(DownloadTask(self.chunks[i][0], self.chunks[i][1]))

        sys.stdout.write(
            "\r[+] Downloading ... %0.2f%%"
            % (float(len(self.chunks) - len(self.pending_tasks)) * 100.0 / len(self.chunks))
        )
        sys.stdout.flush()

        # and start processing
        for worker in self.workers:
            worker.start()

            # wait until workers stop
        for worker in self.workers:
            worker.join()
        sys.stdout.write("\n")
        sys.stdout.flush()
        return "".join(self.content)
Пример #2
0
 def __init__(self, chunks):
     self.workers = [DownloadWorker(self) for i in range(WORKERS)]
     self.pending_tasks = []
     self.chunks = chunks
     self.content = [None for i in self.chunks]
     self.canceled = False
     self.db = VodstokDB()