Alerts_in_ua.py
Бібліотека для користування API сайту alerts.in.ua. Бібліотека досі в розробці, якщо ви знайшли помилку або у вас є ідея щодо бібліотеки, вертайтесь до розробника! Telegram: @FOUREX_dot_py.
Розробники сайту alerts.in.ua випустили офіційну бібліотеку.
Встановлення
pip install alerts-in-ua.py
Приклад використання:
from alerts_in_ua.alerts_client import AlertsClient # Імпортуємо клієнт
alerts_client = AlertsClient("token") # Ініціалізуємо клієнт
def main():
locations = alerts_client.get_active() # Отримуємо список місць з тривогою
# Фільтруємо список місць залишаючи місця з ПОВІТРЯНОЮ тривогою
air_raid_locations = locations.filter(alert_type="air_raid")
for location in air_raid_locations:
# Виводимо назву та час початку тривоги кожного місця зі списку
print(location.location_title, location.started_at)
if __name__ == "__main__":
main()
Приклад використання асинхронного клієнта:
Рекомендовано використовувати для ботів
import asyncio
from alerts_in_ua.async_alerts_client import AsyncAlertsClient
alerts_client = AsyncAlertsClient("token") # Ініціалізуємо клієнт
async def main():
locations = await alerts_client.get_active() # Отримуємо список місць з тривогою
# Фільтруємо список місць залишаючи місця з ПОВІТРЯНОЮ тривогою
air_raid_locations = locations.filter(alert_type="air_raid")
for location in air_raid_locations:
# Виводимо назву та час початку тривоги кожного місця зі списку
print(location.location_title, location.started_at)
if __name__ == "__main__":
loop = asyncio.new_event_loop()
loop.run_until_complete(main())
Приклад використання рендера мапи тривог:
Для телеграм боту написаного з допомогою бібліотеки aiogram
from aiogram import Bot, Dispatcher, executor
from aiogram.types import Message
from alerts_in_ua.async_alerts_client import AsyncAlertsClient
bot = Bot("telegram_bot_token")
dp = Dispatcher(bot)
alerts_client = AsyncAlertsClient("api_alerts_in_ua_token")
@dp.message_handler(commands=["alerts"])
async def yep(message: Message):
locations = await alerts_client.get_active()
alerts_map = locations.render_map()
message_text = "\n".join(locations.location_title)
await message.reply_photo(alerts_map, message_text)
if __name__ == "__main__":
executor.start_polling(dispatcher=dp)
Результат:
Використання фільтрів:
Спосіб 1
locations = alerts_client.get_active()
air_raid = locations.filter(alert_type="air_raid")
oblast = locations.filter(location_type="oblast")
air_raid_and_oblast = locations.filter(alert_type="air_raid", location_type="oblast")
print(air_raid) # Місця лише з повітряною тривогою
print(oblast) # Лише області
print(air_raid_and_oblast) # Лише області з повітряною тривогою
Спосіб 2
locations = alerts_client.get_active()
air_raid_filter = {"alert_type": "air_raid"}
oblast_filter = {"location_type": "oblast"}
air_raid_and_oblast_filter = {"alert_type": "air_raid", "location_type": "oblast"}
air_raid = locations.filter(**air_raid_filter)
oblast = locations.filter(**oblast_filter)
air_raid_and_oblast = locations.filter(**air_raid_and_oblast_filter)
print(air_raid) # Місця лише з повітряною тривогою
print(oblast) # Лише області
print(air_raid_and_oblast) # Лише області з повітряною тривогою
Отримання значень атрибутів місць через список місць:
locations = alerts_client.get_active()
print(list(zip(locations.location_title, locations.location_uid)))
# [('Луганська область', '16'), ('Автономна Республіка Крим', '29'), ('Нікопольська територіальна громада', '351'), ('м. Нікополь', '5351')]
Перевірка наявності місця в списку:
За його UID (location_uid) або назвою (location_title)
locations = alerts_client.get_active()
print("Автономна Республіка Крим" in locations)
Release files for alerts-in-ua.py 1.4.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| alerts_in_ua_py-1.4.4.tar.gz | 1.1 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| alerts_in_ua_py-1.4.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 2.3 MB
Release files / alerts_in_ua_py-1.4.4.tar.gz
| Download URL | alerts_in_ua_py-1.4.4.tar.gz |
|---|---|
| Size | 1.1 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0fc0198aa77fd8413506f35bca75c572b39992771c03138aa66e3e8453c235a0
|
|
BLAKE2b-256 checksum How to use checksums |
f96a7d7a872c6ca0428ea57913c980359a7ab062940da95a7a4011f001dc3c65
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.2
|
Release files / alerts_in_ua_py-1.4.4-py3-none-any.whl
| Download URL | alerts_in_ua_py-1.4.4-py3-none-any.whl |
|---|---|
| Size | 1.1 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5892f09ba2fc5bb32121d745d67f01bc8857e6ab3b374fc11b08602e4e1ee024
|
|
BLAKE2b-256 checksum How to use checksums |
864bbfb210a40256a4800d6e6e335ae350159f0a6755d460b6add1a1a9245e42
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.2
|