Files
WPIQ/wo/core/download.py

66 lines
2.4 KiB
Python
Raw Permalink Normal View History

2018-11-13 21:55:59 +01:00
"""WordOps download core classes."""
import os
import requests
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, packages must be list in format of
2018-11-13 21:55:59 +01:00
[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)
2024-06-08 15:58:29 +02:00
Log.wait(self, "Downloading {0:20}".format(pkg_name))
2019-10-23 14:08:54 +02:00
with open(filename, "wb") as out_file:
req = requests.get(url, timeout=(5, 30))
2019-10-23 14:08:54 +02:00
if req.encoding is None:
req.encoding = 'utf-8'
out_file.write(req.content)
2024-06-08 15:58:29 +02:00
Log.valide(self, "Downloading {0:20}".format(pkg_name))
except requests.RequestException as e:
Log.debug(self, f"[{type(e).__name__}] {str(e)}")
2018-11-13 21:55:59 +01:00
Log.error(self, "Unable to download file, {0}"
2024-06-08 15:58:29 +02:00
.format(filename), exit=False)
2018-11-13 21:55:59 +01:00
return False
2019-10-28 06:52:23 +01:00
return 0
def latest_release(self, repository, name=False):
"""Get the latest release number of a GitHub repository.\n
repository format should be: \"user/repo\""""
try:
req = requests.get(
'https://api.github.com/repos/{0}/releases/latest'
.format(repository),
timeout=(5, 30))
2019-10-30 16:14:14 +01:00
github_json = req.json()
except requests.RequestException as e:
Log.debug(self, str(e))
Log.error(self, "Unable to query GitHub API")
if name:
return github_json["name"]
else:
return github_json["tag_name"]
2019-10-30 15:18:19 +01:00
def pma_release(self):
"""Get the latest phpmyadmin release number from a json file"""
try:
req = requests.get(
'https://www.phpmyadmin.net/home_page/version.json',
timeout=(5, 30))
pma_json = req.json()
except requests.RequestException as e:
Log.debug(self, str(e))
Log.error(self, "Unable to query phpmyadmin API")
return pma_json["version"]