def four_day_weather_forecast(self, date_time=None, dt=None): """Retrieve the latest 4 day weather forecast. Arguments: date_time (datetime): (optional) Specific date-time to retrieve the readings. Will be standardised to SGT timezone. If both `dt` and `date_time` are specified, then `date_time` is used. dt (date): (optional) Specific date to retrieve the readings. If both `dt` and `date_time` are specified, then `dt` is NOT used. Returns: (dict) Weather forecast for the next 4 days by area. References: https://data.gov.sg/dataset/weather-forecast?resource_id=4df6d890-f23e-47f0-add1-fd6d580447d1 """ kwargs = { 'date_time': date_time, 'date': dt, } kwargs = self.prepare_kwargs(kwargs) four_day_weather_forecast = net.send_request( FOUR_DAY_WEATHER_FORECAST_API_ENDPOINT, **kwargs, ) return four_day_weather_forecast
def relative_humidity(self, date_time=None, dt=None): """Get relative humidity readings across Singapore. Arguments: date_time (datetime): (optional) Specific date-time to retrieve the readings. Will be standardised to SGT timezone. If both `dt` and `date_time` are specified, then `date_time` is used. dt (date): (optional) Specific date to retrieve the readings. If both `dt` and `date_time` are specified, then `dt` is NOT used. Returns: (dict) Readings of relative humidity by station. References: https://data.gov.sg/dataset/realtime-weather-readings?resource_id=59eb2883-2ceb-4d16-85f0-7e3a3176ef46 """ kwargs = { 'date_time': date_time, 'date': dt, } kwargs = self.prepare_kwargs(kwargs) relative_humidity = net.send_request( RELATIVE_HUMIDITY_API_ENDPOINT, **kwargs, ) return relative_humidity
def air_temperature(self, date_time=None, dt=None): """Get air temperature readings across Singapore. Arguments: date_time (datetime): (optional) Specific date-time to retrieve the readings. Will be standardised to SGT timezone. If both `dt` and `date_time` are specified, then `date_time` is used. dt (date): (optional) Specific date to retrieve the readings. If both `dt` and `date_time` are specified, then `dt` is NOT used. Returns: (dict) Readings of air temperature by station. References: https://data.gov.sg/dataset/realtime-weather-readings?resource_id=17494bed-23e9-4b3b-ae89-232f87987163 """ kwargs = { 'date_time': date_time, 'date': dt, } kwargs = self.prepare_kwargs(kwargs) air_temperature = net.send_request( AIR_TEMPERATURE_API_ENDPOINT, **kwargs, ) return air_temperature
def two_hour_weather_forecast(self, date_time=None, dt=None): """Retrieve the latest two hour weather forecast across Singapore. Arguments: date_time (datetime): (optional) Specific date-time to retrieve the readings. Will be standardised to SGT timezone. If both `dt` and `date_time` are specified, then `date_time` is used. dt (date): (optional) Specific date to retrieve the readings. If both `dt` and `date_time` are specified, then `dt` is NOT used. Returns: (dict) Weather forecast for the next 2 hours by area. References: https://data.gov.sg/dataset/weather-forecast?resource_id=571ef5fb-ed31-48b2-85c9-61677de42ca9 """ kwargs = { 'date_time': date_time, 'date': dt, } kwargs = self.prepare_kwargs(kwargs) two_hour_weather_forecast = net.send_request( TWO_HOUR_WEATHER_FORECAST_API_ENDPOINT, **kwargs, ) return two_hour_weather_forecast
def twenty_four_hour_weather_forecast(self, date_time=None, dt=None): """Retrieve the latest 24 hour weather forecast across Singapore. Arguments: date_time (datetime): (optional) Specific date-time to retrieve the readings. Will be standardised to SGT timezone. If both `dt` and `date_time` are specified, then `date_time` is used. dt (date): (optional) Specific date to retrieve the readings. If both `dt` and `date_time` are specified, then `dt` is NOT used. Returns: (dict) Weather forecast for the next 24 hours by area. References: https://data.gov.sg/dataset/weather-forecast?resource_id=9a8bd97e-0e38-46b7-bc39-9a2cb4a53a62 """ kwargs = { 'date_time': date_time, 'date': dt, } kwargs = self.prepare_kwargs(kwargs) twenty_four_hour_weather_forecast = net.send_request( TWENTY_FOUR_HOUR_WEATHER_FORECAST_API_ENDPOINT, **kwargs, ) return twenty_four_hour_weather_forecast
def wind_speed(self, date_time=None, dt=None): """Get wind speed readings across Singapore. Arguments: date_time (datetime): (optional) Specific date-time to retrieve the readings. Will be standardised to SGT timezone. If both `dt` and `date_time` are specified, then `date_time` is used. dt (date): (optional) Specific date to retrieve the readings. If both `dt` and `date_time` are specified, then `dt` is NOT used. Returns: (dict) Readings of wind speed by station. References: https://data.gov.sg/dataset/realtime-weather-readings?resource_id=16035f22-37b4-4a5c-b024-ca2381f11b48 """ kwargs = { 'date_time': date_time, 'date': dt, } kwargs = self.prepare_kwargs(kwargs) wind_speed = net.send_request( WIND_SPEED_API_ENDPOINT, **kwargs, ) return wind_speed
def __paginate_search_datastore(self, url, **kwargs): """Search one page of data in a resource. This function calls itself recursively to get all records. In the recursion, only `url` is specified since it contains all of the necessary parameters. Arguments: url (str): Datastore Search URL. kwargs (dict): Arguments that are set in the calling function. See search_datastore(). Returns: (dict) One page of resources that match the search criteria. The records are concatenated with the previous called page's, then returned altogether. """ page_data = net.send_request(url, **kwargs) # it is possible to paginate "forever" by following result._links.next # so check if there are any records in the current page first if len(page_data['result']['records']) > 0: # get the next page of results next_link = page_data['result']['_links']['next'] next_url = BASE_CKAN_DOMAIN + next_link next_page_data = self.__paginate_search_datastore(next_url) page_data['result']['records'] += \ next_page_data['result']['records'] return page_data
def rainfall(self, date_time=None, dt=None): """Get rainfall readings across Singapore. Arguments: date_time (datetime): (optional) Specific date-time to retrieve the readings. Will be standardised to SGT timezone. If both `dt` and `date_time` are specified, then `date_time` is used. dt (date): (optional) Specific date to retrieve the readings. If both `dt` and `date_time` are specified, then `dt` is NOT used. Returns: (dict) Readings of rainfall by station. References: https://data.gov.sg/dataset/realtime-weather-readings?resource_id=8bd37e06-cdd7-4ca4-9ad8-5754eb70a33d """ kwargs = { 'date_time': date_time, 'date': dt, } kwargs = self.prepare_kwargs(kwargs) rainfall = net.send_request( RAINFALL_API_ENDPOINT, **kwargs, ) return rainfall
def psi(self, date_time=None, dt=None): """Retrieve the latest PSI information in Singapore. Arguments: date_time (datetime): (optional) Specific date-time to retrieve the readings. Will be standardised to SGT timezone. If both `dt` and `date_time` are specified, then `date_time` is used. dt (date): (optional) Specific date to retrieve the readings. If both `dt` and `date_time` are specified, then `dt` is NOT used. Returns: (dict) Readings of PSI by region. References: https://data.gov.sg/dataset/psi """ kwargs = { 'date_time': date_time, 'date': dt, } kwargs = self.prepare_kwargs(kwargs) psi_information = net.send_request( PSI_API_ENDPOINT, **kwargs, ) return psi_information
def wind_direction(self, date_time=None, dt=None): """Get wind direction readings across Singapore. Arguments: date_time (datetime): (optional) Specific date-time to retrieve the readings. Will be standardised to SGT timezone. If both `dt` and `date_time` are specified, then `date_time` is used. dt (date): (optional) Specific date to retrieve the readings. If both `dt` and `date_time` are specified, then `dt` is NOT used. Returns: (dict) Readings of wind direction by station. References: https://data.gov.sg/dataset/realtime-weather-readings?resource_id=5dcf8aa5-cf6a-44e4-b359-1173eecfdf4c """ kwargs = { 'date_time': date_time, 'date': dt, } kwargs = self.prepare_kwargs(kwargs) wind_direction = net.send_request( WIND_DIRECTION_API_ENDPOINT, **kwargs, ) return wind_direction
def resource_show(self, resource_id): """Return the metadata of a resource. Arguments: resource_id (str): ID of the resource. Returns: (dict) Metadata of the requested resource. References: https://data.gov.sg/dataset/ckan-resource-show """ resource = net.send_request( RESOURCE_SHOW_CKAN_ENDPOINT, id=resource_id, ) return resource
def package_show(self, package_id): """Return the metadata of a dataset (package) and its resources. Arguments: package_id (str): ID or name of the dataset. Returns: (dict) Metadata and resources of the requested package. References: https://data.gov.sg/dataset/ckan-package-show """ package = net.send_request( PACKAGE_SHOW_CKAN_ENDPOINT, id=package_id, ) return package
def uv_index(self, date_time=None): """Retrieve the latest UV index information in Singapore. Arguments: date_time (datetime): (optional) Specific date-time to retrieve the readings. Will be standardised to SGT timezone. Returns: (dict) Readings of UV Index by station. References: https://data.gov.sg/dataset/ultraviolet-index-uvi """ uv_index_information = net.send_request( UV_INDEX_API_ENDPOINT, date_time=date_time, ) return uv_index_information
def trademarks(self, date=None): """Get trademark applications lodged with IPOS in Singapore. Arguments: date (date): (optional) Specific date to retrieve the lodged trademarks on that date. Can be in any timezone (will be standardised to SGT.) Returns: (dict) Trademark applications that have been lodged. References: https://data.gov.sg/dataset/ipos-apis?resource_id=1522db0e-808b-48ea-9869-fe5adc566585 """ lodged_trademarks = net.send_request( IPOS_TRADEMARKS_API_ENDPOINT, lodgement_date=date, ) return lodged_trademarks
def patents(self, date=None): """Get patent applications lodged with IPOS in Singapore. Arguments: date (date): (optional) Specific date to retrieve the lodged patents on that date. Can be in any timezone (will be standardised to SGT.) Returns: (dict) Patent applications that have been lodged. References: https://data.gov.sg/dataset/ipos-apis?resource_id=6a030bf2-22da-4621-8ab0-9a5956a30ef3 """ lodged_patents = net.send_request( IPOS_PATENTS_API_ENDPOINT, lodgement_date=date, ) return lodged_patents
def designs(self, date=None): """Get design applications lodged with IPOS in Singapore. Arguments: date (date): (optional) Specific date to retrieve the lodged designs on that date. Can be in any timezone (will be standardised to SGT.) Returns: (dict) Design applications that have been lodged. References: https://data.gov.sg/dataset/ipos-apis?resource_id=adf6222f-955b-4a76-892f-802a396844a1 """ lodged_designs = net.send_request( IPOS_DESIGNS_API_ENDPOINT, lodgement_date=date, ) return lodged_designs
def traffic_images(self, date_time=None): """Get the latest images from traffic cameras all around Singapore. Arguments: date_time (datetime): (optional) Specific date-time to retrieve the images at that time. Can be in any timezone (will be standardised to SGT.) Returns: (dict) Images from traffic cameras. References: https://data.gov.sg/dataset/traffic-images """ traffic_images = net.send_request( TRAFFIC_IMAGES_API_ENDPOINT, date_time=date_time, ) return traffic_images
def carpark_availability(self, date_time=None): """Get the latest carpark availability in Singapore. Arguments: date_time (datetime): (optional) Specific date-time to retrieve the availabilities at that time. Can be in any timezone (will be standardised to SGT.) Returns: (dict) Available carpark spaces. References: https://data.gov.sg/dataset/carpark-availability """ carpark_availabilities = net.send_request( CARPARK_AVAILABILITY_API_ENDPOINT, date_time=date_time, ) return carpark_availabilities
def taxi_availability(self, date_time=None): """Get locations of available taxis in Singapore. Arguments: date_time (datetime): (optional) Specific date-time to retrieve the availabilities at that time. Can be in any timezone (will be standardised to SGT.) Returns: (dict) GeoJSON of the taxi availabilities. References: https://data.gov.sg/dataset/taxi-availability """ taxi_availabilities = net.send_request( TAXI_AVAILABILITY_API_ENDPOINT, accept_type='vnd.geo+json', date_time=date_time, ) return taxi_availabilities
def package_list(self, limit=None, offset=None): """Return a list of datasets on data.gov.sg. Arguments: limit (int): (optional) Maximum number of packages to return per page. Default: None, i.e. return all packages. offset (int): (optional) Offset this number of packages. Returns: (dict) Metadata and list of packages. References: https://data.gov.sg/dataset/ckan-package-list """ package_list = net.send_request( PACKAGE_LIST_CKAN_ENDPOINT, limit=limit, offset=offset, ) return package_list
def test_send_request_with_bad_accept_type(): with pytest.raises(ValueError): _ = net.send_request( 'https://api.data.gov.sg/v1/transport/taxi-availability', 'csv', )
def test_send_request(url, accept_type, kwargs): response_content = net.send_request(url, accept_type, **kwargs) assert isinstance(response_content, dict)