|
| 1 | +""" |
| 2 | +fetch_oecd_full_gdp_dataset.py |
| 3 | +
|
| 4 | +This script provides a complete example of fetching both the metadata and the |
| 5 | +full data series for the OECD's Quarterly GDP Growth dataset. |
| 6 | +""" |
| 7 | + |
| 8 | +import logging |
| 9 | +import sys |
| 10 | +import os |
| 11 | + |
| 12 | +# Add the project root to the Python path |
| 13 | +sys.path.append( |
| 14 | + os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))) |
| 15 | + |
| 16 | +from tools.sdmx_import.sdmx_client import SdmxClient |
| 17 | + |
| 18 | +# Configure logging |
| 19 | +logging.basicConfig(level=logging.INFO, |
| 20 | + format='%(asctime)s - %(levelname)s - %(message)s') |
| 21 | + |
| 22 | + |
| 23 | +def main(): |
| 24 | + """Downloads the full OECD Quarterly GDP Growth dataset and its metadata.""" |
| 25 | + # --- 1. Define Common Parameters --- |
| 26 | + agency_id = "OECD.SDD.NAD" |
| 27 | + dataflow_id = "DSD_NAMAIN1@DF_QNA_EXPENDITURE_GROWTH_OECD" |
| 28 | + endpoint = "https://sdmx.oecd.org/public/rest/" |
| 29 | + |
| 30 | + # Create output directory inside the samples folder |
| 31 | + output_dir = os.path.join(os.path.dirname(__file__), "output") |
| 32 | + os.makedirs(output_dir, exist_ok=True) |
| 33 | + metadata_output_path = os.path.join(output_dir, |
| 34 | + "oecd_gdp_full_metadata.xml") |
| 35 | + data_output_path = os.path.join(output_dir, "oecd_gdp_full_data.csv") |
| 36 | + |
| 37 | + # --- 2. Initialize Client --- |
| 38 | + client = SdmxClient(endpoint, agency_id) |
| 39 | + |
| 40 | + # --- 3. Fetch Metadata --- |
| 41 | + logging.info("--- Step 1: Starting Metadata Download ---") |
| 42 | + try: |
| 43 | + client.download_metadata(dataflow_id=dataflow_id, |
| 44 | + output_path=metadata_output_path) |
| 45 | + logging.info( |
| 46 | + f"--- Successfully downloaded metadata to {metadata_output_path} ---" |
| 47 | + ) |
| 48 | + except Exception as e: |
| 49 | + logging.error(f"Failed to download metadata. Error: {e}") |
| 50 | + # Exit with a non-zero status code to indicate failure. |
| 51 | + sys.exit(1) |
| 52 | + |
| 53 | + # --- 4. Fetch Full Data Series --- |
| 54 | + logging.info("\n--- Step 2: Starting Full Data Download ---") |
| 55 | + # For the full dataset, we use an empty key and no time parameters |
| 56 | + data_key = {} |
| 57 | + data_params = {} |
| 58 | + |
| 59 | + try: |
| 60 | + client.download_data_as_csv(dataflow_id=dataflow_id, |
| 61 | + key=data_key, |
| 62 | + params=data_params, |
| 63 | + output_path=data_output_path) |
| 64 | + logging.info( |
| 65 | + f"--- Successfully downloaded data to {data_output_path} ---") |
| 66 | + except Exception as e: |
| 67 | + logging.error(f"Failed to download data. Error: {e}") |
| 68 | + sys.exit(1) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + main() |
0 commit comments