Source code for ogu_api.resources.notifications
from __future__ import annotations
import tls_client.response
from ._base import ResourceBase
__all__ = ['NotificationsResource']
[docs]
class NotificationsResource(ResourceBase):
"""Site notifications and the alerts dropdown.
Paths:
- ``/notifications`` — full notifications page.
- ``/alerts.php`` — alerts page.
- ``/alerts.php?action=read`` — mark all alerts read.
"""
[docs]
async def get_notifications(self) -> tls_client.response.Response:
"""Fetch the notifications page."""
return await self._http.get('/notifications')
[docs]
async def get_alerts(self) -> tls_client.response.Response:
"""Fetch the alerts page."""
return await self._http.get('/alerts.php')
[docs]
async def mark_alerts_read(self) -> tls_client.response.Response:
"""Mark every alert as read (``/alerts.php?action=read``)."""
return await self._http.get('/alerts.php?action=read')
[docs]
@staticmethod
def extract_alert_count(page_html: str) -> int:
"""Best-effort extraction of the unread-alert count from any page.
Looks for ``<span class="alerts-count">`` first, then falls back to a
``data-alerts-count`` attribute.
Returns:
Unread count, or ``0`` if the badge couldn't be located.
"""
soup = ResourceBase._soup(page_html)
badge = soup.find('span', class_ = 'alerts-count')
if not badge:
badge = soup.find(attrs = {'data-alerts-count': True})
if badge:
raw = badge.get('data-alerts-count') or '0'
try:
return int(raw)
except ValueError:
return 0
return 0
text = badge.get_text(strip = True).replace(',', '')
return int(text) if text.isdigit() else 0