FeedService

lobstr.services.feed.FeedService

Bases: BaseService

Service for interacting with the feed endpoints of the Lobstr API.

Methods:

Name Description
`hot

int = 1) -> Feed`: Retrieve the hottest posts from the feed.

`new

int = 1) -> Feed`: Retrieve the newest posts from the feed.

`tag

str, page: int = 1) -> Feed`: Retrieve posts associated with a specific tag.

`filter

str, page: int = 1) -> Feed`: Retrieve posts associated with multiple tags.

Source code in lobstr/services/feed.py
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
class FeedService(BaseService):
    """
    Service for interacting with the feed endpoints of the Lobstr API.

    Methods:
        `hot(page: int = 1) -> Feed`: Retrieve the hottest posts from the feed.
        `new(page: int = 1) -> Feed`: Retrieve the newest posts from the feed.
        `tag(tag: str, page: int = 1) -> Feed`: Retrieve posts associated with a specific tag.
        `filter(*tag: str, page: int = 1) -> Feed`: Retrieve posts associated with multiple tags.
    """

    async def _feed(self, endpoint: str, page: int = 1) -> Feed:
        """
        Internal method to retrieve feed data from a specific endpoint and page.
        """
        resp = await self._client.get(f"{endpoint}/page/{page}.json")
        if resp.status_code == 404:
            raise FeedPageNotFound(
                page=page,
                endpoint=resp.url,
            )
        resp.raise_for_status()

        return Feed.model_validate(
            {
                "page": page,
                "posts": resp.json(),
            }
        )

    async def hot(self, page: int = 1) -> Feed:
        """
        Retrieve the hottest posts from the feed.

        Args:
            page (int): The page number to retrieve. Defaults to 1.

        Returns:
            Feed: The feed object containing the hottest posts for the specified page.

        Raises:
            FeedPageNotFound: If the specified page does not exist in the feed.
            HTTPError: If the request to the API fails for any other reason.
        """
        resp = await self._feed("/hottest", page)
        return resp

    async def new(self, page: int = 1) -> Feed:
        """
        Retrieve the newest posts from the feed.

        Args:
            page (int): The page number to retrieve. Defaults to 1.


        Returns:
            Feed: The feed object containing the newest posts for the specified page.

        Raises:
            FeedPageNotFound: If the specified page does not exist in the feed.
            HTTPError: If the request to the API fails for any other reason.
        """
        resp = await self._feed("/newest", page)
        return resp

    async def tag(self, tag: str, page: int = 1) -> Feed:
        """
        Retrieve posts associated with a specific tag.

        Args:
            tag (str): The tag to filter posts by.
            page (int): The page number to retrieve. Defaults to 1.

        Returns:
            Feed: The feed object containing posts associated with the specified tag for the specified page.

        Raises:
            FeedPageNotFound: If the specified page does not exist for the given tag in the feed.
            HTTPError: If the request to the API fails for any other reason.
        """
        resp = await self._feed(f"/t/{tag}", page)
        return resp

    async def filter(self, *tag: str, page: int = 1) -> Feed:
        """
        Retrieve posts associated with multiple tags.

        Args:
            *tag (str): The tags to filter posts by.
            page (int): The page number to retrieve. Defaults to 1.

        Returns:
            Feed: The feed object containing posts associated with the specified tags for the specified page.

        Raises:
            FeedPageNotFound: If the specified page does not exist for the given tags in the feed.
            HTTPError: If the request to the API fails for any other reason.
        """
        resp = await self._feed(f"/t/{','.join(tag)}", page)
        return resp

filter(*tag, page=1) async

Retrieve posts associated with multiple tags.

Parameters:

Name Type Description Default
*tag str

The tags to filter posts by.

()
page int

The page number to retrieve. Defaults to 1.

1

Returns:

Name Type Description
Feed Feed

The feed object containing posts associated with the specified tags for the specified page.

Raises:

Type Description
FeedPageNotFound

If the specified page does not exist for the given tags in the feed.

HTTPError

If the request to the API fails for any other reason.

Source code in lobstr/services/feed.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
async def filter(self, *tag: str, page: int = 1) -> Feed:
    """
    Retrieve posts associated with multiple tags.

    Args:
        *tag (str): The tags to filter posts by.
        page (int): The page number to retrieve. Defaults to 1.

    Returns:
        Feed: The feed object containing posts associated with the specified tags for the specified page.

    Raises:
        FeedPageNotFound: If the specified page does not exist for the given tags in the feed.
        HTTPError: If the request to the API fails for any other reason.
    """
    resp = await self._feed(f"/t/{','.join(tag)}", page)
    return resp

hot(page=1) async

Retrieve the hottest posts from the feed.

Parameters:

Name Type Description Default
page int

The page number to retrieve. Defaults to 1.

1

Returns:

Name Type Description
Feed Feed

The feed object containing the hottest posts for the specified page.

Raises:

Type Description
FeedPageNotFound

If the specified page does not exist in the feed.

HTTPError

If the request to the API fails for any other reason.

Source code in lobstr/services/feed.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
async def hot(self, page: int = 1) -> Feed:
    """
    Retrieve the hottest posts from the feed.

    Args:
        page (int): The page number to retrieve. Defaults to 1.

    Returns:
        Feed: The feed object containing the hottest posts for the specified page.

    Raises:
        FeedPageNotFound: If the specified page does not exist in the feed.
        HTTPError: If the request to the API fails for any other reason.
    """
    resp = await self._feed("/hottest", page)
    return resp

new(page=1) async

Retrieve the newest posts from the feed.

Parameters:

Name Type Description Default
page int

The page number to retrieve. Defaults to 1.

1

Returns:

Name Type Description
Feed Feed

The feed object containing the newest posts for the specified page.

Raises:

Type Description
FeedPageNotFound

If the specified page does not exist in the feed.

HTTPError

If the request to the API fails for any other reason.

Source code in lobstr/services/feed.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
async def new(self, page: int = 1) -> Feed:
    """
    Retrieve the newest posts from the feed.

    Args:
        page (int): The page number to retrieve. Defaults to 1.


    Returns:
        Feed: The feed object containing the newest posts for the specified page.

    Raises:
        FeedPageNotFound: If the specified page does not exist in the feed.
        HTTPError: If the request to the API fails for any other reason.
    """
    resp = await self._feed("/newest", page)
    return resp

tag(tag, page=1) async

Retrieve posts associated with a specific tag.

Parameters:

Name Type Description Default
tag str

The tag to filter posts by.

required
page int

The page number to retrieve. Defaults to 1.

1

Returns:

Name Type Description
Feed Feed

The feed object containing posts associated with the specified tag for the specified page.

Raises:

Type Description
FeedPageNotFound

If the specified page does not exist for the given tag in the feed.

HTTPError

If the request to the API fails for any other reason.

Source code in lobstr/services/feed.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
async def tag(self, tag: str, page: int = 1) -> Feed:
    """
    Retrieve posts associated with a specific tag.

    Args:
        tag (str): The tag to filter posts by.
        page (int): The page number to retrieve. Defaults to 1.

    Returns:
        Feed: The feed object containing posts associated with the specified tag for the specified page.

    Raises:
        FeedPageNotFound: If the specified page does not exist for the given tag in the feed.
        HTTPError: If the request to the API fails for any other reason.
    """
    resp = await self._feed(f"/t/{tag}", page)
    return resp

StoryService

lobstr.services.story.StoryService

Bases: BaseService

Service for interacting with the story endpoints of the Lobstr API.

Methods:

Name Description
`get

str) -> Story`: Retrieve a story by its short ID.

Source code in lobstr/services/story.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class StoryService(BaseService):
    """
    Service for interacting with the story endpoints of the Lobstr API.

    Methods:
        `get(short_id: str) -> Story`: Retrieve a story by its short ID.
    """

    async def get(self, short_id: str) -> Story:
        """
        Retrieve a story by its short ID.

        Args:
            short_id (str): The short ID of the story to retrieve.

        Returns:
            Story: An instance of the Story model representing the retrieved story.

        Raises:
            StoryNotFound: If the story with the given short ID does not exist.
            HTTPError: If the request to the API fails for any other reason.
        """
        resp = await self._client.get(f"/s/{short_id}.json")
        if resp.status_code == 404:
            raise StoryNotFound(
                story_id=short_id,
                endpoint=resp.url,
            )
        resp.raise_for_status()
        return Story.model_validate(resp.json())

get(short_id) async

Retrieve a story by its short ID.

Parameters:

Name Type Description Default
short_id str

The short ID of the story to retrieve.

required

Returns:

Name Type Description
Story Story

An instance of the Story model representing the retrieved story.

Raises:

Type Description
StoryNotFound

If the story with the given short ID does not exist.

HTTPError

If the request to the API fails for any other reason.

Source code in lobstr/services/story.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
async def get(self, short_id: str) -> Story:
    """
    Retrieve a story by its short ID.

    Args:
        short_id (str): The short ID of the story to retrieve.

    Returns:
        Story: An instance of the Story model representing the retrieved story.

    Raises:
        StoryNotFound: If the story with the given short ID does not exist.
        HTTPError: If the request to the API fails for any other reason.
    """
    resp = await self._client.get(f"/s/{short_id}.json")
    if resp.status_code == 404:
        raise StoryNotFound(
            story_id=short_id,
            endpoint=resp.url,
        )
    resp.raise_for_status()
    return Story.model_validate(resp.json())

CommentService

lobstr.services.comment.CommentService

Bases: BaseService

Service for interacting with the comments API.

Methods:

Name Description
`get

str) -> Comment`: Retrieve a comment by its short ID.

Source code in lobstr/services/comment.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class CommentService(BaseService):
    """
    Service for interacting with the comments API.

    Methods:
        `get(short_id: str) -> Comment`: Retrieve a comment by its short ID.
    """

    async def get(self, short_id: str) -> Comment:
        """
        Retrieve a comment by its short ID.

        Args:
            short_id (str): The short ID of the comment to retrieve.

        Returns:
            Comment: The retrieved comment.

        Raises:
            CommentNotFound: If the comment with the given short ID does not exist.
            HTTPError: If the request to the API fails for any other reason.
        """
        resp = await self._client.get(f"/c/{short_id}.json")
        if resp.status_code == 400 and resp.text == "can't find comment":
            raise CommentNotFound(
                comment_id=short_id,
                endpoint=resp.url,
            )
        resp.raise_for_status()
        return Comment.model_validate(resp.json())

get(short_id) async

Retrieve a comment by its short ID.

Parameters:

Name Type Description Default
short_id str

The short ID of the comment to retrieve.

required

Returns:

Name Type Description
Comment Comment

The retrieved comment.

Raises:

Type Description
CommentNotFound

If the comment with the given short ID does not exist.

HTTPError

If the request to the API fails for any other reason.

Source code in lobstr/services/comment.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
async def get(self, short_id: str) -> Comment:
    """
    Retrieve a comment by its short ID.

    Args:
        short_id (str): The short ID of the comment to retrieve.

    Returns:
        Comment: The retrieved comment.

    Raises:
        CommentNotFound: If the comment with the given short ID does not exist.
        HTTPError: If the request to the API fails for any other reason.
    """
    resp = await self._client.get(f"/c/{short_id}.json")
    if resp.status_code == 400 and resp.text == "can't find comment":
        raise CommentNotFound(
            comment_id=short_id,
            endpoint=resp.url,
        )
    resp.raise_for_status()
    return Comment.model_validate(resp.json())

UserService

lobstr.services.user.UserService

Bases: BaseService

Service for interacting with the Lobstr API's user endpoints.

Methods:

Name Description
`get

str) -> User`: Fetches user information by username.

Source code in lobstr/services/user.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class UserService(BaseService):
    """
    Service for interacting with the Lobstr API's user endpoints.

    Methods:
        `get(username: str) -> User`: Fetches user information by username.
    """

    async def get(self, username: str) -> User:
        """
        Fetches user information by username.

        Args:
            username (str): The username of the user to fetch.

        Returns:
            User: An instance of the User model containing user information.

        Raises:
            UserNotFound: If the user with the specified username does not exist.
            HTTPError: If the request to the API fails for any other reason.
        """
        resp = await self._client.get(f"/~{username}.json")
        if resp.status_code == 404:
            raise UserNotFound(username, endpoint=self._client.base_url)
        resp.raise_for_status()
        return User.model_validate(resp.json())

get(username) async

Fetches user information by username.

Parameters:

Name Type Description Default
username str

The username of the user to fetch.

required

Returns:

Name Type Description
User User

An instance of the User model containing user information.

Raises:

Type Description
UserNotFound

If the user with the specified username does not exist.

HTTPError

If the request to the API fails for any other reason.

Source code in lobstr/services/user.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
async def get(self, username: str) -> User:
    """
    Fetches user information by username.

    Args:
        username (str): The username of the user to fetch.

    Returns:
        User: An instance of the User model containing user information.

    Raises:
        UserNotFound: If the user with the specified username does not exist.
        HTTPError: If the request to the API fails for any other reason.
    """
    resp = await self._client.get(f"/~{username}.json")
    if resp.status_code == 404:
        raise UserNotFound(username, endpoint=self._client.base_url)
    resp.raise_for_status()
    return User.model_validate(resp.json())

TagService

lobstr.services.tag.TagService

Bases: BaseService

Service for interacting with the tag endpoints of the Lobstr API.

Methods:

Name Description
`get

str) -> Tag`: Retrieve a tag by its name.

`list

Retrieve a list of all available tags.

Source code in lobstr/services/tag.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class TagService(BaseService):
    """
    Service for interacting with the tag endpoints of the Lobstr API.

    Methods:
        `get(name: str) -> Tag`: Retrieve a tag by its name.
        `list() -> List[Tag]`: Retrieve a list of all available tags.
    """

    async def get(self, name: str) -> Tag:
        """
        Retrieve a tag by its name.

        Args:
            name (str): The name of the tag to retrieve.

        Returns:
            Tag: The tag object corresponding to the provided name.

        Raises:
            TagNotFound: If the tag with the specified name does not exist.
        """
        tags = await self.list()
        for tag in tags:
            if tag.name == name:
                return tag
        raise TagNotFound(name, endpoint=self._client.base_url)

    async def list(self) -> List[Tag]:
        """
        Retrieve a list of all available tags.

        Returns:
            List[Tag]: A list of tag objects.
        """
        resp = await self._client.get("/tags.json")
        resp.raise_for_status()
        return Tags.validate_python(resp.json())

get(name) async

Retrieve a tag by its name.

Parameters:

Name Type Description Default
name str

The name of the tag to retrieve.

required

Returns:

Name Type Description
Tag Tag

The tag object corresponding to the provided name.

Raises:

Type Description
TagNotFound

If the tag with the specified name does not exist.

Source code in lobstr/services/tag.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
async def get(self, name: str) -> Tag:
    """
    Retrieve a tag by its name.

    Args:
        name (str): The name of the tag to retrieve.

    Returns:
        Tag: The tag object corresponding to the provided name.

    Raises:
        TagNotFound: If the tag with the specified name does not exist.
    """
    tags = await self.list()
    for tag in tags:
        if tag.name == name:
            return tag
    raise TagNotFound(name, endpoint=self._client.base_url)

list() async

Retrieve a list of all available tags.

Returns:

Type Description
List[Tag]

List[Tag]: A list of tag objects.

Source code in lobstr/services/tag.py
37
38
39
40
41
42
43
44
45
46
async def list(self) -> List[Tag]:
    """
    Retrieve a list of all available tags.

    Returns:
        List[Tag]: A list of tag objects.
    """
    resp = await self._client.get("/tags.json")
    resp.raise_for_status()
    return Tags.validate_python(resp.json())