-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.py
127 lines (114 loc) · 5.12 KB
/
Utils.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import sys
import os
import winreg
import requests
import time
import urllib
from zipfile import ZipFile
def GetSystemEnvironmentVariable(name):
key = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, r"System\CurrentControlSet\Control\Session Manager\Environment")
try:
return winreg.QueryValueEx(key, name)[0]
except:
return None
def GetUserEnvironmentVariable(name):
key = winreg.CreateKey(winreg.HKEY_CURRENT_USER, r"Environment")
try:
return winreg.QueryValueEx(key, name)[0]
except:
return None
def DownloadFile(url, filepath):
path = filepath
filepath = os.path.abspath(filepath)
os.makedirs(os.path.dirname(filepath), exist_ok=True)
if (type(url) is list):
for url_option in url:
print("Downloading", url_option)
try:
DownloadFile(url_option, filepath)
return
except urllib.error.URLError as e:
print(f"URL Error encountered: {e.reason}. Proceeding with backup...\n\n")
os.remove(filepath)
pass
except urllib.error.HTTPError as e:
print(f"HTTP Error encountered: {e.code}. Proceeding with backup...\n\n")
os.remove(filepath)
pass
except:
print(f"Something went wrong. Proceeding with backup...\n\n")
os.remove(filepath)
pass
raise ValueError(f"Failed to download {filepath}")
if not(type(url) is str):
raise TypeError("Argument 'url' must be of type list or string")
with open(filepath, 'wb') as f:
headers = {'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
response = requests.get(url, headers=headers, stream=True)
total = response.headers.get('content-length')
if total is None:
f.write(response.content)
else:
downloaded = 0
total = int(total)
startTime = time.time()
for data in response.iter_content(chunk_size=max(int(total/1000), 1024*1024)):
downloaded += len(data)
f.write(data)
try:
done = int(50*downloaded/total) if downloaded < total else 50
percentage = (downloaded / total) * 100 if downloaded < total else 100
except ZeroDivisionError:
done = 50
percentage = 100
elapsedTime = time.time() - startTime
try:
avgKBPerSecond = (downloaded / 1024) / elapsedTime
except ZeroDivisionError:
avgKBPerSecond = 0.0
avgSpeedString = '{:.2f} KB/s'.format(avgKBPerSecond)
if (avgKBPerSecond > 1024):
avgMBPerSecond = avgKBPerSecond / 1024
avgSpeedString = '{:.2f} MB/s'.format(avgMBPerSecond)
sys.stdout.write('\r[{}{}] {:.2f}% ({}) '.format('█' * done, '.' * (50-done), percentage, avgSpeedString))
sys.stdout.flush()
sys.stdout.write('\n')
def UnzipFile(filepath, deleteZipFile=True):
zipFilePath = os.path.abspath(filepath) # get full path of files
zipFileLocation = os.path.dirname(zipFilePath)
zipFileContent = dict()
zipFileContentSize = 0
with ZipFile(zipFilePath, 'r') as zipFileFolder:
for name in zipFileFolder.namelist():
zipFileContent[name] = zipFileFolder.getinfo(name).file_size
zipFileContentSize = sum(zipFileContent.values())
extractedContentSize = 0
startTime = time.time()
for zippedFileName, zippedFileSize in zipFileContent.items():
UnzippedFilePath = os.path.abspath(f"{zipFileLocation}/{zippedFileName}")
os.makedirs(os.path.dirname(UnzippedFilePath), exist_ok=True)
if os.path.isfile(UnzippedFilePath):
zipFileContentSize -= zippedFileSize
else:
zipFileFolder.extract(zippedFileName, path=zipFileLocation, pwd=None)
extractedContentSize += zippedFileSize
try:
done = int(50*extractedContentSize/zipFileContentSize)
percentage = (extractedContentSize / zipFileContentSize) * 100
except ZeroDivisionError:
done = 50
percentage = 100
elapsedTime = time.time() - startTime
try:
avgKBPerSecond = (extractedContentSize / 1024) / elapsedTime
except ZeroDivisionError:
avgKBPerSecond = 0.0
avgSpeedString = '{:.2f} KB/s'.format(avgKBPerSecond)
if (avgKBPerSecond > 1024):
avgMBPerSecond = avgKBPerSecond / 1024
avgSpeedString = '{:.2f} MB/s'.format(avgMBPerSecond)
sys.stdout.write('\r[{}{}] {:.2f}% ({}) '.format('█' * done, '.' * (50-done), percentage, avgSpeedString))
sys.stdout.flush()
sys.stdout.write('\n')
if deleteZipFile:
os.remove(zipFilePath) # delete zip file