Exemple #1
0
    def format_weather(self, res: WeatherResponse, /) -> BetterEmbed:
        """Returns a formatted embed from the data received by the api"""

        embed = BetterEmbed(title=f"Weather in {res.name} ({res.sys.country})",
                             description=f"Overall description : {res.weather[0].description}")

        embed.set_thumbnail(url=f"http://openweathermap.org/img/wn/{res.weather[0].icon}@2x.png")

        main = res.main
        sys = res.sys
        wind = res.wind

        nf = lambda obj, attr: getattr(obj, attr, '[NF]')  # -> Any
        from_timestamp = lambda ts: naturaltime(dt.now(tz.utc) - dt.fromtimestamp(ts, tz=tz.utc))  # -> str
        from_kelvin = lambda kt: f"{kt - 273.15:.1f}°C | {(kt * 9/5 - 459.67):.1f}°F"  # -> str

        fields = (
            ('Min temperature', from_kelvin(main.temp_min)),
            ('Max temperature', from_kelvin(main.temp_max)),
            ('Feels like', from_kelvin(main.feels_like)),

            ('Pressure', f"{main.pressure} hPA"),
            ('Humidity', f"{main.humidity}%"),
            ('Visibility', f"{((res.visibility or 0) / 1000):.1f} kilometers"),

            ('Wind speed', f"{wind.speed} km/h"),
            ('Wind direction', f"{wind.deg} degrees"),
            ('Cloudiness', f"{res.clouds.all}%"),

            ('Data refreshed', from_timestamp(res.dt)),
            ('Sunrise', from_timestamp(sys.sunrise)),
            ('Sunset', from_timestamp(sys.sunset))
        )

        return embed.add_fields(fields)
Exemple #2
0
    def format_page(self, menu, response: Result) -> BetterEmbed:
        """Formats the repsponse into an embed"""
        header = response.header
        data = response.data
                    
        embed = BetterEmbed(title=f"{header.index_name} | {header.similarity}%")
        embed.set_thumbnail(url=header.thumbnail)
        embed.add_field(name='External urls', 
                        value='\n'.join([maybe_url(url) for url in data.get('ext_urls', [])]))

        for key, value in data.items():
            if not isinstance(value, list):
                if url := maybe_url(value):
                    embed.add_field(name=key, value=url)  # no idea about what it will return