|
6 | 6 | from src.models.capacity import Capacity |
7 | 7 | from src.utils.constants import ( |
8 | 8 | C2C_URL, |
| 9 | + CRC_URL_NEW, |
9 | 10 | CAPACITY_MARKER_COUNTS, |
10 | 11 | CAPACITY_MARKER_NAMES, |
11 | 12 | CAPACITY_MARKER_UPDATED, |
|
14 | 15 | ) |
15 | 16 | from src.utils.utils import get_facility_id, unix_time |
16 | 17 |
|
17 | | - |
18 | | -def fetch_capacities(): |
| 18 | +# Legacy scraper from old webpage using CRC_URL |
| 19 | +def fetch_capacities_old(): |
19 | 20 | """ |
20 | 21 | Fetch capacities for all facilities from Connect2Concepts. |
21 | 22 | """ |
@@ -49,6 +50,44 @@ def fetch_capacities(): |
49 | 50 | # Add to sheets |
50 | 51 | add_single_capacity(count, facility_id, percent, updated) |
51 | 52 |
|
| 53 | +# New scraper from new API using CRC_URL_NEW |
| 54 | +def fetch_capacities(): |
| 55 | + """Fetch capacities from the new JSON API endpoint.""" |
| 56 | + try: |
| 57 | + headers = { |
| 58 | + "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0" |
| 59 | + } |
| 60 | + |
| 61 | + response = requests.get(CRC_URL_NEW, headers=headers) |
| 62 | + facilities = response.json() |
| 63 | + |
| 64 | + for facility in facilities: |
| 65 | + try: |
| 66 | + facility_name = facility["LocationName"] |
| 67 | + |
| 68 | + # Map API name to database name |
| 69 | + if facility_name not in CAPACITY_MARKER_NAMES: |
| 70 | + print(f"Warning: No name mapping for facility: {facility_name}") |
| 71 | + continue |
| 72 | + |
| 73 | + db_name = CAPACITY_MARKER_NAMES[facility_name] |
| 74 | + facility_id = get_facility_id(db_name) |
| 75 | + |
| 76 | + count = int(facility["LastCount"]) |
| 77 | + updated_str = facility["LastUpdatedDateAndTime"] |
| 78 | + total_capacity = int(facility["TotalCapacity"]) |
| 79 | + |
| 80 | + percent = count / total_capacity if total_capacity > 0 else 0.0 |
| 81 | + updated = datetime.strptime(updated_str.split(".")[0], "%Y-%m-%dT%H:%M:%S") |
| 82 | + |
| 83 | + add_single_capacity(count, facility_id, percent, updated) |
| 84 | + |
| 85 | + except Exception as e: |
| 86 | + print(f"Error processing facility {facility.get('LocationName', 'unknown')}: {str(e)}") |
| 87 | + |
| 88 | + except Exception as e: |
| 89 | + print(f"Error fetching capacities: {str(e)}") |
| 90 | + raise |
52 | 91 |
|
53 | 92 | def add_single_capacity(count, facility_id, percent, updated): |
54 | 93 | """ |
|
0 commit comments