From 382d388102e0e11c5743cf71fd9c84cd4988391e Mon Sep 17 00:00:00 2001 From: devlinwaluja <81210186+devlinwaluja@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:48:52 +0800 Subject: [PATCH 1/3] Delete pipeline/api.py --- pipeline/api.py | 70 ------------------------------------------------- 1 file changed, 70 deletions(-) delete mode 100644 pipeline/api.py diff --git a/pipeline/api.py b/pipeline/api.py deleted file mode 100644 index 815ba56..0000000 --- a/pipeline/api.py +++ /dev/null @@ -1,70 +0,0 @@ -# -# Flowmotion -# Pipeline -# Traffic Images API Client -# - -import asyncio - -import httpx - -from model import Camera, Location - - -class TrafficImageAPI: - """Data.gov.sg Traffic Images API Client.""" - - API_URL = "https://api.data.gov.sg/v1/transport/traffic-images" - - def __init__(self): - self._sync = httpx.Client() - self._async = httpx.AsyncClient() - - def get_cameras(self) -> list[Camera]: - """Get Traffic Camera metadata from traffic images API endpoint. - - Returns: - Parsed traffic camera metadata. - """ - # fetch traffic-images api endpoint - response = self._sync.get(TrafficImageAPI.API_URL) - response.raise_for_status() - meta = response.json() - return parse_cameras(meta) - - # parse traffic camera metadata - - def get_images(self, cameras: list[Camera]) -> list[bytes]: - """Get Traffic Camera images from given Traffic Cameras. - - Args: - cameras: - List of traffic cameras to retrieve traffic images from. - Returns: - List of JPEG image bytes camera captured by each given Camera. - """ - - async def fetch(): - responses = [self._async.get(camera.image_url) for camera in cameras] - images = [(await r).aread() for r in responses] - return await asyncio.gather(*images) - - return asyncio.run(fetch()) - - -def parse_cameras(meta: dict) -> list[Camera]: - meta = meta["items"][0] - retrieved_on = meta["timestamp"] - return [ - Camera( - camera_id=c["camera_id"], - retrieved_on=retrieved_on, - captured_on=c["timestamp"], - image_url=c["image"], - location=Location( - longitude=c["location"]["longitude"], - latitude=c["location"]["latitude"], - ), - ) - for c in meta["cameras"] - ] From 88a3681d701a15a0a53c254aa08b4d1b91325357 Mon Sep 17 00:00:00 2001 From: devlinwaluja <81210186+devlinwaluja@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:50:36 +0800 Subject: [PATCH 2/3] Changed api.py APIClient serves as a blueprint for APIClient instances. This acts according to the UML Diagram, and improves readability through simplicity. Proposed replacement of api.py --- pipeline/APIClient.py | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 pipeline/APIClient.py diff --git a/pipeline/APIClient.py b/pipeline/APIClient.py new file mode 100644 index 0000000..fffb7d9 --- /dev/null +++ b/pipeline/APIClient.py @@ -0,0 +1,51 @@ + +import requests + +API_URL = "https://api.data.gov.sg/v1/transport/traffic-images" + +class APIClient(): + def __init__(self, url="No API URL"): + self.url = url + self.timestamp = None + self.api_status = "Unverified" + self.metadata = None + + # Get API response + response = requests.get(self.url) + response_json = response.json() + self.metadata = response_json + + # Get and set API status + self.api_status = self.metadata["api_info"]["status"] + + # Get and set timestamp + self.timestamp = self.metadata["items"][0]["timestamp"] + + print(f"The API status is: {self.api_status}") + print(f"The API was called at: {self.timestamp}") + + def extract_image(self, camera_id): + # Debugging step to ensure camera_id is correctly parsed + #print(f"Searching for camera_id: {camera_id}") + + # Loop through the items and cameras to find the correct camera_id + for item in self.metadata["items"]: + for camera in item["cameras"]: + # Debugging step to print the camera_id being checked + #print(f"Checking camera_id: {camera['camera_id']}") + + if camera["camera_id"] == str(camera_id): + #print(f"Found camera_id: {camera_id}") + return camera["image"] # Return the image URL if the camera ID matches + + # If camera ID is not found + return f"Camera ID {camera_id} not found." + +#Debugging +#client = APIClient(API_URL) +#image_url = client.extract_image(1111) +#print("Camera Image URL:", image_url) + + + + From 06da117ed38063138bf94a19c80bfdb5a5eb23b8 Mon Sep 17 00:00:00 2001 From: devlinwaluja <81210186+devlinwaluja@users.noreply.github.com> Date: Fri, 27 Sep 2024 12:33:42 +0800 Subject: [PATCH 3/3] Added Congestion, Rating, TrafficImage --- pipeline/Congestion.py | 25 +++++++++++++++++++++++++ pipeline/Rating.py | 14 ++++++++++++++ pipeline/TrafficImage.py | 13 +++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 pipeline/Congestion.py create mode 100644 pipeline/Rating.py create mode 100644 pipeline/TrafficImage.py diff --git a/pipeline/Congestion.py b/pipeline/Congestion.py new file mode 100644 index 0000000..16183b6 --- /dev/null +++ b/pipeline/Congestion.py @@ -0,0 +1,25 @@ +import APIClient +import Rating + +class Congestion: + def __init__(self, congestion_rating=None, captured_on=None): + self.congestion_rating = congestion_rating + self.captured_on = captured_on + + + def add_datetime(self, apiclient_instance): + self.captured_on = apiclient_instance.timestamp + + def add_congestion_rating(self, rating): + self.congestion_rating = rating + + def is_heavy_traffic(self): + if self.congestion_rating == None: + return "Error: No congestion rating has been set!" + elif self.congestion_rating < 0.5: + return False + else: + return True + + + diff --git a/pipeline/Rating.py b/pipeline/Rating.py new file mode 100644 index 0000000..b22e8f9 --- /dev/null +++ b/pipeline/Rating.py @@ -0,0 +1,14 @@ +class Rating: + def __init__(self, rated_on=None, rated_by=None, rating_value=None): + self.rated_on = rated_on + self.rated_by = rated_by + self.rating_value = rating_value + + def add_rating_datetime(self, rated_on): + self.rated_on = rated_on + + def add_rater(self, rated_by): + self.rated_by = rated_by + + def add_rating_value(self, rating): + self.rating_value = rating \ No newline at end of file diff --git a/pipeline/TrafficImage.py b/pipeline/TrafficImage.py new file mode 100644 index 0000000..db4ba04 --- /dev/null +++ b/pipeline/TrafficImage.py @@ -0,0 +1,13 @@ +import APIClient + +class TrafficImage: + def __init__(self, image_url,processed=False, congestion_rating=None): + self.image_url = image_url + self.processed = processed + self.conestion_rating = congestion_rating + + def is_processed(self): + self.processed = True + + def add_congestion_rating(self, congestion_rating): + self.conestion_rating = congestion_rating