|
| 1 | +import json |
| 2 | + |
| 3 | +from django.core.management.base import BaseCommand |
| 4 | +from structlog import get_logger |
| 5 | + |
| 6 | +from ...scraper import get_amendments, get_contractors, get_contracts, get_entities |
| 7 | + |
| 8 | +logger = get_logger("contratospr.commands.download_contracts") |
| 9 | + |
| 10 | + |
| 11 | +def get_contracts_by_entity(entity): |
| 12 | + offset = 0 |
| 13 | + total_records = 0 |
| 14 | + limit = 1000 |
| 15 | + |
| 16 | + entity_id = entity["Code"] |
| 17 | + entity_name = entity["Name"].strip() |
| 18 | + |
| 19 | + while offset <= total_records: |
| 20 | + logger.info( |
| 21 | + "Scraping contracts", |
| 22 | + limit=limit, |
| 23 | + entity_id=entity_id, |
| 24 | + entity_name=entity_name, |
| 25 | + offset=offset, |
| 26 | + total_records=total_records, |
| 27 | + ) |
| 28 | + |
| 29 | + contracts_json = get_contracts(offset, limit, entity_id=entity_id) |
| 30 | + |
| 31 | + with open(f"data/contracts-{entity_id}-{offset}.json", "w+") as f: |
| 32 | + json.dump(contracts_json, f) |
| 33 | + |
| 34 | + expanded_contracts = [] |
| 35 | + |
| 36 | + for contract_data in contracts_json.get("data", []): |
| 37 | + try: |
| 38 | + logger.info( |
| 39 | + "Getting contractors", contract_id=contract_data["ContractId"] |
| 40 | + ) |
| 41 | + contract_data["_Contractors"] = get_contractors( |
| 42 | + contract_data["ContractId"] |
| 43 | + ) |
| 44 | + contract_data["_Amendments"] = None |
| 45 | + |
| 46 | + if contract_data["HasAmendments"]: |
| 47 | + logger.info( |
| 48 | + "Getting amendments", |
| 49 | + contract_number=contract_data["ContractNumber"], |
| 50 | + entity_id=contract_data["EntityId"], |
| 51 | + ) |
| 52 | + contract_data["_Amendments"] = get_amendments( |
| 53 | + contract_data["ContractNumber"], contract_data["EntityId"] |
| 54 | + ) |
| 55 | + except Exception as exc: |
| 56 | + logger.info( |
| 57 | + "Error extending contract", |
| 58 | + contract_id=contract_data["ContractId"], |
| 59 | + exception=exc, |
| 60 | + ) |
| 61 | + |
| 62 | + expanded_contracts.append(contract_data) |
| 63 | + |
| 64 | + contracts_json["data"] = expanded_contracts |
| 65 | + |
| 66 | + with open(f"data/contracts-{entity_id}-{offset}.json", "w+") as f: |
| 67 | + json.dump(contracts_json, f) |
| 68 | + |
| 69 | + if not total_records: |
| 70 | + total_records = contracts_json["recordsFiltered"] |
| 71 | + |
| 72 | + offset += limit |
| 73 | + |
| 74 | + |
| 75 | +def get_contracts_by_entities(entities): |
| 76 | + for entity in entities: |
| 77 | + get_contracts_by_entity(entity) |
| 78 | + |
| 79 | + |
| 80 | +class Command(BaseCommand): |
| 81 | + help = "Download contracts for entities from consultacontratos.ocpr.gov.pr" |
| 82 | + |
| 83 | + def add_arguments(self, parser): |
| 84 | + parser.add_argument("--file", nargs="?", type=str, default=None) |
| 85 | + |
| 86 | + def handle(self, *args, **options): |
| 87 | + entities_file_path = options.get("file") |
| 88 | + |
| 89 | + if entities_file_path: |
| 90 | + entities = json.load(open(entities_file_path)).get("Results", []) |
| 91 | + else: |
| 92 | + entities = get_entities() |
| 93 | + |
| 94 | + get_contracts_by_entities(entities) |
0 commit comments