reimplement !weather in the IRC bot
This commit is contained in:
		
							parent
							
								
									b42d0ac0e9
								
							
						
					
					
						commit
						56d0e26c6d
					
				@ -33,7 +33,27 @@ class Weather(Plugin):
 | 
				
			|||||||
        if len(queryitems) <= 0:
 | 
					        if len(queryitems) <= 0:
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return self.bot.reply(event, weather_summary(queryitems[0]))
 | 
					        weather = weather_summary(queryitems[0])
 | 
				
			||||||
 | 
					        weather_output = (f"Weather in {weather['location']}: {weather['current']['description']}. "
 | 
				
			||||||
 | 
					                          f"{weather['current']['temp_F']}/{weather['current']['temp_C']}, "
 | 
				
			||||||
 | 
					                          f"feels like {weather['current']['feels_like_temp_F']}/{weather['current']['feels_like_temp_C']}. "
 | 
				
			||||||
 | 
					                          f"Wind {weather['current']['wind_speed']} from the {weather['current']['wind_direction']}. "
 | 
				
			||||||
 | 
					                          f"Visibility {weather['current']['visibility']}. "
 | 
				
			||||||
 | 
					                          f"Precipitation {weather['current']['precipitation']}. "
 | 
				
			||||||
 | 
					                          f"Pressure {weather['current']['pressure']}. "
 | 
				
			||||||
 | 
					                          f"Cloud cover {weather['current']['cloud_cover']}. UV index {weather['current']['uv_index']}. "
 | 
				
			||||||
 | 
					                          f"Today: {weather['today_forecast']['noteworthy']}, "
 | 
				
			||||||
 | 
					                          f"High {weather['today_forecast']['high_F']}/{weather['today_forecast']['high_C']}, "
 | 
				
			||||||
 | 
					                          f"Low {weather['today_forecast']['low_F']}/{weather['today_forecast']['low_C']}. "
 | 
				
			||||||
 | 
					                          f"Tomorrow: {weather['tomorrow_forecast']['noteworthy']}, "
 | 
				
			||||||
 | 
					                          f"High {weather['tomorrow_forecast']['high_F']}/{weather['tomorrow_forecast']['high_C']}, "
 | 
				
			||||||
 | 
					                          f"Low {weather['tomorrow_forecast']['low_F']}/{weather['tomorrow_forecast']['low_C']}. "
 | 
				
			||||||
 | 
					                          f"Day after tomorrow: {weather['day_after_tomorrow_forecast']['noteworthy']}, "
 | 
				
			||||||
 | 
					                          f"High {weather['day_after_tomorrow_forecast']['high_F']}/{weather['day_after_tomorrow_forecast']['high_C']}, "
 | 
				
			||||||
 | 
					                          f"Low {weather['day_after_tomorrow_forecast']['low_F']}/{weather['day_after_tomorrow_forecast']['low_C']}."
 | 
				
			||||||
 | 
					                          )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return self.bot.reply(event, weather_output)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
plugin = Weather
 | 
					plugin = Weather
 | 
				
			||||||
 | 
				
			|||||||
@ -19,6 +19,7 @@ def query_wttr_in(query):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def weather_summary(query):
 | 
					def weather_summary(query):
 | 
				
			||||||
    """Create a more consumable version of the weather report."""
 | 
					    """Create a more consumable version of the weather report."""
 | 
				
			||||||
 | 
					    logger.info(f"assembling weather summary for '{query}")
 | 
				
			||||||
    weather_info = query_wttr_in(query)
 | 
					    weather_info = query_wttr_in(query)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # get some common/nested stuff once now
 | 
					    # get some common/nested stuff once now
 | 
				
			||||||
@ -28,6 +29,18 @@ def weather_summary(query):
 | 
				
			|||||||
    tomorrow_forecast = weather_info['weather'][1]
 | 
					    tomorrow_forecast = weather_info['weather'][1]
 | 
				
			||||||
    day_after_tomorrow_forecast = weather_info['weather'][2]
 | 
					    day_after_tomorrow_forecast = weather_info['weather'][2]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    today_notes = [{'code': int(item['weatherCode']), 'desc': item['weatherDesc'][0]['value'] }
 | 
				
			||||||
 | 
					                   for item in today_forecast['hourly']]
 | 
				
			||||||
 | 
					    today_noteworthy = sorted(today_notes, key=lambda i: i['code'], reverse=True)[0]['desc']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    tomorrow_notes = [{'code': int(item['weatherCode']), 'desc': item['weatherDesc'][0]['value'] }
 | 
				
			||||||
 | 
					                      for item in tomorrow_forecast['hourly']]
 | 
				
			||||||
 | 
					    tomorrow_noteworthy = sorted(tomorrow_notes, key=lambda i: i['code'], reverse=True)[0]['desc']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    day_after_tomorrow_notes = [{'code': int(item['weatherCode']), 'desc': item['weatherDesc'][0]['value'] }
 | 
				
			||||||
 | 
					                                for item in day_after_tomorrow_forecast['hourly']]
 | 
				
			||||||
 | 
					    day_after_tomorrow_noteworthy = sorted(day_after_tomorrow_notes, key=lambda i: i['code'], reverse=True)[0]['desc']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    summary = {
 | 
					    summary = {
 | 
				
			||||||
        'location': query,
 | 
					        'location': query,
 | 
				
			||||||
        'current': {
 | 
					        'current': {
 | 
				
			||||||
@ -41,10 +54,32 @@ def weather_summary(query):
 | 
				
			|||||||
            'precipitation': f"{current.get('precipMM')} mm",
 | 
					            'precipitation': f"{current.get('precipMM')} mm",
 | 
				
			||||||
            'visibility': f"{current.get('visibility')} mi",
 | 
					            'visibility': f"{current.get('visibility')} mi",
 | 
				
			||||||
            'wind_speed': f"{current.get('windspeedMiles')} MPH",
 | 
					            'wind_speed': f"{current.get('windspeedMiles')} MPH",
 | 
				
			||||||
            'wind_direction': f"{current.get('winddir16Point')}",
 | 
					            'wind_direction': current.get('winddir16Point'),
 | 
				
			||||||
            'pressure': f"{current.get('pressure')} mb",
 | 
					            'pressure': f"{current.get('pressure')} mb",
 | 
				
			||||||
            'uv_index': f"{current.get('uvIndex')}",
 | 
					            'uv_index': current.get('uvIndex'),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        'today_forecast': {
 | 
				
			||||||
 | 
					            'high_C': f"{today_forecast.get('maxtempC')}°C",
 | 
				
			||||||
 | 
					            'high_F': f"{today_forecast.get('maxtempF')}°F",
 | 
				
			||||||
 | 
					            'low_C': f"{today_forecast.get('mintempC')}°C",
 | 
				
			||||||
 | 
					            'low_F': f"{today_forecast.get('mintempF')}°F",
 | 
				
			||||||
 | 
					            'noteworthy': today_noteworthy,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        'tomorrow_forecast': {
 | 
				
			||||||
 | 
					            'high_C': f"{tomorrow_forecast.get('maxtempC')}°C",
 | 
				
			||||||
 | 
					            'high_F': f"{tomorrow_forecast.get('maxtempF')}°F",
 | 
				
			||||||
 | 
					            'low_C': f"{tomorrow_forecast.get('mintempC')}°C",
 | 
				
			||||||
 | 
					            'low_F': f"{tomorrow_forecast.get('mintempF')}°F",
 | 
				
			||||||
 | 
					            'noteworthy': tomorrow_noteworthy,
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        'day_after_tomorrow_forecast': {
 | 
				
			||||||
 | 
					            'high_C': f"{day_after_tomorrow_forecast.get('maxtempC')}°C",
 | 
				
			||||||
 | 
					            'high_F': f"{day_after_tomorrow_forecast.get('maxtempF')}°F",
 | 
				
			||||||
 | 
					            'low_C': f"{day_after_tomorrow_forecast.get('mintempC')}°C",
 | 
				
			||||||
 | 
					            'low_F': f"{day_after_tomorrow_forecast.get('mintempF')}°F",
 | 
				
			||||||
 | 
					            'noteworthy': day_after_tomorrow_noteworthy,
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    logger.debug(f"results: {summary}")
 | 
				
			||||||
    return summary
 | 
					    return summary
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user