boostylib 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- boostylib/__init__.py +55 -0
- boostylib/api/__init__.py +25 -0
- boostylib/api/base.py +71 -0
- boostylib/api/blogs.py +22 -0
- boostylib/api/bundles.py +26 -0
- boostylib/api/comments.py +157 -0
- boostylib/api/donations.py +54 -0
- boostylib/api/media.py +66 -0
- boostylib/api/posts.py +191 -0
- boostylib/api/showcase.py +22 -0
- boostylib/api/subscriptions.py +184 -0
- boostylib/api/targets.py +77 -0
- boostylib/api/users.py +30 -0
- boostylib/auth/__init__.py +20 -0
- boostylib/auth/manager.py +150 -0
- boostylib/auth/models.py +23 -0
- boostylib/auth/storage.py +124 -0
- boostylib/builders/__init__.py +5 -0
- boostylib/builders/post_builder.py +154 -0
- boostylib/cache.py +130 -0
- boostylib/client.py +160 -0
- boostylib/config.py +73 -0
- boostylib/enums.py +59 -0
- boostylib/events/__init__.py +13 -0
- boostylib/events/dispatcher.py +55 -0
- boostylib/events/models.py +46 -0
- boostylib/events/poller.py +215 -0
- boostylib/export/__init__.py +0 -0
- boostylib/export/writers/__init__.py +0 -0
- boostylib/http/__init__.py +27 -0
- boostylib/http/exceptions.py +60 -0
- boostylib/http/middleware.py +68 -0
- boostylib/http/rate_limiter.py +46 -0
- boostylib/http/retry.py +62 -0
- boostylib/http/transport.py +227 -0
- boostylib/models/__init__.py +30 -0
- boostylib/models/blog.py +48 -0
- boostylib/models/comment.py +34 -0
- boostylib/models/donation.py +22 -0
- boostylib/models/media.py +21 -0
- boostylib/models/pagination.py +27 -0
- boostylib/models/post.py +87 -0
- boostylib/models/subscriber.py +38 -0
- boostylib/models/subscription.py +46 -0
- boostylib/models/target.py +20 -0
- boostylib/models/user.py +18 -0
- boostylib/plugins/__init__.py +0 -0
- boostylib/py.typed +0 -0
- boostylib/sync/__init__.py +5 -0
- boostylib/sync/_wrapper.py +31 -0
- boostylib/sync/client.py +82 -0
- boostylib-0.1.0.dist-info/METADATA +510 -0
- boostylib-0.1.0.dist-info/RECORD +55 -0
- boostylib-0.1.0.dist-info/WHEEL +4 -0
- boostylib-0.1.0.dist-info/licenses/LICENSE +21 -0
boostylib/__init__.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""boostylib — Async Python library for Boosty.to API."""
|
|
2
|
+
|
|
3
|
+
from boostylib.auth.models import AuthCredentials, TokenPair
|
|
4
|
+
from boostylib.auth.storage import EnvTokenStorage, FileTokenStorage, MemoryTokenStorage
|
|
5
|
+
from boostylib.builders.post_builder import PostBuilder
|
|
6
|
+
from boostylib.cache import CacheBackend, CacheManager, MemoryCache, NullCache
|
|
7
|
+
from boostylib.client import BoostyClient
|
|
8
|
+
from boostylib.config import BoostySettings
|
|
9
|
+
from boostylib.enums import (
|
|
10
|
+
CommentOrder,
|
|
11
|
+
ContentType,
|
|
12
|
+
EventType,
|
|
13
|
+
ExportFormat,
|
|
14
|
+
PostAccess,
|
|
15
|
+
TargetType,
|
|
16
|
+
)
|
|
17
|
+
from boostylib.http.exceptions import (
|
|
18
|
+
BoostyAuthError,
|
|
19
|
+
BoostyError,
|
|
20
|
+
BoostyForbiddenError,
|
|
21
|
+
BoostyNetworkError,
|
|
22
|
+
BoostyNotFoundError,
|
|
23
|
+
BoostyRateLimitError,
|
|
24
|
+
BoostyServerError,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
__version__ = "0.1.0"
|
|
28
|
+
|
|
29
|
+
__all__ = [
|
|
30
|
+
"AuthCredentials",
|
|
31
|
+
"BoostyAuthError",
|
|
32
|
+
"BoostyClient",
|
|
33
|
+
"BoostyError",
|
|
34
|
+
"BoostyForbiddenError",
|
|
35
|
+
"BoostyNetworkError",
|
|
36
|
+
"BoostyNotFoundError",
|
|
37
|
+
"BoostyRateLimitError",
|
|
38
|
+
"BoostyServerError",
|
|
39
|
+
"BoostySettings",
|
|
40
|
+
"CacheBackend",
|
|
41
|
+
"CacheManager",
|
|
42
|
+
"CommentOrder",
|
|
43
|
+
"ContentType",
|
|
44
|
+
"EnvTokenStorage",
|
|
45
|
+
"EventType",
|
|
46
|
+
"ExportFormat",
|
|
47
|
+
"FileTokenStorage",
|
|
48
|
+
"MemoryCache",
|
|
49
|
+
"MemoryTokenStorage",
|
|
50
|
+
"NullCache",
|
|
51
|
+
"PostAccess",
|
|
52
|
+
"PostBuilder",
|
|
53
|
+
"TargetType",
|
|
54
|
+
"TokenPair",
|
|
55
|
+
]
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""API endpoint modules."""
|
|
2
|
+
|
|
3
|
+
from boostylib.api.blogs import BlogsAPI
|
|
4
|
+
from boostylib.api.bundles import BundlesAPI
|
|
5
|
+
from boostylib.api.comments import CommentsAPI
|
|
6
|
+
from boostylib.api.donations import DonationsAPI
|
|
7
|
+
from boostylib.api.media import MediaAPI
|
|
8
|
+
from boostylib.api.posts import PostsAPI
|
|
9
|
+
from boostylib.api.showcase import ShowcaseAPI
|
|
10
|
+
from boostylib.api.subscriptions import SubscriptionsAPI
|
|
11
|
+
from boostylib.api.targets import TargetsAPI
|
|
12
|
+
from boostylib.api.users import UsersAPI
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"BlogsAPI",
|
|
16
|
+
"BundlesAPI",
|
|
17
|
+
"CommentsAPI",
|
|
18
|
+
"DonationsAPI",
|
|
19
|
+
"MediaAPI",
|
|
20
|
+
"PostsAPI",
|
|
21
|
+
"ShowcaseAPI",
|
|
22
|
+
"SubscriptionsAPI",
|
|
23
|
+
"TargetsAPI",
|
|
24
|
+
"UsersAPI",
|
|
25
|
+
]
|
boostylib/api/base.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"""Base class for API endpoint groups."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import AsyncIterator
|
|
6
|
+
from typing import TYPE_CHECKING, Any, TypeVar
|
|
7
|
+
|
|
8
|
+
from boostylib.models.pagination import PaginatedResponse
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from boostylib.http.transport import HTTPTransport
|
|
12
|
+
|
|
13
|
+
T = TypeVar("T")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class BaseAPI:
|
|
17
|
+
"""Base API class providing HTTP transport access."""
|
|
18
|
+
|
|
19
|
+
def __init__(self, transport: HTTPTransport) -> None:
|
|
20
|
+
self._transport = transport
|
|
21
|
+
|
|
22
|
+
async def _get(self, path: str, **kwargs: Any) -> Any:
|
|
23
|
+
response = await self._transport.request("GET", path, **kwargs)
|
|
24
|
+
return response.json()
|
|
25
|
+
|
|
26
|
+
async def _post(self, path: str, **kwargs: Any) -> Any:
|
|
27
|
+
response = await self._transport.request("POST", path, **kwargs)
|
|
28
|
+
return response.json()
|
|
29
|
+
|
|
30
|
+
async def _put(self, path: str, **kwargs: Any) -> Any:
|
|
31
|
+
response = await self._transport.request("PUT", path, **kwargs)
|
|
32
|
+
return response.json()
|
|
33
|
+
|
|
34
|
+
async def _delete(self, path: str, **kwargs: Any) -> None:
|
|
35
|
+
await self._transport.request("DELETE", path, **kwargs)
|
|
36
|
+
|
|
37
|
+
async def _iter_pages(
|
|
38
|
+
self,
|
|
39
|
+
path: str,
|
|
40
|
+
model_cls: type[T],
|
|
41
|
+
*,
|
|
42
|
+
params: dict[str, Any] | None = None,
|
|
43
|
+
limit: int = 20,
|
|
44
|
+
) -> AsyncIterator[T]:
|
|
45
|
+
"""Async iterate over all pages of a paginated endpoint."""
|
|
46
|
+
offset: str | None = None
|
|
47
|
+
while True:
|
|
48
|
+
request_params = dict(params or {})
|
|
49
|
+
request_params["limit"] = limit
|
|
50
|
+
if offset is not None:
|
|
51
|
+
request_params["offset"] = offset
|
|
52
|
+
|
|
53
|
+
data = await self._get(path, params=request_params)
|
|
54
|
+
page = self._parse_paginated(data, model_cls)
|
|
55
|
+
for item in page.data:
|
|
56
|
+
yield item
|
|
57
|
+
|
|
58
|
+
if page.is_last or page.cursor is None:
|
|
59
|
+
break
|
|
60
|
+
offset = page.cursor
|
|
61
|
+
|
|
62
|
+
def _parse_paginated(self, data: dict[str, Any], model_cls: type[T]) -> PaginatedResponse[T]:
|
|
63
|
+
"""Parse a raw paginated API response."""
|
|
64
|
+
items_raw = data.get("data", [])
|
|
65
|
+
extra = data.get("extra", {})
|
|
66
|
+
items = [model_cls.model_validate(item) for item in items_raw] # type: ignore[union-attr]
|
|
67
|
+
return PaginatedResponse(
|
|
68
|
+
data=items,
|
|
69
|
+
cursor=extra.get("offset"),
|
|
70
|
+
is_last=extra.get("isLast", True),
|
|
71
|
+
)
|
boostylib/api/blogs.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Blogs API."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from boostylib.api.base import BaseAPI
|
|
8
|
+
from boostylib.models.blog import Blog
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class BlogsAPI(BaseAPI):
|
|
12
|
+
"""Endpoints for blog operations."""
|
|
13
|
+
|
|
14
|
+
async def get_blog(self, username: str) -> Blog:
|
|
15
|
+
"""Get blog information by username."""
|
|
16
|
+
data = await self._get(f"/blog/{username}")
|
|
17
|
+
return Blog.model_validate(data)
|
|
18
|
+
|
|
19
|
+
async def get_blacklist(self, blog_url: str) -> list[dict[str, Any]]:
|
|
20
|
+
"""Get blacklisted users for a blog."""
|
|
21
|
+
data = await self._get("/blacklist/", params={"blog_url": blog_url})
|
|
22
|
+
return data.get("data", [])
|
boostylib/api/bundles.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""Bundles API."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from boostylib.api.base import BaseAPI
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class BundlesAPI(BaseAPI):
|
|
11
|
+
"""Endpoints for blog bundles (paid content packages)."""
|
|
12
|
+
|
|
13
|
+
async def get_bundles(self, username: str) -> list[dict[str, Any]]:
|
|
14
|
+
"""Get bundles for a blog."""
|
|
15
|
+
data = await self._get(f"/blog/{username}/bundle/")
|
|
16
|
+
if isinstance(data, dict):
|
|
17
|
+
return data.get("data", [])
|
|
18
|
+
return data if isinstance(data, list) else []
|
|
19
|
+
|
|
20
|
+
async def get_bundle(self, username: str, bundle_id: str) -> dict[str, Any]:
|
|
21
|
+
"""Get a single bundle by ID."""
|
|
22
|
+
return await self._get(f"/blog/{username}/bundle/{bundle_id}")
|
|
23
|
+
|
|
24
|
+
async def delete_bundle(self, username: str, bundle_id: str) -> None:
|
|
25
|
+
"""Delete a bundle."""
|
|
26
|
+
await self._transport.request("DELETE", f"/blog/{username}/bundle/{bundle_id}")
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""Comments API."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from collections.abc import AsyncIterator
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from boostylib.api.base import BaseAPI
|
|
10
|
+
from boostylib.enums import CommentOrder
|
|
11
|
+
from boostylib.models.comment import Comment
|
|
12
|
+
from boostylib.models.pagination import PaginatedResponse
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class CommentsAPI(BaseAPI):
|
|
16
|
+
"""Endpoints for comment operations."""
|
|
17
|
+
|
|
18
|
+
async def get_comments(
|
|
19
|
+
self,
|
|
20
|
+
username: str,
|
|
21
|
+
post_id: str,
|
|
22
|
+
*,
|
|
23
|
+
limit: int = 20,
|
|
24
|
+
offset: str | None = None,
|
|
25
|
+
reply_limit: int = 2,
|
|
26
|
+
order: CommentOrder = CommentOrder.DESC,
|
|
27
|
+
) -> PaginatedResponse[Comment]:
|
|
28
|
+
"""Get comments for a post."""
|
|
29
|
+
params: dict[str, Any] = {
|
|
30
|
+
"limit": limit,
|
|
31
|
+
"reply_limit": reply_limit,
|
|
32
|
+
"order": order.value,
|
|
33
|
+
}
|
|
34
|
+
if offset is not None:
|
|
35
|
+
params["offset"] = offset
|
|
36
|
+
|
|
37
|
+
data = await self._get(f"/blog/{username}/post/{post_id}/comment/", params=params)
|
|
38
|
+
return self._parse_comments(data)
|
|
39
|
+
|
|
40
|
+
async def create_comment(
|
|
41
|
+
self,
|
|
42
|
+
username: str,
|
|
43
|
+
post_id: str,
|
|
44
|
+
text: str,
|
|
45
|
+
*,
|
|
46
|
+
reply_to: str | None = None,
|
|
47
|
+
) -> Comment:
|
|
48
|
+
"""Create a comment on a post.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
username: Blog username.
|
|
52
|
+
post_id: Post ID.
|
|
53
|
+
text: Comment text.
|
|
54
|
+
reply_to: Comment ID to reply to (optional).
|
|
55
|
+
"""
|
|
56
|
+
text_block = {
|
|
57
|
+
"type": "text",
|
|
58
|
+
"content": json.dumps([text, "unstyled", []]),
|
|
59
|
+
"modificator": "",
|
|
60
|
+
}
|
|
61
|
+
end_block = {"type": "text", "content": "", "modificator": "BLOCK_END"}
|
|
62
|
+
|
|
63
|
+
form_data: dict[str, str] = {
|
|
64
|
+
"data": json.dumps([text_block, end_block]),
|
|
65
|
+
}
|
|
66
|
+
if reply_to is not None:
|
|
67
|
+
form_data["reply_id"] = reply_to
|
|
68
|
+
|
|
69
|
+
response = await self._transport.request(
|
|
70
|
+
"POST",
|
|
71
|
+
f"/blog/{username}/post/{post_id}/comment/",
|
|
72
|
+
params={"from_page": "blog"},
|
|
73
|
+
data=form_data,
|
|
74
|
+
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
|
75
|
+
)
|
|
76
|
+
raw = response.json()
|
|
77
|
+
return self._parse_single_comment(raw)
|
|
78
|
+
|
|
79
|
+
async def delete_comment(
|
|
80
|
+
self,
|
|
81
|
+
username: str,
|
|
82
|
+
post_id: str,
|
|
83
|
+
comment_id: str,
|
|
84
|
+
) -> None:
|
|
85
|
+
"""Delete a comment.
|
|
86
|
+
|
|
87
|
+
Args:
|
|
88
|
+
username: Blog username.
|
|
89
|
+
post_id: Post ID.
|
|
90
|
+
comment_id: Comment UUID to delete.
|
|
91
|
+
"""
|
|
92
|
+
await self._transport.request(
|
|
93
|
+
"DELETE",
|
|
94
|
+
f"/blog/{username}/post/{post_id}/comment/{comment_id}",
|
|
95
|
+
params={"from_page": "blog"},
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
async def iter_comments(
|
|
99
|
+
self,
|
|
100
|
+
username: str,
|
|
101
|
+
post_id: str,
|
|
102
|
+
*,
|
|
103
|
+
limit: int = 20,
|
|
104
|
+
order: CommentOrder = CommentOrder.DESC,
|
|
105
|
+
) -> AsyncIterator[Comment]:
|
|
106
|
+
"""Async iterate over all comments for a post."""
|
|
107
|
+
offset: str | None = None
|
|
108
|
+
while True:
|
|
109
|
+
page = await self.get_comments(
|
|
110
|
+
username,
|
|
111
|
+
post_id,
|
|
112
|
+
limit=limit,
|
|
113
|
+
offset=offset,
|
|
114
|
+
order=order,
|
|
115
|
+
)
|
|
116
|
+
for comment in page.data:
|
|
117
|
+
yield comment
|
|
118
|
+
if page.is_last or page.cursor is None:
|
|
119
|
+
break
|
|
120
|
+
offset = page.cursor
|
|
121
|
+
|
|
122
|
+
def _parse_comments(self, data: dict[str, Any]) -> PaginatedResponse[Comment]:
|
|
123
|
+
"""Parse the comments response into Comment models."""
|
|
124
|
+
raw_items = data.get("data", [])
|
|
125
|
+
extra = data.get("extra", {})
|
|
126
|
+
comments = [self._parse_single_comment(item) for item in raw_items]
|
|
127
|
+
return PaginatedResponse(
|
|
128
|
+
data=comments,
|
|
129
|
+
cursor=extra.get("offset"),
|
|
130
|
+
is_last=extra.get("isLast", True),
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
def _parse_single_comment(self, raw: dict[str, Any]) -> Comment:
|
|
134
|
+
"""Parse a single comment from raw API data."""
|
|
135
|
+
# Extract text from Draft.js content blocks
|
|
136
|
+
content_blocks = raw.get("data", [])
|
|
137
|
+
text_parts: list[str] = []
|
|
138
|
+
for block in content_blocks:
|
|
139
|
+
if block.get("type") == "text" and block.get("modificator") != "BLOCK_END":
|
|
140
|
+
raw_content = block.get("content", "")
|
|
141
|
+
try:
|
|
142
|
+
parsed = json.loads(raw_content)
|
|
143
|
+
if isinstance(parsed, list) and parsed:
|
|
144
|
+
text_parts.append(str(parsed[0]))
|
|
145
|
+
except json.JSONDecodeError, IndexError:
|
|
146
|
+
if raw_content:
|
|
147
|
+
text_parts.append(raw_content)
|
|
148
|
+
|
|
149
|
+
author_data = raw.get("author", {})
|
|
150
|
+
return Comment(
|
|
151
|
+
id=str(raw.get("id", "")),
|
|
152
|
+
intId=raw.get("intId"),
|
|
153
|
+
author=author_data,
|
|
154
|
+
content="\n".join(text_parts),
|
|
155
|
+
createdAt=raw.get("createdAt"),
|
|
156
|
+
replyId=raw.get("replyId"),
|
|
157
|
+
)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""Donations API."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import AsyncIterator
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from boostylib.api.base import BaseAPI
|
|
9
|
+
from boostylib.models.donation import Donation
|
|
10
|
+
from boostylib.models.pagination import PaginatedResponse
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class DonationsAPI(BaseAPI):
|
|
14
|
+
"""Endpoints for donation tracking."""
|
|
15
|
+
|
|
16
|
+
async def get_donations(
|
|
17
|
+
self,
|
|
18
|
+
username: str,
|
|
19
|
+
*,
|
|
20
|
+
limit: int = 20,
|
|
21
|
+
offset: str | None = None,
|
|
22
|
+
from_ts: int | None = None,
|
|
23
|
+
to_ts: int | None = None,
|
|
24
|
+
) -> PaginatedResponse[Donation]:
|
|
25
|
+
"""Get donations with filtering and pagination."""
|
|
26
|
+
params: dict[str, Any] = {"limit": limit}
|
|
27
|
+
if offset is not None:
|
|
28
|
+
params["offset"] = offset
|
|
29
|
+
if from_ts is not None:
|
|
30
|
+
params["from_ts"] = from_ts
|
|
31
|
+
if to_ts is not None:
|
|
32
|
+
params["to_ts"] = to_ts
|
|
33
|
+
|
|
34
|
+
data = await self._get(f"/blog/{username}/donations/", params=params)
|
|
35
|
+
return self._parse_paginated(data, Donation)
|
|
36
|
+
|
|
37
|
+
async def iter_donations(
|
|
38
|
+
self,
|
|
39
|
+
username: str,
|
|
40
|
+
*,
|
|
41
|
+
limit: int = 20,
|
|
42
|
+
from_ts: int | None = None,
|
|
43
|
+
to_ts: int | None = None,
|
|
44
|
+
) -> AsyncIterator[Donation]:
|
|
45
|
+
"""Async iterate over all donations."""
|
|
46
|
+
params: dict[str, Any] = {}
|
|
47
|
+
if from_ts is not None:
|
|
48
|
+
params["from_ts"] = from_ts
|
|
49
|
+
if to_ts is not None:
|
|
50
|
+
params["to_ts"] = to_ts
|
|
51
|
+
async for donation in self._iter_pages(
|
|
52
|
+
f"/blog/{username}/donations/", Donation, params=params, limit=limit
|
|
53
|
+
):
|
|
54
|
+
yield donation
|
boostylib/api/media.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Media upload API."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import IO, Any
|
|
7
|
+
|
|
8
|
+
from boostylib.api.base import BaseAPI
|
|
9
|
+
from boostylib.models.media import MediaFile
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class MediaAPI(BaseAPI):
|
|
13
|
+
"""Endpoints for media file uploads."""
|
|
14
|
+
|
|
15
|
+
async def upload_image(
|
|
16
|
+
self,
|
|
17
|
+
file: Path | IO[bytes],
|
|
18
|
+
*,
|
|
19
|
+
content_type: str = "image/png",
|
|
20
|
+
) -> MediaFile:
|
|
21
|
+
"""Upload an image file."""
|
|
22
|
+
return await self._upload(file, content_type=content_type)
|
|
23
|
+
|
|
24
|
+
async def upload_file(
|
|
25
|
+
self,
|
|
26
|
+
file: Path | IO[bytes],
|
|
27
|
+
*,
|
|
28
|
+
filename: str | None = None,
|
|
29
|
+
) -> MediaFile:
|
|
30
|
+
"""Upload a generic file (archive, document, etc.)."""
|
|
31
|
+
return await self._upload(file, filename=filename)
|
|
32
|
+
|
|
33
|
+
async def upload_video(self, file: Path | IO[bytes]) -> MediaFile:
|
|
34
|
+
"""Upload a video file."""
|
|
35
|
+
return await self._upload(file, content_type="video/mp4")
|
|
36
|
+
|
|
37
|
+
async def upload_audio(self, file: Path | IO[bytes]) -> MediaFile:
|
|
38
|
+
"""Upload an audio file."""
|
|
39
|
+
return await self._upload(file, content_type="audio/mpeg")
|
|
40
|
+
|
|
41
|
+
async def _upload(
|
|
42
|
+
self,
|
|
43
|
+
file: Path | IO[bytes],
|
|
44
|
+
*,
|
|
45
|
+
content_type: str | None = None,
|
|
46
|
+
filename: str | None = None,
|
|
47
|
+
) -> MediaFile:
|
|
48
|
+
"""Internal upload handler."""
|
|
49
|
+
if isinstance(file, Path):
|
|
50
|
+
filename = filename or file.name
|
|
51
|
+
file_bytes = file.read_bytes()
|
|
52
|
+
else:
|
|
53
|
+
file_bytes = file.read()
|
|
54
|
+
filename = filename or getattr(file, "name", "upload")
|
|
55
|
+
|
|
56
|
+
files_payload: dict[str, Any] = {"file": (filename, file_bytes)}
|
|
57
|
+
if content_type:
|
|
58
|
+
files_payload["file"] = (filename, file_bytes, content_type)
|
|
59
|
+
|
|
60
|
+
response = await self._transport.request(
|
|
61
|
+
"POST",
|
|
62
|
+
"/upload/",
|
|
63
|
+
files=files_payload,
|
|
64
|
+
)
|
|
65
|
+
data = response.json()
|
|
66
|
+
return MediaFile.model_validate(data)
|
boostylib/api/posts.py
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"""Posts API — two-step draft → publish flow."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from collections.abc import AsyncIterator
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from boostylib.api.base import BaseAPI
|
|
10
|
+
from boostylib.models.pagination import PaginatedResponse
|
|
11
|
+
from boostylib.models.post import Post, PostCreateRequest
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class PostsAPI(BaseAPI):
|
|
15
|
+
"""Endpoints for post CRUD operations.
|
|
16
|
+
|
|
17
|
+
Post creation uses Boosty's two-step flow:
|
|
18
|
+
1. ``PUT /blog/{name}/post_draft`` — save draft
|
|
19
|
+
2. ``POST /blog/{name}/post_draft/publish/`` — publish draft
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
async def list_posts(
|
|
23
|
+
self,
|
|
24
|
+
username: str,
|
|
25
|
+
*,
|
|
26
|
+
limit: int = 20,
|
|
27
|
+
offset: str | None = None,
|
|
28
|
+
level_id: str | None = None,
|
|
29
|
+
tags_ids: list[str] | None = None,
|
|
30
|
+
from_ts: int | None = None,
|
|
31
|
+
to_ts: int | None = None,
|
|
32
|
+
) -> PaginatedResponse[Post]:
|
|
33
|
+
"""List posts with filtering and pagination."""
|
|
34
|
+
params: dict[str, Any] = {"limit": limit}
|
|
35
|
+
if offset is not None:
|
|
36
|
+
params["offset"] = offset
|
|
37
|
+
if level_id is not None:
|
|
38
|
+
params["level_id"] = level_id
|
|
39
|
+
if tags_ids is not None:
|
|
40
|
+
params["tags_ids"] = ",".join(tags_ids)
|
|
41
|
+
if from_ts is not None:
|
|
42
|
+
params["from_ts"] = from_ts
|
|
43
|
+
if to_ts is not None:
|
|
44
|
+
params["to_ts"] = to_ts
|
|
45
|
+
|
|
46
|
+
data = await self._get(f"/blog/{username}/post/", params=params)
|
|
47
|
+
return self._parse_paginated(data, Post)
|
|
48
|
+
|
|
49
|
+
async def iter_posts(
|
|
50
|
+
self,
|
|
51
|
+
username: str,
|
|
52
|
+
*,
|
|
53
|
+
limit: int = 20,
|
|
54
|
+
level_id: str | None = None,
|
|
55
|
+
) -> AsyncIterator[Post]:
|
|
56
|
+
"""Async iterate over all posts."""
|
|
57
|
+
params: dict[str, Any] = {}
|
|
58
|
+
if level_id is not None:
|
|
59
|
+
params["level_id"] = level_id
|
|
60
|
+
async for post in self._iter_pages(
|
|
61
|
+
f"/blog/{username}/post/", Post, params=params, limit=limit
|
|
62
|
+
):
|
|
63
|
+
yield post
|
|
64
|
+
|
|
65
|
+
async def get_post(self, username: str, post_id: str) -> Post:
|
|
66
|
+
"""Get a single post by ID."""
|
|
67
|
+
data = await self._get(f"/blog/{username}/post/{post_id}")
|
|
68
|
+
return Post.model_validate(data)
|
|
69
|
+
|
|
70
|
+
async def create_post(
|
|
71
|
+
self,
|
|
72
|
+
username: str,
|
|
73
|
+
post: PostCreateRequest,
|
|
74
|
+
*,
|
|
75
|
+
is_showcase_visible: bool = True,
|
|
76
|
+
) -> Post:
|
|
77
|
+
"""Create and publish a post (draft → publish).
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
username: Blog username.
|
|
81
|
+
post: Post content and settings.
|
|
82
|
+
is_showcase_visible: Show in showcase after publishing.
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
The published Post.
|
|
86
|
+
"""
|
|
87
|
+
# Step 1: Save draft
|
|
88
|
+
form_data = self._build_draft_form(post)
|
|
89
|
+
await self._transport.request(
|
|
90
|
+
"PUT",
|
|
91
|
+
f"/blog/{username}/post_draft",
|
|
92
|
+
data=form_data,
|
|
93
|
+
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
# Step 2: Publish
|
|
97
|
+
response = await self._transport.request(
|
|
98
|
+
"POST",
|
|
99
|
+
f"/blog/{username}/post_draft/publish/",
|
|
100
|
+
data={"is_showcase_visible": str(is_showcase_visible).lower()},
|
|
101
|
+
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
|
102
|
+
)
|
|
103
|
+
data = response.json()
|
|
104
|
+
post_data = data.get("data", {}).get("post", data)
|
|
105
|
+
return Post.model_validate(post_data)
|
|
106
|
+
|
|
107
|
+
async def save_draft(self, username: str, post: PostCreateRequest) -> dict[str, Any]:
|
|
108
|
+
"""Save a post as draft without publishing.
|
|
109
|
+
|
|
110
|
+
Returns:
|
|
111
|
+
Raw draft response data.
|
|
112
|
+
"""
|
|
113
|
+
form_data = self._build_draft_form(post)
|
|
114
|
+
response = await self._transport.request(
|
|
115
|
+
"PUT",
|
|
116
|
+
f"/blog/{username}/post_draft",
|
|
117
|
+
data=form_data,
|
|
118
|
+
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
|
119
|
+
)
|
|
120
|
+
return response.json()
|
|
121
|
+
|
|
122
|
+
async def publish_draft(
|
|
123
|
+
self,
|
|
124
|
+
username: str,
|
|
125
|
+
*,
|
|
126
|
+
is_showcase_visible: bool = True,
|
|
127
|
+
) -> Post:
|
|
128
|
+
"""Publish the current draft.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
The published Post.
|
|
132
|
+
"""
|
|
133
|
+
response = await self._transport.request(
|
|
134
|
+
"POST",
|
|
135
|
+
f"/blog/{username}/post_draft/publish/",
|
|
136
|
+
data={"is_showcase_visible": str(is_showcase_visible).lower()},
|
|
137
|
+
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
|
138
|
+
)
|
|
139
|
+
data = response.json()
|
|
140
|
+
post_data = data.get("data", {}).get("post", data)
|
|
141
|
+
return Post.model_validate(post_data)
|
|
142
|
+
|
|
143
|
+
async def update_post(self, username: str, post_id: str, post: PostCreateRequest) -> Post:
|
|
144
|
+
"""Update an existing post."""
|
|
145
|
+
form_data = self._build_draft_form(post)
|
|
146
|
+
response = await self._transport.request(
|
|
147
|
+
"PUT",
|
|
148
|
+
f"/blog/{username}/post/{post_id}",
|
|
149
|
+
data=form_data,
|
|
150
|
+
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
|
151
|
+
)
|
|
152
|
+
data = response.json()
|
|
153
|
+
return Post.model_validate(data.get("data", {}).get("post", data))
|
|
154
|
+
|
|
155
|
+
async def delete_post(self, username: str, post_id: str) -> None:
|
|
156
|
+
"""Delete a post."""
|
|
157
|
+
await self._transport.request("DELETE", f"/blog/{username}/post/{post_id}")
|
|
158
|
+
|
|
159
|
+
def _build_draft_form(self, post: PostCreateRequest) -> dict[str, str]:
|
|
160
|
+
"""Build form-urlencoded data for the draft endpoint."""
|
|
161
|
+
content_blocks = []
|
|
162
|
+
for block in post.content:
|
|
163
|
+
boosty_block: dict[str, Any] = {"type": block.type.value}
|
|
164
|
+
if block.type.value == "text":
|
|
165
|
+
raw_text = block.content or ""
|
|
166
|
+
boosty_block["content"] = json.dumps([raw_text, "unstyled", []])
|
|
167
|
+
boosty_block["modificator"] = ""
|
|
168
|
+
else:
|
|
169
|
+
boosty_block["content"] = block.content or ""
|
|
170
|
+
if block.url:
|
|
171
|
+
boosty_block["url"] = block.url
|
|
172
|
+
content_blocks.append(boosty_block)
|
|
173
|
+
|
|
174
|
+
# Append BLOCK_END
|
|
175
|
+
content_blocks.append({"type": "text", "content": "", "modificator": "BLOCK_END"})
|
|
176
|
+
|
|
177
|
+
form: dict[str, str] = {
|
|
178
|
+
"title": post.title,
|
|
179
|
+
"data": json.dumps(content_blocks),
|
|
180
|
+
"price": str(post.minimum_donation_amount or 0),
|
|
181
|
+
"teaser_data": json.dumps([]),
|
|
182
|
+
"tags": ",".join(post.tags) if post.tags else "",
|
|
183
|
+
"deny_comments": "false",
|
|
184
|
+
"deny_reactions": "false",
|
|
185
|
+
"wait_video": "false",
|
|
186
|
+
"advertiser_info": "",
|
|
187
|
+
"bundle_ids": "",
|
|
188
|
+
}
|
|
189
|
+
if post.scheduled_at is not None:
|
|
190
|
+
form["publish_time"] = str(int(post.scheduled_at.timestamp()))
|
|
191
|
+
return form
|