-
Notifications
You must be signed in to change notification settings - Fork 2
/
DBDownloader.py
35 lines (28 loc) · 1.04 KB
/
DBDownloader.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
import asyncio
from os import path
from config import DB_PATH
from HttpClient import HttpClient
from utils import format_filesize
class DBDownloader(HttpClient):
def __init__(self):
super().__init__()
self.tasks = []
def appendTask(self, src, dest=None):
filename = path.basename(src)
if not dest:
dest = DB_PATH + filename
self.tasks.append([src, dest])
async def download(self, src, dest):
size = 0
with open(dest, 'w', encoding='utf-8') as fp:
async with self.session.get(src) as resp:
content = await resp.text()
fp.write(content)
size = len(content)
print('DBDownloader: {} ({}) ok!'.format(dest, format_filesize(size)))
async def start(self):
tasks = []
for task in self.tasks:
tasks.append(asyncio.ensure_future(self.download(*task)))
dones, pendings = await asyncio.wait(tasks)
print('DBDownloader: {} done, {} pendings.'.format(len(dones), len(pendings)))