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.3.3.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

alerts_in_ua.py-1.3.3-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

File details

Details for the file alerts_in_ua.py-1.3.3.tar.gz.

File metadata

  • Download URL: alerts_in_ua.py-1.3.3.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for alerts_in_ua.py-1.3.3.tar.gz
Algorithm Hash digest
SHA256 53f469debfe083ccf7497cad6733bdd0ab414bd0509ea5de34d252dd7cd1efb9
MD5 3330627445abef79bb89c308f9d736c3
BLAKE2b-256 aea74b49dc73b018900c2fc10bedf9a3171b104a4a5bc07b91e86892c3da00f6

See more details on using hashes here.

File details

Details for the file alerts_in_ua.py-1.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for alerts_in_ua.py-1.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4e7698d2fa306773a66cb47314d480b1352c3ce4247f3bf8a0f12145ce18796a
MD5 a2a7741d3464ba55cc0540dae119a309
BLAKE2b-256 2321f229381ea7b67d9a6f333f996aae1d57864799452affc439320e508d7c02

See more details on using hashes here.

Supported by

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