File size: 2,913 Bytes
7105847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3

import os
import sys
import argparse
import requests
from enum import Enum
import urllib.parse as urlparse
import logging
import tarfile


logger = logging.getLogger()
logger.setLevel(logging.INFO)


class EnumString(Enum):
    def __str__(self: Enum) -> str:
        return self.value


class CorpusType(EnumString):
    NEWS = "news"
    WIKI = "wikipedia"


class CorpusSize(EnumString):
    SMALLEST = "10K"
    SMALL = "30K"
    MEDIUM = "100K"
    LARGE = "300K"
    LARGEST = "1M"


BASE_URL = "https://downloads.wortschatz-leipzig.de"
URL_PREFIX = "corpora"
FILE_TEMPLATE = "fra_{type}_{year}_{size}.tar.gz"


def main(args: argparse.Namespace) -> int:
    if not os.path.exists(args.dst_dir):
        logger.info(f"Invalid destination directory: '{args.dst_dir}'.")
        return -1

    filename = FILE_TEMPLATE.format(
        type=args.type.value,
        year=args.year,
        size=args.size.value,
    )
    url = urlparse.urljoin(
        BASE_URL,
        f"{URL_PREFIX}/{filename}"    
    )

    try:
        file_path = os.path.join(args.dst_dir, filename)
        logger.info("Downloading %s" % file_path)
        with open(file_path, 'wb') as f:
            response = requests.get(url, stream=True)
            total_length = response.headers.get('content-length')

            if total_length is None:  # no content length header
                f.write(response.content)
            else:
                dl = 0
                total_length = int(total_length)
                for data in response.iter_content(chunk_size=4096):
                    dl += len(data)
                    f.write(data)
                    done = int(50 * dl / total_length)
                    done_bar = '=' * done
                    remainder = ' ' * (50 - done)
                    done_pct = f"{100.0 * dl / total_length:6.2f}"
                    sys.stdout.write("\r[%s%s] [%s]" % (done_bar, remainder, done_pct))    
                    sys.stdout.flush()
    except Exception as error:
        logger.error(error)
        return -1

    # Uncompress file
    try:
        logger.info("Extracting %s" % file_path)
        with tarfile.open(file_path) as f:
            f.extractall(os.path.dirname(file_path))
    except Exception as error:
        logger.error(error)
        return -2

    return 0


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--type", '-t',
        type=CorpusType,
        choices=list(CorpusType)
    )
    parser.add_argument(
        "--year", '-y',
        type=str,
        default='2023'
    )
    parser.add_argument(
        "--size", '-s',
        type=CorpusSize,
        choices=list(CorpusSize)
    )
    parser.add_argument(
        "--dst-dir", '-d',
        type=str,
        default='data'
    )
    args, _ = parser.parse_known_args()
    
    sys.exit(
        main(args)
    )