|
| 1 | +"""app.services.location.localjson.py""" |
| 2 | +import csv |
| 3 | +import logging |
| 4 | +import os |
| 5 | +from datetime import datetime |
| 6 | +from pprint import pformat as pf |
| 7 | + |
| 8 | +from asyncache import cached |
| 9 | +from cachetools import TTLCache |
| 10 | + |
| 11 | +import app.io |
| 12 | +from ...caches import check_cache, load_cache |
| 13 | +from ...coordinates import Coordinates |
| 14 | +from ...location import TimelinedLocation |
| 15 | +from ...models import Timeline |
| 16 | +from ...utils import countries |
| 17 | +from ...utils import date as date_util |
| 18 | +from ...utils import httputils |
| 19 | +from . import LocationService |
| 20 | + |
| 21 | +LOGGER = logging.getLogger("services.location.localjson") |
| 22 | +PID = os.getpid() |
| 23 | + |
| 24 | +class LocalJsonLocationService(LocationService): |
| 25 | + """ |
| 26 | + Service for retrieving locations from local JSON file. |
| 27 | + """ |
| 28 | + |
| 29 | + async def get_all(self): |
| 30 | + # Get the locations. |
| 31 | + locations = await get_locations() |
| 32 | + return locations |
| 33 | + |
| 34 | + async def get(self, loc_id): # pylint: disable=arguments-differ |
| 35 | + # Get location at the index equal to provided id. |
| 36 | + locations = await self.get_all() |
| 37 | + |
| 38 | + return locations[loc_id] |
| 39 | + |
| 40 | + |
| 41 | +# --------------------------------------------------------------- |
| 42 | + |
| 43 | + |
| 44 | +LOCATIONS_PATH = "locations.json" |
| 45 | + |
| 46 | + |
| 47 | +@cached(cache=TTLCache(maxsize=1, ttl=1800)) |
| 48 | +async def get_locations(): |
| 49 | + """ |
| 50 | + Retrieves the locations from the categories. The locations are cached for 1 hour. |
| 51 | +
|
| 52 | + :returns: The locations. |
| 53 | + :rtype: List[Location] |
| 54 | + """ |
| 55 | + |
| 56 | + data_id = "localjson.locations" |
| 57 | + LOGGER.info(f"pid:{PID}: {data_id} Requesting data...") |
| 58 | + |
| 59 | + # Transform JSON to list of object |
| 60 | + raw_locations: list[dict] = app.io.load(LOCATIONS_PATH) |
| 61 | + locations: list[TimelinedLocation] = [] |
| 62 | + |
| 63 | + for _, raw_location in enumerate(raw_locations): |
| 64 | + coordinates = raw_location.get("coordinates", {}) |
| 65 | + timelines = raw_location.get("timelines", {}) |
| 66 | + confirmed_timelines = timelines.get("confirmed", {}).get("timeline", {}) |
| 67 | + deaths_timelines = timelines.get("deaths", {}).get("timeline", {}) |
| 68 | + recovered_timelines = timelines.get("recovered", {}).get("timeline", {}) |
| 69 | + |
| 70 | + locations.append( |
| 71 | + TimelinedLocation( |
| 72 | + id=raw_location.get("id"), |
| 73 | + country=raw_location.get("country"), |
| 74 | + province=raw_location.get("province"), |
| 75 | + coordinates=Coordinates( |
| 76 | + latitude=coordinates.get("latitude"), |
| 77 | + longitude=coordinates.get("longitude") |
| 78 | + ), |
| 79 | + last_updated=datetime.utcnow().isoformat() + "Z", |
| 80 | + timelines={ |
| 81 | + "confirmed": Timeline( |
| 82 | + timeline={ |
| 83 | + date: amount |
| 84 | + for date, amount in confirmed_timelines.items() |
| 85 | + } |
| 86 | + ), |
| 87 | + "deaths": Timeline( |
| 88 | + timeline={ |
| 89 | + date: amount |
| 90 | + for date, amount in deaths_timelines.items() |
| 91 | + } |
| 92 | + ), |
| 93 | + "recovered": Timeline( |
| 94 | + timeline={ |
| 95 | + date: amount |
| 96 | + for date, amount in recovered_timelines.items() |
| 97 | + } |
| 98 | + ), |
| 99 | + }, |
| 100 | + ) |
| 101 | + ) |
| 102 | + |
| 103 | + LOGGER.info(f"{data_id} Data normalized") |
| 104 | + |
| 105 | + return locations |
0 commit comments