-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWITCHFileDownloader.py
176 lines (135 loc) · 5.09 KB
/
SWITCHFileDownloader.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from dependencies import *
from SWITCHlogger import logger
class SWITCHFileDownloader:
_auth = None
_src_dst_df = None
_src_col = None
_dst_col = None
def __repr__(self) -> str:
repr_str = (
f"<{__class__.__name__}>\n"
f'{self._auth.username=}\n'
f'{self._auth.password=}\n'
f'{self._src_dst_df=}\n'
f'{self._src_col=}\n'
f'{self._dst_col=}\n'
f"self._src_dst_df=\n{self._src_dst_df}"
)
return repr_str
@classmethod
def set_login(cls, SWITCHemail:str, SWITCHpw:str) -> None:
"""Sets the login credentials for the class.
Parameters
----------
cls : type
The class to which the login credentials are being set.
SWITCHemail : str
The email address used for login.
SWITCHpw : str
The password used for login.
Returns
-------
None
"""
cls._auth = HTTPBasicAuth(
SWITCHemail, SWITCHpw
)
@classmethod
def set_SRC_DST_df(cls, src_dst_df:pd.DataFrame) -> None:
"""Sets the source-destination DataFrame for the class.
Parameters
----------
cls : type
The class to which the DataFrame is being assigned.
src_dst_df : pd.DataFrame
The DataFrame containing source-destination data.
Returns
-------
None
"""
cls._src_dst_df = src_dst_df
@classmethod
def set_src_dst_column_names(cls, src_col:str, dst_col:str) -> None:
"""Sets the source and destination column names for the class.
Parameters
----------
cls : type
The class on which to set the column names.
src_col : str
The name of the source column in the given source-destination DataFrame.
dst_col : str
The name of the destination column in the given source-destination DataFrame.
Returns
-------
None
"""
cls._src_col = src_col
cls._dst_col = dst_col
def _downloadFile(self, row:pd.Series) -> None:
src_url = row[self._src_col]
dst_pth = row[self._dst_col]
try:
logger.debug(f"... starting download from {src_url=}")
r = requests.get(src_url, auth=self._auth, stream=True, verify=True)
if r.ok:
logger.debug(f"... saving to {dst_pth=}")
with open(dst_pth, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024 * 8):
if chunk:
f.write(chunk)
f.flush()
os.fsync(f.fileno())
logger.debug("... download completed successfully")
else: # HTTP status code 4XX/5XX
logger.debug(f"... download failed: status code {r.status_code}\n{r.text}")
except requests.RequestException as e:
logger.debug(f"... request failed: {e}")
except OSError as e:
logger.debug(f"... file operation failed: {e}")
def _checkup(self) -> None:
attributes = {
f'{self._auth.username=}': self._auth.username,
f'{self._auth.password=}': self._auth.password,
f'{self._src_dst_df=}': self._src_dst_df,
f'{self._src_col=}': self._src_col,
f'{self._dst_col=}': self._dst_col
}
missing_attrs = [attr for attr, value in attributes.items() if value is None]
if missing_attrs:
for attr in missing_attrs:
logger.error(
f"... you must define the variable: {attr}"
)
exit()
def KeyInDf(attribute:str) -> None:
if attributes[attribute] not in self._src_dst_df:
logger.error(
f"... col name {attribute} does not exist. You must choose from "
f"{self._src_dst_df.columns=}"
)
exit()
KeyInDf(f'{self._src_col=}')
KeyInDf(f'{self._dst_col=}')
def go(self) -> None:
"""Initiates the download process for files specified in the dataframe.
This method logs the number of files to be downloaded and their source
and destination columns. It then applies the `_downloadFile` method
to each row of the dataframe in parallel to download the files.
Returns
-------
None
"""
self._checkup()
logger.info(
f"... downloading {len(self._src_dst_df)} files "
f"from '{self._src_col=}' to '{self._dst_col=}'"
)
df = self._src_dst_df.parallel_apply(self._downloadFile, axis=1)
if __name__ == '__main__':
SWITCHFileDownloader.set_login(
SWITCHemail='your_email',
SWITCHpw='your_username'
)
print(SWITCHFileDownloader())
myUpdater = SWITCHFileDownloader()
myUpdater.go()