parameterize routing logic - extract api key as env var; allow searching dest by address; limit the number of listings to process to prevent accidental api key usage
This commit is contained in:
parent
57f477c54d
commit
482fff689b
5 changed files with 116 additions and 89 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import asyncio
|
||||
import os
|
||||
import pathlib
|
||||
import click
|
||||
import importlib
|
||||
|
|
@ -7,6 +8,7 @@ from rec.districts import get_districts
|
|||
from data_access import Listing
|
||||
import csv_exporter
|
||||
from rec.query import ListingType, FurnishType
|
||||
from rec.routing import API_KEY_ENVIRONMENT_VARIABLE, TravelMode
|
||||
|
||||
dump_listings_module = importlib.import_module('1_dump_listings')
|
||||
dump_detail_module = importlib.import_module('2_dump_detail')
|
||||
|
|
@ -148,12 +150,46 @@ def detect_floorplan(ctx: click.core.Context):
|
|||
|
||||
|
||||
@cli.command()
|
||||
@click.option(
|
||||
'--destination-address',
|
||||
'-d',
|
||||
help='Destination address for routing',
|
||||
required=True,
|
||||
type=click.STRING,
|
||||
)
|
||||
@click.option(
|
||||
'--travel-mode',
|
||||
'-m',
|
||||
help='Travel mode for routing',
|
||||
type=click.Choice(
|
||||
TravelMode.__members__.keys(),
|
||||
case_sensitive=False,
|
||||
),
|
||||
required=True,
|
||||
)
|
||||
@click.option(
|
||||
'--limit',
|
||||
'-l',
|
||||
help='Limit the number of listings to process',
|
||||
type=click.IntRange(min=1),
|
||||
default=1, # by default limit to 1 to avoid accidental API usage
|
||||
)
|
||||
@click.pass_context
|
||||
def routing(ctx: click.core.Context):
|
||||
def routing(ctx: click.core.Context, destination_address: str,
|
||||
travel_mode: str, limit: int):
|
||||
data_dir = ctx.obj['data_dir']
|
||||
click.echo(f'Running routing for listings in {data_dir}')
|
||||
click.echo(f'Running routing for the first {limit} listings in {data_dir}')
|
||||
listing_paths = sorted(list(pathlib.Path(data_dir).glob("*/listing.json")))
|
||||
routing_module.calculate_route(listing_paths)
|
||||
listing_paths = listing_paths[:limit]
|
||||
if os.environ.get(API_KEY_ENVIRONMENT_VARIABLE) is None:
|
||||
raise click.exceptions.MissingParameter(
|
||||
f'{API_KEY_ENVIRONMENT_VARIABLE} environment variable is not set. '
|
||||
'Please set it to your API key for the routing service.')
|
||||
routing_module.calculate_route(
|
||||
listing_paths,
|
||||
destination_address,
|
||||
TravelMode[travel_mode],
|
||||
)
|
||||
|
||||
|
||||
@cli.command()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue