class TestWeatherApi(TestCase): weather_api = WeatherApi() def test_get_weather_forecast_from_api(self): api_key = '7f1f66212a27b1173d1c96f3f644b3a5' city = 'london,uk' forecast = self.weather_api.get_weather_forecast_from_api( city, api_key) self.assertIsInstance(forecast, WeatherForecast) self.assertEqual(len(forecast.weather_timestamps), 40) self.assertGreaterEqual( forecast.weather_timestamps[0].temperature.feels_like, 0) self.assertGreaterEqual( forecast.weather_timestamps[0].temperature.temp_max, 0) self.assertGreaterEqual( forecast.weather_timestamps[0].temperature.temp_min, 0) self.assertGreaterEqual(forecast.weather_timestamps[0].weather.clouds, 0) self.assertGreaterEqual( forecast.weather_timestamps[0].weather.humidity, 0) self.assertGreaterEqual( forecast.weather_timestamps[0].weather.wind_speed, 0) self.assertGreaterEqual(forecast.weather_timestamps[0].weather.rain, 0)
def test_invalid_key(self): client = WeatherApi("test") result = client.search_date_range(self.start_date, self.end_date, self.zip_code) self.assertEqual(result['weather_observations'][0]['error'], "this key does not exist")
def test_invalid_param(self): client = WeatherApi(self.api_key) result = client.search_date_range(self.start_date, self.end_date, "") self.assertEqual(result['weather_observations'][0]['error'], "you must supply a location query")
def test_search_date_range(self): client = WeatherApi(self.api_key) print( client.search_date_range(self.start_date, self.end_date, self.zip_code))
def test_search_date_range(self): client = WeatherApi(self.api_key) print(client.search_date_range(self.start_date, self.end_date, self.zip_code))
def weather_to_music(self, api_key, city) -> MidiFile: api_handling = WeatherApi() converter = Converter() weather_forecast = api_handling.get_weather_forecast_from_api( city, api_key) average_temperature = converter.average_temperature( weather_forecast.weather_timestamps) ticks_per_beat = converter.average_temperature_to_ticks_per_beat( average_temperature) outfile = MidiFile() outfile.ticks_per_beat = ticks_per_beat melody_builder = MelodyBuilder(outfile, self.PHRASE_LENGTH) temperature = TemperatureTrack(1, Instruments.BrightAcousticPiano) rain = RainTrack(2, Instruments.Celesta) clouds = CloudsTrack(3, Instruments.TremoloStrings) humidity = HumidityTrack(4, Instruments.ElectricGuitar_clean) wind = WindTrack(5, Instruments.Seashore) for track in [temperature, rain, clouds, humidity, wind]: melody_builder.set_instrument(track.get_track(), track.get_channel(), track.get_instrument()) for entry in weather_forecast.weather_timestamps: base_note = converter.temperature_to_base_note( entry.temperature.feels_like) music_scale = self.music_scales.melodic_minor(base_note) temperature_sequence = TemperatureSequence(entry.temperature, self.PHRASE_LENGTH, base_note, temperature.get_track()) temperature_appender = TemperatureAppender() temperature_appender.append(melody_builder, temperature_sequence, temperature) rain_sequence = RainSequence(entry.weather.rain, self.PHRASE_LENGTH, base_note, rain.get_track(), music_scale) rain_appender = RainAppender() rain_appender.append(melody_builder, rain_sequence, rain) clouds_sequence = CloudsSequence(entry.weather.clouds, self.PHRASE_LENGTH, base_note, clouds.get_track()) clouds_appender = CloudsAppender() clouds_appender.append(melody_builder, clouds_sequence, clouds) humidity_sequence = HumiditySequence(entry.weather.humidity, self.PHRASE_LENGTH, base_note, humidity.get_track()) humidity_appender = HumidityAppender() humidity_appender.append(melody_builder, humidity_sequence, humidity) wind_sequence = WindSequence(entry.weather.wind_speed, self.PHRASE_LENGTH, base_note, wind.get_track()) wind_appender = WindAppender() wind_appender.append(melody_builder, wind_sequence, wind) for track in [ temperature.get_track(), rain.get_track(), clouds.get_track(), humidity.get_track(), wind.get_track() ]: outfile.tracks.append(track) file_name = 'weather_song_' + weather_forecast.city + '_' + weather_forecast.country + '_' + str( weather_forecast.weather_timestamps[0].timestamp) self.save_file(outfile, self.OUTPUT_FILE_DIR, file_name) return outfile