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/v1.0.0/xgb_vaccine.zip' |
|
|
|
|
11
|
|
|
- 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/v1.0.0/pa_vaccine.zip' |
|
|
|
|
12
|
|
|
- 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/v1.0.0/lasso_vaccine.zip' |
|
|
|
|
13
|
|
|
- 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/v1.0.0/gnb_vaccine.zip' |
|
|
|
|
14
|
|
|
- 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/v1.0.0/xgb_jcpoa.zip' |
|
|
|
|
15
|
|
|
- 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/v1.0.0/pa_jcpoa.zip' |
|
|
|
|
16
|
|
|
- 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/v1.0.0/lasso_jcpoa.zip', |
|
|
|
|
17
|
|
|
- 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/v1.0.0/gnb_jcpoa.zip' |
|
|
|
|
18
|
|
|
""" |
19
|
|
|
|
20
|
1 |
|
import os |
21
|
1 |
|
from typing import Union |
22
|
1 |
|
import zipfile |
23
|
1 |
|
from io import BytesIO |
24
|
1 |
|
import requests |
25
|
|
|
|
26
|
1 |
|
http_dict = {'xgb_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/xgb_vaccine.zip', |
|
|
|
|
27
|
|
|
'pa_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/pa_vaccine.zip', |
|
|
|
|
28
|
|
|
'lasso_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/lasso_vaccine.zip', |
|
|
|
|
29
|
|
|
'gnb_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/gnb_vaccine.zip', |
|
|
|
|
30
|
|
|
'xgb_jcpoa': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/v1.0.0/xgb_jcpoa.zip', |
|
|
|
|
31
|
|
|
'pa_jcpoa': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/v1.0.0/pa_jcpoa.zip', |
|
|
|
|
32
|
|
|
'lasso_jcpoa': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/v1.0.0/lasso_jcpoa.zip', |
|
|
|
|
33
|
|
|
'gnb_jcpoa': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/v1.0.0/gnb_jcpoa.zip'} |
|
|
|
|
34
|
|
|
|
35
|
|
|
|
36
|
1 |
|
def downloader(path: str, save_path: str) -> Union[int, None]: |
37
|
|
|
""" |
38
|
|
|
A tool to download and save files over https. |
39
|
|
|
:param path: The path to the desired file. |
40
|
|
|
:param save_path: The intended storage path. |
41
|
|
|
:return: If the file exists, it returns 0 (int), otherwise nothing would be returned. |
42
|
|
|
""" |
43
|
1 |
|
assert path in http_dict.values(), f'{path[-path[::-1].find("/"):]} is not a supported models, use: \n{http_dict}' |
|
|
|
|
44
|
1 |
|
model_bin = requests.get(path, allow_redirects=True) |
45
|
1 |
|
with zipfile.ZipFile(BytesIO(model_bin.content)) as resource: |
46
|
1 |
|
resource.extractall(save_path) |
47
|
1 |
|
return 0 |
48
|
|
|
|
49
|
|
|
|
50
|
1 |
View Code Duplication |
def get_resources(dir_path: str, resource_name: str) -> Union[int, str]: |
|
|
|
|
51
|
|
|
""" |
52
|
|
|
A tool to download required resources over internet. |
53
|
|
|
:param dir_path: Path to the https link of the resource |
54
|
|
|
:param resource_name: Resource name. |
55
|
|
|
:return: Path to the downloaded resource. |
56
|
|
|
""" |
57
|
1 |
|
save_dir = dir_path + 'model_dir/' |
58
|
1 |
|
if os.path.isdir(save_dir + f'{resource_name}/'): return 0 |
|
|
|
|
59
|
1 |
|
os.makedirs(save_dir, exist_ok=True) |
60
|
1 |
|
downloader(path=http_dict[resource_name], save_path=save_dir) |
61
|
|
|
return str(save_dir + resource_name) |
62
|
|
|
|
This check looks for lines that are too long. You can specify the maximum line length.