Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added src/clients/news.py
Empty file.
6 changes: 6 additions & 0 deletions src/collectors/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ async def read(cls) -> Optional[list[CountryDTO]]:
population=item["population"],
subregion=item["subregion"],
timezones=item["timezones"],
area=item["area"],
latitude=item["latitude"],
longitude=item["longitude"],
)
)

Expand Down Expand Up @@ -219,6 +222,9 @@ async def read(cls, location: LocationDTO) -> Optional[WeatherInfoDTO]:
humidity=result["main"]["humidity"],
wind_speed=result["wind"]["speed"],
description=result["weather"][0]["description"],
visibility=result["visibility"],
dt=result["dt"],
timezone=result["timezone"] // 3600,
)

return None
Expand Down
9 changes: 9 additions & 0 deletions src/collectors/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from pydantic import Field, BaseModel
from datetime import datetime


class HashableBaseModel(BaseModel):
Expand Down Expand Up @@ -93,6 +94,7 @@ class CountryDTO(BaseModel):
timezones=[
"UTC+02:00",
],
area=228,
)
"""

Expand All @@ -106,6 +108,10 @@ class CountryDTO(BaseModel):
population: int
subregion: str
timezones: list[str]
area: float | None
latitude: float | None
longitude: float | None
timezones: list[str]


class CurrencyRatesDTO(BaseModel):
Expand Down Expand Up @@ -148,6 +154,9 @@ class WeatherInfoDTO(BaseModel):
humidity: int
wind_speed: float
description: str
visibility: int
dt: datetime
timezone: int


class LocationInfoDTO(BaseModel):
Expand Down
39 changes: 31 additions & 8 deletions src/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,38 @@ async def render(self) -> tuple[str, ...]:
:return: Результат форматирования
"""

return (
f"Страна: {self.location_info.location.name}",
f"Столица: {self.location_info.location.capital}",
f"Регион: {self.location_info.location.subregion}",
f"Языки: {await self._format_languages()}",
f"Население страны: {await self._format_population()} чел.",
f"Курсы валют: {await self._format_currency_rates()}",
f"Погода: {self.location_info.weather.temp} °C",
values = {
"Страна": self.location_info.location.name,
"Столица": self.location_info.location.capital,
"Регион": self.location_info.location.subregion,
"Языки": await self._format_languages(),
"Население страны": await self._format_population(),
"Курсы валют": await self._format_currency_rates(),
"Площадь страны": self.location_info.location.area,
"Широта": self.location_info.location.latitude,
"Долгота": self.location_info.location.longitude,
"Погода": self.location_info.weather.temp,
"Время": self.location_info.weather.dt.strftime("%d.%m.%Y %H:%M"),
"Часовой пояс": self.location_info.weather.timezone,
"Описание погоды": self.location_info.weather.description,
"Видимость": self.location_info.weather.visibility,
"Влажность": self.location_info.weather.humidity,
"Скорость ветра": self.location_info.weather.wind_speed,
"Давление": self.location_info.weather.pressure,
}

first_column_width = max(len(key) for key in values) + 1
second_column_width = max(len(str(value)) for value in values.values()) + 1
formatted_values = [("-" * (first_column_width + second_column_width + 3))]
formatted_values.extend(
[
f"|{key:<{first_column_width}}|{value:>{second_column_width}}|"
for key, value in values.items()
]
)
formatted_values.append("-" * (first_column_width + second_column_width + 3))

return tuple(formatted_values)

async def _format_languages(self) -> str:
"""
Expand Down
1 change: 1 addition & 0 deletions src/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# ключи для доступа к API
API_KEY_APILAYER: Optional[str] = os.getenv("API_KEY_APILAYER")
API_KEY_OPENWEATHER: Optional[str] = os.getenv("API_KEY_OPENWEATHER")
API_KEY_NEWS: Optional[str] = os.getenv("API_KEY_NEWS")

# время актуальности данных о странах (в секундах), по умолчанию – один год
CACHE_TTL_COUNTRY: int = int(os.getenv("CACHE_TTL_COUNTRY", "31_536_000"))
Expand Down