1
|
|
|
""" |
2
|
|
|
API |
3
|
|
|
.................................................................................................... |
4
|
|
|
MIT License |
5
|
|
|
Copyright (c) 2021-2023 AUT Iran, Mohammad H Forouhesh |
6
|
|
|
Copyright (c) 2021-2022 MetoData.ai, Mohammad H Forouhesh |
7
|
|
|
.................................................................................................... |
8
|
|
|
This module contains tools to download resources over http connections. |
9
|
|
|
supported http links are: |
10
|
|
|
- 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/xgb_vaccine', |
|
|
|
|
11
|
|
|
- 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/pa_vaccine', |
|
|
|
|
12
|
|
|
- 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/lasso_vaccine', |
|
|
|
|
13
|
|
|
- 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/gnb_vaccine' |
|
|
|
|
14
|
|
|
""" |
15
|
|
|
|
16
|
1 |
|
import os |
17
|
1 |
|
from typing import Union |
18
|
1 |
|
import zipfile |
19
|
1 |
|
from io import BytesIO |
20
|
1 |
|
import requests |
21
|
|
|
|
22
|
1 |
|
http_dict = {'xgb_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/xgb_vaccine.zip', |
|
|
|
|
23
|
|
|
'pa_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/pa_vaccine.zip', |
|
|
|
|
24
|
|
|
'lasso_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/lasso_vaccine.zip', |
|
|
|
|
25
|
|
|
'gnb_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/gnb_vaccine.zip'} |
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
1 |
View Code Duplication |
def downloader(path: str, save_path: str) -> Union[int, None]: |
|
|
|
|
29
|
|
|
""" |
30
|
|
|
A tool to download and save files over https. |
31
|
|
|
:param path: The path to the desired file. |
32
|
|
|
:param save_path: The intended storage path. |
33
|
|
|
:return: If the file exists, it returns 0 (int), otherwise nothing would be returned. |
34
|
|
|
""" |
35
|
1 |
|
try: |
36
|
1 |
|
model_bin = requests.get(path, allow_redirects=True) |
37
|
1 |
|
with zipfile.ZipFile(BytesIO(model_bin.content)) as resource: |
38
|
1 |
|
resource.extractall(save_path) |
39
|
|
|
except Exception: |
40
|
|
|
raise Exception('not a proper webpage') |
41
|
1 |
|
return 0 |
42
|
|
|
|
43
|
|
|
|
44
|
1 |
View Code Duplication |
def get_resources(dir_path: str, resource_name: str) -> Union[int, str]: |
|
|
|
|
45
|
|
|
""" |
46
|
|
|
A tool to download required resources over internet. |
47
|
|
|
:param dir_path: Path to the https link of the resource |
48
|
|
|
:param resource_name: Resource name. |
49
|
|
|
:return: Path to the downloaded resource. |
50
|
|
|
""" |
51
|
1 |
|
save_dir = dir_path + 'model_dir/' |
52
|
1 |
|
if os.path.isdir(save_dir + f'{resource_name}/'): return 0 |
|
|
|
|
53
|
1 |
|
os.makedirs(save_dir, exist_ok=True) |
54
|
1 |
|
downloader(path=http_dict[resource_name], save_path=save_dir) |
55
|
|
|
return str(save_dir + resource_name) |
56
|
|
|
|
This check looks for lines that are too long. You can specify the maximum line length.