File size: 1,284 Bytes
d710a81
 
92cf271
 
 
 
 
 
 
 
 
 
 
 
d710a81
92cf271
 
 
d710a81
 
 
 
 
 
 
 
92cf271
d710a81
 
 
92cf271
d710a81
 
92cf271
d710a81
 
 
 
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
from __future__ import annotations

import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
LOGGER = logging.getLogger(__name__)


from pathlib import Path
ROOT_DIR = Path(__file__).resolve().parents[2]

from _overpass_utils import build_arg_parser, build_india_query, fetch_elements, normalize_row, write_rows


DEFAULT_OUTPUT = ROOT_DIR / 'chatbot_service' / 'data' / 'hospitals' / 'hospital_directory.csv'
SELECTORS = [
    'node["amenity"~"hospital|clinic"](area.searchArea);',
    'way["amenity"~"hospital|clinic"](area.searchArea);',
    'relation["amenity"~"hospital|clinic"](area.searchArea);',
]


def main() -> None:
    parser = build_arg_parser('Fetch India hospital and clinic data from Overpass.', DEFAULT_OUTPUT)
    args = parser.parse_args()

    query = build_india_query(SELECTORS, timeout=args.timeout)
    elements = fetch_elements(query, endpoint=args.endpoint, timeout=args.timeout)
    rows = [
        row
        for element in elements
        if (row := normalize_row(element, default_type='hospital', fallback_name='Unnamed hospital')) is not None
    ]
    count = write_rows(args.output, rows)
    LOGGER.info(f'Saved {count} hospital records to {args.output}')


if __name__ == '__main__':
    main()