Passed
Push — main ( 081b85...deff66 )
by Mohammad
02:40 queued 12s
created

tracking_policy_agendas.api.downloader()   A

Complexity

Conditions 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
eloc 8
dl 14
loc 14
ccs 6
cts 8
cp 0.75
rs 10
c 0
b 0
f 0
cc 3
nop 2
crap 3.1406
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',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (103/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
11
    - 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/pa_vaccine',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (102/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
12
    - 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/lasso_vaccine',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (105/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
13
    - 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/gnb_vaccine'
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (102/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
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',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (129/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
23
             'pa_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/pa_vaccine.zip',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (127/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
24
             'lasso_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/lasso_vaccine.zip',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (133/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
25
             'gnb_vaccine': 'https://github.com/MohammadForouhesh/tracking-policy-agendas/releases/download/bin/gnb_vaccine.zip'}
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (129/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
26
27
28 1 View Code Duplication
def downloader(path: str, save_path: str) -> Union[int, None]:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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]:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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
0 ignored issues
show
Coding Style introduced by
More than one statement on a single line
Loading history...
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