librus_apix.announcements

This module provides functions for retrieving announcements from the Librus site and parsing them into Announcement objects.

Classes: - Announcement: Represents an announcement with attributes for title, author, description, and date.

Functions: - get_announcements: Retrieves a list of announcements from the Librus API using a Client object.

Usage:

from librus_apix.client import new_client

# Create a new client instance
client = new_client()
client.get_token(username, password)

# Retrieve announcements
announcements = get_announcements(client)
 1"""
 2This module provides functions for retrieving announcements from the Librus site and parsing them into Announcement objects.
 3
 4Classes:
 5    - Announcement: Represents an announcement with attributes for title, author, description, and date.
 6
 7Functions:
 8    - get_announcements: Retrieves a list of announcements from the Librus API using a Client object.
 9
10Usage:
11```python
12from librus_apix.client import new_client
13
14# Create a new client instance
15client = new_client()
16client.get_token(username, password)
17
18# Retrieve announcements
19announcements = get_announcements(client)
20```
21"""
22
23from dataclasses import dataclass
24from typing import List
25
26from bs4 import BeautifulSoup
27
28from librus_apix.client import Client
29from librus_apix.exceptions import ParseError
30from librus_apix.helpers import no_access_check
31
32
33@dataclass
34class Announcement:
35    """
36    Represents an announcement.
37
38    Attributes:
39        title (str): The title of the announcement.
40        author (str): The author of the announcement.
41        description (str): The description of the announcement.
42        date (str): The date of the announcement.
43    """
44
45    title: str = ""
46    author: str = ""
47    description: str = ""
48    date: str = ""
49
50
51def get_announcements(client: Client) -> List[Announcement]:
52    """
53    Retrieves a list of announcements from the client.
54
55    Args:
56        client (Client): The client object used to make the request.
57
58    Returns:
59        List[Announcement]: A list of Announcement objects representing the retrieved announcements.
60
61    Raises:
62        ParseError: If there is an error parsing the announcements.
63    """
64    soup = no_access_check(
65        BeautifulSoup(client.get(client.ANNOUNCEMENTS_URL).text, "lxml")
66    )
67    if soup.select_one("div.container.border-red.resizeable.center > div > p"):
68        return []
69    announcements = []
70    announcement_tables = soup.select("table.decorated.big.center.printable.margin-top")
71    if len(announcement_tables) < 1:
72        raise ParseError("Error in parsing announcements")
73    for table in announcement_tables:
74        title = table.select_one("thead > tr > td")
75        title = title.text if title is not None else ""
76
77        data = [
78            (
79                line.select_one("td").text.strip()
80                if line.select_one("td") is not None
81                else ""
82            )
83            for line in table.find_all("tr", attrs={"class": ["line0", "line1"]})
84        ]
85        if len(data) != 3:
86            raise ParseError(f"Expected 3 items in Announcement data, got {len(data)}")
87        author, date, desc = data
88        a = Announcement(title, author, desc, date)
89        announcements.append(a)
90    return announcements
@dataclass
class Announcement:
34@dataclass
35class Announcement:
36    """
37    Represents an announcement.
38
39    Attributes:
40        title (str): The title of the announcement.
41        author (str): The author of the announcement.
42        description (str): The description of the announcement.
43        date (str): The date of the announcement.
44    """
45
46    title: str = ""
47    author: str = ""
48    description: str = ""
49    date: str = ""

Represents an announcement.

Attributes: title (str): The title of the announcement. author (str): The author of the announcement. description (str): The description of the announcement. date (str): The date of the announcement.

Announcement( title: str = '', author: str = '', description: str = '', date: str = '')
title: str = ''
author: str = ''
description: str = ''
date: str = ''
def get_announcements( client: librus_apix.client.Client) -> List[Announcement]:
52def get_announcements(client: Client) -> List[Announcement]:
53    """
54    Retrieves a list of announcements from the client.
55
56    Args:
57        client (Client): The client object used to make the request.
58
59    Returns:
60        List[Announcement]: A list of Announcement objects representing the retrieved announcements.
61
62    Raises:
63        ParseError: If there is an error parsing the announcements.
64    """
65    soup = no_access_check(
66        BeautifulSoup(client.get(client.ANNOUNCEMENTS_URL).text, "lxml")
67    )
68    if soup.select_one("div.container.border-red.resizeable.center > div > p"):
69        return []
70    announcements = []
71    announcement_tables = soup.select("table.decorated.big.center.printable.margin-top")
72    if len(announcement_tables) < 1:
73        raise ParseError("Error in parsing announcements")
74    for table in announcement_tables:
75        title = table.select_one("thead > tr > td")
76        title = title.text if title is not None else ""
77
78        data = [
79            (
80                line.select_one("td").text.strip()
81                if line.select_one("td") is not None
82                else ""
83            )
84            for line in table.find_all("tr", attrs={"class": ["line0", "line1"]})
85        ]
86        if len(data) != 3:
87            raise ParseError(f"Expected 3 items in Announcement data, got {len(data)}")
88        author, date, desc = data
89        a = Announcement(title, author, desc, date)
90        announcements.append(a)
91    return announcements

Retrieves a list of announcements from the client.

Args: client (Client): The client object used to make the request.

Returns: List[Announcement]: A list of Announcement objects representing the retrieved announcements.

Raises: ParseError: If there is an error parsing the announcements.