71 lines
2.8 KiB
Python
71 lines
2.8 KiB
Python
"""Explicit-date v2 capture batches sharing the existing private local job store.
|
|
|
|
No browser, scheduler, source adaptation or Finance submission is performed.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from . import audit_arr_day as audit
|
|
from . import capture_job as jobs
|
|
from . import collect_arr_day as day
|
|
from . import collect_arr_source as source
|
|
from .rate_info import RateInfoReader
|
|
|
|
|
|
class DayProtocol:
|
|
@staticmethod
|
|
def identity(batch_id, options):
|
|
source.require(type(options) is day.Options, "invalid_day_options")
|
|
request = jobs.identity(batch_id, options)
|
|
request.update(version="arr-api-capture-job/v2", source_contract="arr-api-date-candidates/v2",
|
|
operations=day.OPERATIONS)
|
|
return request
|
|
|
|
@staticmethod
|
|
def collect(options, archive, reader_factory):
|
|
reader, rates = reader_factory(archive, options.hotel_id)
|
|
return day.collect(options, archive, reader, rates)
|
|
|
|
@staticmethod
|
|
def verify_completed(directory, attempt, options):
|
|
capture = directory / f"attempt-{attempt['attempt_no']:04d}"
|
|
verified = audit.VerifiedArchive(capture, attempt["manifest_sha256"])
|
|
source.require(verified.options == options, "completed_capture_context_mismatch")
|
|
audit.replay(verified)
|
|
return verified.result
|
|
|
|
|
|
def run_batch(root: Path, batch_id: str, options: day.Options, reader_factory) -> dict:
|
|
"""Factory returns (Reader, RateInfoReader) only when new reads are necessary."""
|
|
return jobs.run_batch(root, batch_id, options, reader_factory, protocol=DayProtocol)
|
|
|
|
|
|
def main(argv=None) -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--job-store", required=True, type=Path)
|
|
parser.add_argument("--batch-id", required=True)
|
|
parser.add_argument("--from-date", required=True)
|
|
parser.add_argument("--to-date", required=True)
|
|
parser.add_argument("--rate-date", required=True)
|
|
parser.add_argument("--hotel-id", required=True)
|
|
parser.add_argument("--credential-file", required=True, type=Path)
|
|
args = parser.parse_args(argv)
|
|
|
|
def reader_factory(archive, hotel_id):
|
|
key = source.load_key(args.credential_file)
|
|
transport = source.HTTPTransport(key)
|
|
return (source.Reader(archive, hotel_id, transport, key=key),
|
|
RateInfoReader(archive, hotel_id, transport, key=key))
|
|
|
|
result = run_batch(args.job_store, args.batch_id,
|
|
day.Options(args.from_date, args.to_date, args.rate_date, args.hotel_id), reader_factory)
|
|
print(json.dumps(result, ensure_ascii=False, allow_nan=False))
|
|
return 0 if result.get("candidate_capture_complete") else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|