def test_init_with_license_keys(self): met_office = MetOffice() self.assertIsNotNone(met_office) license_keys = LicenseKeys() license_keys.add_key('METOFFICE_API_KEY', "ABCDEFGHIJKL") met_office.check_for_license_keys(license_keys) self.assertIsNotNone(met_office._met_office_api)
def execute(self, bot, clientid, data): splits = data.split() if len(splits) < 4: return None if splits[0] == 'LOCATION': postcode = splits[1] else: return None if splits[2] == 'WHEN': when = splits[3] else: return None if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug("Getting weather for %s at time %s", postcode, when) googlemaps = GoogleMaps(bot.license_keys) latlng = googlemaps.get_latlong_for_location(postcode) if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug( "Weather - Calling external weather service for with extra data [%s]", data) met_office = MetOffice(bot.license_keys) observation = met_office.current_observation(latlng.latitude, latlng.longitude) return observation.get_latest().to_program_y_text()
def get_observation(self, bot, clientid, postcode, when): if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug("Getting weather observation for [%s] at time [%s]" % (postcode, when)) googlemaps = GoogleMaps(bot.license_keys) latlng = googlemaps.get_latlong_for_location(postcode) met_office = MetOffice(bot.license_keys) observation = met_office.current_observation(latlng.latitude, latlng.longitude) if observation is not None: return observation.get_latest().to_program_y_text() else: return None
def get_forecast24(self, bot, clientid, postcode, when): if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug( "Getting 24 hour weather forecast for [%s] at time [%s]" % (postcode, when)) googlemaps = GoogleMaps(bot.license_keys) latlng = googlemaps.get_latlong_for_location(postcode) met_office = MetOffice(bot.license_keys) forecast = met_office.daily_forecast(latlng.latitude, latlng.longitude) if forecast is not None: return forecast.get_latest().to_program_y_text() else: return None
def test_observation(self): met_office = MetOffice(self.license_keys) self.assertIsNotNone(met_office) observation = met_office.current_observation(self.lat, self.lng) self.assertIsNotNone(observation) report = observation.get_latest_report() self.assertIsNotNone(report) date = DateFormatter.year_month_day_now() report = observation.get_report_for_date(date) self.assertIsNotNone(report) datapoint = report.get_time_period_by_time("300") self.assertIsNotNone(datapoint) self.assertIsInstance(datapoint, ObservationDataPoint)
def test_threehourly_forecast(self): met_office = MetOffice(self.license_keys) self.assertIsNotNone(met_office) forecast = met_office.three_hourly_forecast(self.lat, self.lng) self.assertIsNotNone(forecast) report = forecast.get_latest_report() self.assertIsNotNone(report) date = DateFormatter.year_month_day_now() report = forecast.get_report_for_date(date) self.assertIsNotNone(report) datapoint = report.get_time_period_by_time("900") self.assertIsNotNone(datapoint) self.assertIsInstance(datapoint, ThreeHourlyForecastDataPoint)
def test_threehourly_forecast(self): met_office = MetOffice(self.license_keys) self.assertIsNotNone(met_office) met_office.set_three_hourly_forecast_response_file( os.path.dirname(__file__) + "/forecast_3hourly.json") forecast = met_office.three_hourly_forecast(self.lat, self.lng) self.assertIsNotNone(forecast) report = forecast.get_latest_report() self.assertIsNotNone(report) date = DateFormatter.year_month_day(2017, 4, 3) report = forecast.get_report_for_date(date) self.assertIsNotNone(report) datapoint = report.get_time_period_by_time("540") self.assertIsNotNone(datapoint) self.assertIsInstance(datapoint, ThreeHourlyForecastDataPoint)
def test_observation(self): met_office = MetOffice(self.license_keys) self.assertIsNotNone(met_office) met_office.set_current_observation_response_file( os.path.dirname(__file__) + "/observation.json") observation = met_office.current_observation(self.lat, self.lng) self.assertIsNotNone(observation) report = observation.get_latest_report() self.assertIsNotNone(report) date = DateFormatter.year_month_day(2017, 4, 3) report = observation.get_report_for_date(date) self.assertIsNotNone(report) datapoint = report.get_time_period_by_time("300") self.assertIsNotNone(datapoint) self.assertIsInstance(datapoint, ObservationDataPoint)
def test_daily_forecast(self): met_office = MetOffice(self.license_keys) self.assertIsNotNone(met_office) forecast = met_office.daily_forecast(self.lat, self.lng) self.assertIsNotNone(forecast) report = forecast.get_latest_report() self.assertIsNotNone(report) date = DateFormatter.year_month_day_now() report = forecast.get_report_for_date(date) self.assertIsNotNone(report) day_datapoint = report.get_time_period_by_type('Day') self.assertIsNotNone(day_datapoint) self.assertIsInstance(day_datapoint, DailyForecastDayDataPoint) night_datapoint = report.get_time_period_by_type('Night') self.assertIsNotNone(night_datapoint) self.assertIsInstance(night_datapoint, DailyForecastNightDataPoint)
def test_daily_forecast(self): met_office = MetOffice(self.license_keys) self.assertIsNotNone(met_office) met_office.set_daily_forecast_response_file( os.path.dirname(__file__) + "/forecast_daily.json") forecast = met_office.daily_forecast(self.lat, self.lng) self.assertIsNotNone(forecast) report = forecast.get_latest_report() self.assertIsNotNone(report) date = DateFormatter.year_month_day(2017, 4, 3) report = forecast.get_report_for_date(date) self.assertIsNotNone(report) day_datapoint = report.get_time_period_by_type('Day') self.assertIsNotNone(day_datapoint) self.assertIsInstance(day_datapoint, DailyForecastDayDataPoint) night_datapoint = report.get_time_period_by_type('Night') self.assertIsNotNone(night_datapoint) self.assertIsInstance(night_datapoint, DailyForecastNightDataPoint)
import os import metoffer from programy.utils.weather.metoffice import MetOffice if __name__ == '__main__': # Only to be used to create test data for unit aiml_tests from programy.utils.license.keys import LicenseKeys license_keys = LicenseKeys() license_keys.load_license_key_file(os.path.dirname(__file__) + '/../../../../bots/y-bot/config/license.keys') met_office = MetOffice(license_keys) lat = 56.0720397 lng = -3.1752001 log_to_file = False if log_to_file is True: met_office.nearest_location_observation_to_file(lat, lng, "observation.json") met_office.nearest_location_forecast_to_file(lat, lng, metoffer.DAILY, "forecast_daily.json") met_office.nearest_location_forecast_to_file(lat, lng, metoffer.THREE_HOURLY, "forecast_threehourly.json") else: met_office.nearest_location_observation(lat, lng) met_office.nearest_location_forecast(lat, lng, metoffer.DAILY) met_office.nearest_location_forecast(lat, lng, metoffer.THREE_HOURLY)
def test_init_no_license_keys(self): self._client.license_keys.empty() with self.assertRaises(Exception): met_office = MetOffice() met_office.check_for_license_keys(None)
def test_init_with_license_keys_none_present(self): met_office = MetOffice() self.assertIsNotNone(met_office) license_keys = LicenseKeys() with self.assertRaises(Exception): met_office.check_for_license_keys(license_keys)
def get_met_office(self): return MetOffice()
def __init__(self, license_keys, observation=None, forecast=None): MetOffice.__init__(self) self._license_keys = license_keys self._observation = observation self._forecast = forecast
def get_met_office(self, license_keys): return MetOffice(license_keys)
def get_met_office(self, bot): return MetOffice(bot.license_keys)
import metoffer from programy.utils.weather.metoffice import MetOffice if __name__ == '__main__': # Only to be used to create test data for unit aiml_tests from programy.utils.license.keys import LicenseKeys license_keys = LicenseKeys() license_keys.load_license_key_file( os.path.dirname(__file__) + '/../../../../bots/y-bot/config/license.keys') met_office = MetOffice(license_keys) lat = 56.0720397 lng = -3.1752001 log_to_file = False if log_to_file is True: met_office.nearest_location_observation_to_file( lat, lng, "observation.json") met_office.nearest_location_forecast_to_file(lat, lng, metoffer.DAILY, "forecast_daily.json") met_office.nearest_location_forecast_to_file( lat, lng, metoffer.THREE_HOURLY, "forecast_threehourly.json") else: met_office.nearest_location_observation(lat, lng)