|
| 1 | +import os |
| 2 | +from datetime import datetime |
| 3 | + |
| 4 | +from operators.ntd_xlsx_list_tabs_operator import NTDXLSXListTabsOperator |
| 5 | +from operators.ntd_xlsx_to_gcs_operator import NTDXLSXToGCSOperator |
| 6 | +from operators.ntd_xlsx_to_jsonl_operator import NTDXLSXToJSONLOperator |
| 7 | + |
| 8 | +from airflow import DAG, XComArg |
| 9 | +from airflow.decorators import task |
| 10 | +from airflow.operators.latest_only import LatestOnlyOperator |
| 11 | + |
| 12 | +NTD_PRODUCTS = [ |
| 13 | + { |
| 14 | + "type": "annual_database_agency_information", |
| 15 | + "short_type": "_2022_agency_information", |
| 16 | + "year": 2022, |
| 17 | + "url": "https://www.transit.dot.gov/ntd/data-product/2022-annual-database-agency-information", |
| 18 | + }, |
| 19 | +] |
| 20 | + |
| 21 | +with DAG( |
| 22 | + dag_id="download_and_parse_ntd_xlsx", |
| 23 | + # Every day at midnight |
| 24 | + schedule="0 0 * * *", |
| 25 | + start_date=datetime(2025, 11, 1), |
| 26 | + catchup=False, |
| 27 | + tags=["ntd"], |
| 28 | +): |
| 29 | + latest_only = LatestOnlyOperator(task_id="latest_only", depends_on_past=False) |
| 30 | + |
| 31 | + @task |
| 32 | + def create_download_kwargs(ntd_product): |
| 33 | + return { |
| 34 | + "source_url": ntd_product["url"], |
| 35 | + "destination_path": os.path.join( |
| 36 | + f"{ntd_product['type']}_raw", |
| 37 | + f"{ntd_product['year']}", |
| 38 | + "dt={{ dt }}", |
| 39 | + "execution_ts={{ ts }}", |
| 40 | + f"{ntd_product['year']}__{ntd_product['type']}_raw.xlsx", |
| 41 | + ), |
| 42 | + } |
| 43 | + |
| 44 | + download_kwargs = create_download_kwargs.expand(ntd_product=NTD_PRODUCTS) |
| 45 | + |
| 46 | + download_xlsx = NTDXLSXToGCSOperator( |
| 47 | + task_id="download_to_gcs", |
| 48 | + destination_bucket=os.environ.get("CALITP_BUCKET__NTD_XLSX_DATA_PRODUCTS__RAW"), |
| 49 | + ).expand_kwargs(download_kwargs) |
| 50 | + |
| 51 | + xlsx_tabs = NTDXLSXListTabsOperator( |
| 52 | + task_id="ntd_xlsx_list_tabs", |
| 53 | + source_bucket=os.environ.get("CALITP_BUCKET__NTD_XLSX_DATA_PRODUCTS__RAW"), |
| 54 | + ).expand_kwargs( |
| 55 | + download_xlsx.map(lambda dl: {"source_path": dl["destination_path"]}) |
| 56 | + ) |
| 57 | + |
| 58 | + @task |
| 59 | + def create_parse_kwargs(xlsx_tab_ntd_products) -> dict: |
| 60 | + xlsx_tab, ntd_products = xlsx_tab_ntd_products |
| 61 | + tab_name = ( |
| 62 | + xlsx_tab["tab"] |
| 63 | + .lower() |
| 64 | + .replace(" ", "_") |
| 65 | + .replace("(", "_") |
| 66 | + .replace(")", "_") |
| 67 | + .replace(":", "_") |
| 68 | + .replace("-", "_") |
| 69 | + ) |
| 70 | + result = [] |
| 71 | + for ntd_product in ntd_products: |
| 72 | + result.append( |
| 73 | + { |
| 74 | + "tab": xlsx_tab["tab"], |
| 75 | + "source_path": xlsx_tab["source_path"], |
| 76 | + "destination_path": os.path.join( |
| 77 | + ntd_product["type"], |
| 78 | + ntd_product["year"], |
| 79 | + xlsx_tab["tab"], |
| 80 | + "dt={{ dt }}", |
| 81 | + "execution_ts={{ ts }}", |
| 82 | + f"{ntd_product['year']}__{ntd_product['type']}__{tab_name}.jsonl.gz", |
| 83 | + ), |
| 84 | + } |
| 85 | + ) |
| 86 | + return result |
| 87 | + |
| 88 | + parse_kwargs = create_parse_kwargs.expand( |
| 89 | + xlsx_tab=XComArg(xlsx_tabs).zip(fillvalue=NTD_PRODUCTS) |
| 90 | + ) |
| 91 | + |
| 92 | + parse_xlsx = NTDXLSXToJSONLOperator.partial( |
| 93 | + task_id="xlsx_to_jsonl", |
| 94 | + source_bucket=os.environ.get("CALITP_BUCKET__NTD_XLSX_DATA_PRODUCTS__RAW"), |
| 95 | + destination_bucket=os.environ.get( |
| 96 | + "CALITP_BUCKET__NTD_XLSX_DATA_PRODUCTS__CLEAN" |
| 97 | + ), |
| 98 | + ).expand_kwargs(parse_kwargs) |
| 99 | + |
| 100 | + latest_only >> download_kwargs |
0 commit comments