2018-11-13 21:55:59 +01:00
|
|
|
"""WordOps download core classes."""
|
|
|
|
|
import os
|
2019-10-23 14:08:54 +02:00
|
|
|
from requests import get, RequestException
|
2019-09-04 20:36:15 +02:00
|
|
|
|
2018-11-13 21:55:59 +01:00
|
|
|
from wo.core.logging import Log
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WODownload():
|
|
|
|
|
"""Method to download using urllib"""
|
|
|
|
|
def __init__():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def download(self, packages):
|
|
|
|
|
"""Download packages, packges must be list in format of
|
|
|
|
|
[url, path, package name]"""
|
|
|
|
|
for package in packages:
|
|
|
|
|
url = package[0]
|
|
|
|
|
filename = package[1]
|
|
|
|
|
pkg_name = package[2]
|
|
|
|
|
try:
|
|
|
|
|
directory = os.path.dirname(filename)
|
|
|
|
|
if not os.path.exists(directory):
|
|
|
|
|
os.makedirs(directory)
|
|
|
|
|
Log.info(self, "Downloading {0:20}".format(pkg_name), end=' ')
|
2019-10-23 14:08:54 +02:00
|
|
|
with open(filename, "wb") as out_file:
|
|
|
|
|
req = get(url, timeout=15)
|
|
|
|
|
if req.encoding is None:
|
|
|
|
|
req.encoding = 'utf-8'
|
|
|
|
|
out_file.write(req.content)
|
2018-11-13 21:55:59 +01:00
|
|
|
Log.info(self, "{0}".format("[" + Log.ENDC + "Done"
|
|
|
|
|
+ Log.OKBLUE + "]"))
|
2019-10-23 14:08:54 +02:00
|
|
|
except RequestException as e:
|
2018-11-13 21:55:59 +01:00
|
|
|
Log.debug(self, "[{err}]".format(err=str(e.reason)))
|
|
|
|
|
Log.error(self, "Unable to download file, {0}"
|
|
|
|
|
.format(filename))
|
|
|
|
|
return False
|