-
Notifications
You must be signed in to change notification settings - Fork 0
/
fr_download.py
88 lines (72 loc) · 3.08 KB
/
fr_download.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
import os
import requests
from pathlib import Path
###
# French information on the approval number (german: Genusstauglichkeitskennzeichen)
# Downloads the data from the link. Last checked 30th October 2023.
# Link: https://fichiers-publics.agriculture.gouv.fr/dgal/ListesOfficielles/
# The links might be broken by now.
# I had to add ', verify = False' to re request due to some certificate error.#
# However, this is not recomended. It is a hack.
###
def download_file(base_url: str, file: str, file_path: str):
"""
Downloads the contents of a file from a URL.
Args:
url (str): The URL of the file to download.
file_path (str): The path of the local file where the contents should be saved.
Raises:
requests.RequestException: If the request to the URL fails.
OSError: If the local file cannot be written to.
"""
try:
print(f" downloading {file}")
response = requests.get(base_url + file, allow_redirects=True, verify=False)
if response.status_code == 200:
with open(file_path, "wb") as file:
file.write(response.content)
else:
print(f"Failed to download: {file_path}. Status code: {response.status_code}")
except requests.RequestException as e:
print(f"Request failed: {e}")
except Exception as ex:
print(f"An unexpected error occurred: {ex}")
def download_files(base_url, files, directory):
"""
Downloads a list of files from a base URL.
Args:
base_url (str): The base URL from which the files should be downloaded.
files (list): A list of the files to download.
directory (str): The directory where the files should be saved.
Raises:
OSError: If the directory cannot be created.
"""
try:
os.makedirs(directory, exist_ok=True)
for file in files:
file_path = os.path.join(directory, file)
if not os.path.exists(file_path):
download_file(base_url, file, file_path)
else:
print(f"File already exists: {file_path}")
except OSError as ose:
print(f"Failed to create directory: {ose}")
except Exception as ex:
print(f"An unexpected error occurred: {ex}")
# Base url for the french information on approval numbers
base_url = 'https://fichiers-publics.agriculture.gouv.fr/dgal/ListesOfficielles/'
# director to write the files
directory = 'fr'
# list of files to be downloaded
files = ['SSA1_ACTIV_GEN.txt', 'SSA1_VIAN_ONG_DOM.txt', 'SSA1_VIAN_COL_LAGO.txt', 'SSA1_VIAN_GIB_ELEV.txt',
'SSA1_VIAN_GIB_SAUV.txt', 'SSA1_VIAND_HACHE_VSM.txt', 'SSA4_AGSANPROBASEVDE_PRV.txt', 'SSA4B_AS_CE_PRODCOQUI_COV.txt',
'SSA4B_AS_CE_PRODPECHE_COV.txt', 'SSA1_LAIT.txt', 'SSA1_OEUF.txt', 'SSA1_GREN_ESCARG.txt',
'SSA4_AGSANGREXPR_PRV.txt', 'SSA4_AGR_ESVEBO_PRV.txt', 'SSA4_AGSANGELAT_PRV.txt', 'SSA4_AGSANCOLL_PRV.txt',
'SSA_PROD_RAFF.txt', 'SSA4_ASCCC_PRV.txt']
# start downloading files
def download_fr():
download_files(base_url, files, directory)
def main():
download_fr()
if __name__ == "__main__":
main()