-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrent_ingest.py
More file actions
67 lines (57 loc) · 1.76 KB
/
Copy pathrent_ingest.py
File metadata and controls
67 lines (57 loc) · 1.76 KB
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
"""Download or load median HDB rent for yield estimates."""
from __future__ import annotations
import argparse
import os
import sys
from pathlib import Path
from singapore_eda.constants import (
DEFAULT_RENT_CSV,
HDB_MEDIAN_RENT_CITATION_URL,
)
from singapore_eda.datastore import download_paginated_resource
def download_median_rent(
out_path: Path,
resource_id: str,
*,
max_rows: int | None = None,
skip_if_fresh_hours: float | None = None,
) -> int:
"""Paginated CKAN download; returns row count."""
return download_paginated_resource(
resource_id,
Path(out_path),
max_rows=max_rows,
skip_if_fresh_hours=skip_if_fresh_hours,
)
def main() -> None:
from singapore_eda import constants
rid = os.environ.get("HDB_MEDIAN_RENT_RESOURCE_ID", "").strip() or getattr(
constants, "HDB_MEDIAN_RENT_RESOURCE_ID", ""
)
if not rid:
print(
"Set HDB_MEDIAN_RENT_RESOURCE_ID to a data.gov.sg resource id.",
file=sys.stderr,
)
sys.exit(1)
p = argparse.ArgumentParser()
p.add_argument("-o", "--output", type=Path, default=DEFAULT_RENT_CSV)
p.add_argument("--max-rows", type=int, default=None)
p.add_argument(
"--skip-if-fresh-hours",
type=float,
default=None,
metavar="H",
help="Skip download if output exists and is newer than H hours (no API calls).",
)
args = p.parse_args()
n = download_median_rent(
args.output,
rid,
max_rows=args.max_rows,
skip_if_fresh_hours=args.skip_if_fresh_hours,
)
print(f"Wrote {n} rows to {args.output}", file=sys.stderr)
print(f"Source: {HDB_MEDIAN_RENT_CITATION_URL}", file=sys.stderr)
if __name__ == "__main__":
main()