|
| 1 | +import sys |
| 2 | +import shutil |
| 3 | +import os |
| 4 | +import xml.etree.ElementTree as ET |
| 5 | +import urllib.request |
| 6 | +import urllib.error |
| 7 | +import urllib.parse |
| 8 | +import time |
| 9 | + |
| 10 | +def download_tsl_file(url, output_path, filename): |
| 11 | + output_file = os.path.join(output_path, filename) |
| 12 | + print(f"Downloading TSL list from: {url}") |
| 13 | + retries = 3 |
| 14 | + for attempt in range(retries): |
| 15 | + try: |
| 16 | + with urllib.request.urlopen(url, timeout=60) as response, open(output_file, 'wb') as f: |
| 17 | + shutil.copyfileobj(response, f) |
| 18 | + print(f"Saved to: {output_file}") |
| 19 | + return output_file |
| 20 | + except (urllib.error.HTTPError, urllib.error.URLError) as e: |
| 21 | + print(f"Attempt {attempt + 1}/{retries} failed: {e}") |
| 22 | + if attempt + 1 < retries: |
| 23 | + sleep_time = 10 * (attempt + 1) |
| 24 | + print(f"Retrying in {sleep_time} seconds...") |
| 25 | + time.sleep(sleep_time) |
| 26 | + else: |
| 27 | + print("All retry attempts failed.") |
| 28 | + sys.exit(1) |
| 29 | + except IOError as e: |
| 30 | + print(f"Error saving TSL list {output_file}: {e}") |
| 31 | + sys.exit(1) |
| 32 | + |
| 33 | +def download_tsl_files(output_path, list_url, territories): |
| 34 | + master_list_filename = os.path.basename(urllib.parse.urlparse(list_url).path) |
| 35 | + if not master_list_filename: |
| 36 | + master_list_filename = "eu-lotl.xml" # Fallback filename |
| 37 | + file = download_tsl_file(list_url, output_path, master_list_filename) |
| 38 | + try: |
| 39 | + root = ET.parse(file) |
| 40 | + ns = {'tsl': "http://uri.etsi.org/02231/v2#"} |
| 41 | + for pointer in root.findall('.//tsl:OtherTSLPointer', ns): |
| 42 | + territory = pointer.find('.//tsl:SchemeTerritory', ns) |
| 43 | + if territory is None or territory.text not in territories: |
| 44 | + continue |
| 45 | + |
| 46 | + mime = pointer.find('.//tslx:MimeType', {'tslx': 'http://uri.etsi.org/02231/v2/additionaltypes#'}) |
| 47 | + if mime is None or mime.text != 'application/vnd.etsi.tsl+xml': |
| 48 | + continue |
| 49 | + |
| 50 | + location = pointer.find('.//tsl:TSLLocation', ns) |
| 51 | + if location is not None: |
| 52 | + download_tsl_file(location.text, output_path, f"{territory.text}.xml") |
| 53 | + |
| 54 | + except ET.ParseError as e: |
| 55 | + print(f"Error parsing XML: {e}") |
| 56 | + sys.exit(1) |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + if len(sys.argv) < 4: |
| 60 | + print("Usage: python download_tsl.py <output_path> <url> <territory1> [territory2] ...") |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | + output_path = sys.argv[1] |
| 64 | + url = sys.argv[2] |
| 65 | + territories_to_download = sys.argv[3:] |
| 66 | + |
| 67 | + if not os.path.isdir(output_path): |
| 68 | + print(f"Error: Output path '{output_path}' is not a directory.") |
| 69 | + sys.exit(1) |
| 70 | + |
| 71 | + download_tsl_files(output_path, url, territories_to_download) |
| 72 | + print("TSL download process finished.") |
0 commit comments