Skip to main content

Бібліотека для використання API сайту alerts.in.ua

Project description

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)

Результат:

map_render_result.png

Використання фільтрів:

Спосіб 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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alerts_in_ua_py-1.4.4.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

alerts_in_ua_py-1.4.4-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

File details

Details for the file alerts_in_ua_py-1.4.4.tar.gz.

File metadata

  • Download URL: alerts_in_ua_py-1.4.4.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.2

File hashes

Hashes for alerts_in_ua_py-1.4.4.tar.gz
Algorithm Hash digest
SHA256 0fc0198aa77fd8413506f35bca75c572b39992771c03138aa66e3e8453c235a0
MD5 9597529d6c4eac44cca930f2041aa09e
BLAKE2b-256 f96a7d7a872c6ca0428ea57913c980359a7ab062940da95a7a4011f001dc3c65

See more details on using hashes here.

File details

Details for the file alerts_in_ua_py-1.4.4-py3-none-any.whl.

File metadata

File hashes

Hashes for alerts_in_ua_py-1.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5892f09ba2fc5bb32121d745d67f01bc8857e6ab3b374fc11b08602e4e1ee024
MD5 137598a749a507f6df05426d351b9fcf
BLAKE2b-256 864bbfb210a40256a4800d6e6e335ae350159f0a6755d460b6add1a1a9245e42

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page