Datasets:
Download export-from-scraper.py from fulldecent/septa-regionalrail-otp: direct link, hf CLI and curl.
- Browser
- Download file 2.22 kB
-
https://huggingface.co/datasets/fulldecent/septa-regionalrail-otp/resolve/main/export-from-scraper.py
- Command line
-
hf download hf://datasets/fulldecent/septa-regionalrail-otp/export-from-scraper.py
-
curl -L -o export-from-scraper.py https://huggingface.co/datasets/fulldecent/septa-regionalrail-otp/resolve/main/export-from-scraper.py
2.22 kB
| #!/usr/bin/env python3 | |
| """Export all trainview SQLite databases to a Hugging Face parquet dataset partitioned by year.""" | |
| import glob | |
| import os | |
| import duckdb | |
| DB_DIR = os.path.join(os.path.dirname(__file__), "..", "website", "databases") | |
| OUTPUT_DIR = os.path.join(os.path.dirname(__file__), "..", "huggingface") | |
| def main(): | |
| db_files = sorted(glob.glob(os.path.join(DB_DIR, "trainview-*.db"))) | |
| if not db_files: | |
| raise SystemExit("No trainview-*.db files found") | |
| print(f"Found {len(db_files)} trainview databases") | |
| con = duckdb.connect() | |
| con.execute("INSTALL sqlite; LOAD sqlite;") | |
| con.execute("SET sqlite_all_varchar = true;") | |
| # ATTACH each SQLite file so DuckDB reads them directly (no Python materialization) | |
| union_parts = [] | |
| for i, db_file in enumerate(db_files): | |
| alias = f"db{i}" | |
| con.execute(f"ATTACH '{db_file}' AS {alias} (TYPE sqlite);") | |
| union_parts.append( | |
| f"SELECT day AS service_date, time, train AS train_number, lateness " | |
| f"FROM {alias}.trainview" | |
| ) | |
| union_query = " UNION ALL ".join(union_parts) | |
| print("Writing hive-partitioned parquet...") | |
| os.makedirs(OUTPUT_DIR, exist_ok=True) | |
| con.execute(f""" | |
| COPY ( | |
| SELECT | |
| service_date::DATE AS service_date, | |
| time::VARCHAR AS time, | |
| train_number::VARCHAR AS train_number, | |
| lateness::INT32 AS lateness, | |
| YEAR(service_date::DATE) AS year | |
| FROM ({union_query}) | |
| ORDER BY service_date, time, train_number | |
| ) | |
| TO '{OUTPUT_DIR}' | |
| (FORMAT PARQUET, | |
| PARTITION_BY (year), | |
| OVERWRITE_OR_IGNORE, | |
| COMPRESSION 'zstd', | |
| ROW_GROUP_SIZE 100000); | |
| """) | |
| # Summary | |
| result = con.execute(f""" | |
| SELECT YEAR(service_date::DATE) AS year, COUNT(*) AS rows | |
| FROM ({union_query}) | |
| GROUP BY year | |
| ORDER BY year | |
| """).fetchall() | |
| print("Rows per year:") | |
| for year, count in result: | |
| print(f" {year}: {count:,}") | |
| con.close() | |
| print(f"\nParquet dataset written to {os.path.abspath(OUTPUT_DIR)}") | |
| if __name__ == "__main__": | |
| main() | |