marqetive-lib 0.1.3__py3-none-any.whl → 0.1.4__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.
- marqetive/__init__.py +54 -55
- marqetive/factory.py +380 -0
- marqetive/platforms/base.py +33 -0
- marqetive/platforms/instagram/__init__.py +1 -3
- marqetive/platforms/instagram/client.py +5 -1
- marqetive/platforms/linkedin/__init__.py +1 -3
- marqetive/platforms/linkedin/client.py +5 -1
- marqetive/platforms/tiktok/__init__.py +1 -3
- marqetive/platforms/tiktok/client.py +10 -5
- marqetive/platforms/tiktok/media.py +1 -3
- marqetive/platforms/twitter/__init__.py +1 -3
- marqetive/platforms/twitter/client.py +5 -1
- {marqetive_lib-0.1.3.dist-info → marqetive_lib-0.1.4.dist-info}/METADATA +1 -1
- marqetive_lib-0.1.4.dist-info/RECORD +35 -0
- marqetive/core/account_factory.py +0 -212
- marqetive/core/base_manager.py +0 -303
- marqetive/core/progress.py +0 -291
- marqetive/core/registry.py +0 -257
- marqetive/platforms/instagram/factory.py +0 -106
- marqetive/platforms/instagram/manager.py +0 -112
- marqetive/platforms/linkedin/factory.py +0 -130
- marqetive/platforms/linkedin/manager.py +0 -119
- marqetive/platforms/tiktok/factory.py +0 -188
- marqetive/platforms/tiktok/manager.py +0 -115
- marqetive/platforms/twitter/factory.py +0 -150
- marqetive/platforms/twitter/manager.py +0 -121
- marqetive/registry_init.py +0 -68
- marqetive_lib-0.1.3.dist-info/RECORD +0 -47
- {marqetive_lib-0.1.3.dist-info → marqetive_lib-0.1.4.dist-info}/WHEEL +0 -0
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
"""Instagram post manager for handling post operations."""
|
|
2
|
-
|
|
3
|
-
import logging
|
|
4
|
-
from typing import Any
|
|
5
|
-
|
|
6
|
-
from marqetive.core.base_manager import BasePostManager
|
|
7
|
-
from marqetive.platforms.instagram.client import InstagramClient
|
|
8
|
-
from marqetive.platforms.instagram.factory import InstagramAccountFactory
|
|
9
|
-
from marqetive.platforms.models import AuthCredentials, Post, PostCreateRequest
|
|
10
|
-
|
|
11
|
-
logger = logging.getLogger(__name__)
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class InstagramPostManager(BasePostManager):
|
|
15
|
-
"""Manager for Instagram post operations.
|
|
16
|
-
|
|
17
|
-
Coordinates post creation, media uploads, and progress tracking for Instagram.
|
|
18
|
-
|
|
19
|
-
Example:
|
|
20
|
-
>>> manager = InstagramPostManager()
|
|
21
|
-
>>> credentials = AuthCredentials(
|
|
22
|
-
... platform="instagram",
|
|
23
|
-
... access_token="token",
|
|
24
|
-
... additional_data={"instagram_business_account_id": "123"}
|
|
25
|
-
... )
|
|
26
|
-
>>> request = PostCreateRequest(content="Hello Instagram!")
|
|
27
|
-
>>> post = await manager.execute_post(credentials, request)
|
|
28
|
-
>>> print(f"Media ID: {post.post_id}")
|
|
29
|
-
"""
|
|
30
|
-
|
|
31
|
-
def __init__(
|
|
32
|
-
self,
|
|
33
|
-
account_factory: InstagramAccountFactory | None = None,
|
|
34
|
-
) -> None:
|
|
35
|
-
"""Initialize Instagram post manager.
|
|
36
|
-
|
|
37
|
-
Args:
|
|
38
|
-
account_factory: Instagram account factory (creates default if None).
|
|
39
|
-
"""
|
|
40
|
-
if account_factory is None:
|
|
41
|
-
account_factory = InstagramAccountFactory()
|
|
42
|
-
|
|
43
|
-
super().__init__(account_factory=account_factory)
|
|
44
|
-
|
|
45
|
-
@property
|
|
46
|
-
def platform_name(self) -> str:
|
|
47
|
-
"""Get platform name."""
|
|
48
|
-
return "instagram"
|
|
49
|
-
|
|
50
|
-
async def _execute_post_impl(
|
|
51
|
-
self,
|
|
52
|
-
client: Any,
|
|
53
|
-
request: PostCreateRequest,
|
|
54
|
-
credentials: AuthCredentials, # noqa: ARG002
|
|
55
|
-
) -> Post:
|
|
56
|
-
"""Execute Instagram post creation.
|
|
57
|
-
|
|
58
|
-
Args:
|
|
59
|
-
client: InstagramClient instance.
|
|
60
|
-
request: Post creation request.
|
|
61
|
-
credentials: Instagram credentials.
|
|
62
|
-
|
|
63
|
-
Returns:
|
|
64
|
-
Created Post object.
|
|
65
|
-
"""
|
|
66
|
-
if not isinstance(client, InstagramClient):
|
|
67
|
-
raise TypeError(f"Expected InstagramClient, got {type(client)}")
|
|
68
|
-
|
|
69
|
-
# Handle media uploads with progress tracking
|
|
70
|
-
media_ids: list[str] = []
|
|
71
|
-
if request.media_urls:
|
|
72
|
-
self._progress_tracker.emit_start(
|
|
73
|
-
"upload_media",
|
|
74
|
-
total=len(request.media_urls),
|
|
75
|
-
message=f"Uploading {len(request.media_urls)} media files...",
|
|
76
|
-
)
|
|
77
|
-
|
|
78
|
-
for idx, media_url in enumerate(request.media_urls):
|
|
79
|
-
if self.is_cancelled():
|
|
80
|
-
raise InterruptedError("Post creation was cancelled")
|
|
81
|
-
|
|
82
|
-
self._progress_tracker.emit_progress(
|
|
83
|
-
"upload_media",
|
|
84
|
-
progress=idx,
|
|
85
|
-
total=len(request.media_urls),
|
|
86
|
-
message=f"Uploading media {idx + 1}/{len(request.media_urls)}...",
|
|
87
|
-
)
|
|
88
|
-
|
|
89
|
-
media_attachment = await client.upload_media(
|
|
90
|
-
media_url=media_url,
|
|
91
|
-
media_type="image", # Default to image
|
|
92
|
-
alt_text=None,
|
|
93
|
-
)
|
|
94
|
-
media_ids.append(media_attachment.media_id)
|
|
95
|
-
|
|
96
|
-
self._progress_tracker.emit_complete(
|
|
97
|
-
"upload_media",
|
|
98
|
-
message="All media uploaded successfully",
|
|
99
|
-
)
|
|
100
|
-
|
|
101
|
-
# Create post with progress tracking
|
|
102
|
-
self._progress_tracker.emit_progress(
|
|
103
|
-
"execute_post",
|
|
104
|
-
progress=50,
|
|
105
|
-
total=100,
|
|
106
|
-
message="Creating Instagram post...",
|
|
107
|
-
)
|
|
108
|
-
|
|
109
|
-
# Use the client to create the post
|
|
110
|
-
post = await client.create_post(request)
|
|
111
|
-
|
|
112
|
-
return post
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
"""LinkedIn account factory for managing credentials and client creation."""
|
|
2
|
-
|
|
3
|
-
import logging
|
|
4
|
-
import os
|
|
5
|
-
from collections.abc import Callable
|
|
6
|
-
|
|
7
|
-
from marqetive.core.account_factory import BaseAccountFactory
|
|
8
|
-
from marqetive.platforms.exceptions import PlatformAuthError
|
|
9
|
-
from marqetive.platforms.linkedin.client import LinkedInClient
|
|
10
|
-
from marqetive.platforms.models import AccountStatus, AuthCredentials
|
|
11
|
-
from marqetive.utils.oauth import refresh_linkedin_token
|
|
12
|
-
|
|
13
|
-
logger = logging.getLogger(__name__)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
class LinkedInAccountFactory(BaseAccountFactory):
|
|
17
|
-
"""Factory for creating and managing LinkedIn accounts and clients.
|
|
18
|
-
|
|
19
|
-
Example:
|
|
20
|
-
>>> factory = LinkedInAccountFactory(
|
|
21
|
-
... client_id="your_client_id",
|
|
22
|
-
... client_secret="your_client_secret"
|
|
23
|
-
... )
|
|
24
|
-
>>> credentials = AuthCredentials(
|
|
25
|
-
... platform="linkedin",
|
|
26
|
-
... access_token="token",
|
|
27
|
-
... refresh_token="refresh"
|
|
28
|
-
... )
|
|
29
|
-
>>> client = await factory.create_authenticated_client(credentials)
|
|
30
|
-
>>> async with client:
|
|
31
|
-
... post = await client.create_post(request)
|
|
32
|
-
"""
|
|
33
|
-
|
|
34
|
-
def __init__(
|
|
35
|
-
self,
|
|
36
|
-
client_id: str | None = None,
|
|
37
|
-
client_secret: str | None = None,
|
|
38
|
-
on_status_update: Callable[[str, AccountStatus], None] | None = None,
|
|
39
|
-
) -> None:
|
|
40
|
-
"""Initialize LinkedIn account factory.
|
|
41
|
-
|
|
42
|
-
Args:
|
|
43
|
-
client_id: LinkedIn OAuth client ID (uses LINKEDIN_CLIENT_ID env if None).
|
|
44
|
-
client_secret: LinkedIn OAuth client secret (uses LINKEDIN_CLIENT_SECRET env if None).
|
|
45
|
-
on_status_update: Optional callback when account status changes.
|
|
46
|
-
"""
|
|
47
|
-
super().__init__(on_status_update=on_status_update)
|
|
48
|
-
self.client_id = client_id or os.getenv("LINKEDIN_CLIENT_ID")
|
|
49
|
-
self.client_secret = client_secret or os.getenv("LINKEDIN_CLIENT_SECRET")
|
|
50
|
-
|
|
51
|
-
if not self.client_id or not self.client_secret:
|
|
52
|
-
logger.warning(
|
|
53
|
-
"LinkedIn client_id/client_secret not provided. "
|
|
54
|
-
"Token refresh will not work."
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
@property
|
|
58
|
-
def platform_name(self) -> str:
|
|
59
|
-
"""Get platform name."""
|
|
60
|
-
return "linkedin"
|
|
61
|
-
|
|
62
|
-
async def refresh_token(self, credentials: AuthCredentials) -> AuthCredentials:
|
|
63
|
-
"""Refresh LinkedIn OAuth2 access token.
|
|
64
|
-
|
|
65
|
-
Args:
|
|
66
|
-
credentials: Current credentials with refresh token.
|
|
67
|
-
|
|
68
|
-
Returns:
|
|
69
|
-
Updated credentials with new access token.
|
|
70
|
-
|
|
71
|
-
Raises:
|
|
72
|
-
PlatformAuthError: If refresh fails or credentials missing.
|
|
73
|
-
"""
|
|
74
|
-
if not self.client_id or not self.client_secret:
|
|
75
|
-
raise PlatformAuthError(
|
|
76
|
-
"LinkedIn client_id and client_secret are required for token refresh",
|
|
77
|
-
platform=self.platform_name,
|
|
78
|
-
)
|
|
79
|
-
|
|
80
|
-
if not credentials.refresh_token:
|
|
81
|
-
raise PlatformAuthError(
|
|
82
|
-
"No refresh token available",
|
|
83
|
-
platform=self.platform_name,
|
|
84
|
-
)
|
|
85
|
-
|
|
86
|
-
logger.info("Refreshing LinkedIn access token...")
|
|
87
|
-
return await refresh_linkedin_token(
|
|
88
|
-
credentials,
|
|
89
|
-
self.client_id,
|
|
90
|
-
self.client_secret,
|
|
91
|
-
)
|
|
92
|
-
|
|
93
|
-
async def create_client(self, credentials: AuthCredentials) -> LinkedInClient:
|
|
94
|
-
"""Create LinkedIn API client.
|
|
95
|
-
|
|
96
|
-
Args:
|
|
97
|
-
credentials: Valid LinkedIn credentials.
|
|
98
|
-
|
|
99
|
-
Returns:
|
|
100
|
-
LinkedInClient instance.
|
|
101
|
-
|
|
102
|
-
Raises:
|
|
103
|
-
PlatformAuthError: If credentials are invalid.
|
|
104
|
-
"""
|
|
105
|
-
if not credentials.access_token:
|
|
106
|
-
raise PlatformAuthError(
|
|
107
|
-
"Access token is required",
|
|
108
|
-
platform=self.platform_name,
|
|
109
|
-
)
|
|
110
|
-
|
|
111
|
-
return LinkedInClient(credentials=credentials)
|
|
112
|
-
|
|
113
|
-
async def validate_credentials(self, credentials: AuthCredentials) -> bool:
|
|
114
|
-
"""Validate LinkedIn credentials by making a test API call.
|
|
115
|
-
|
|
116
|
-
Args:
|
|
117
|
-
credentials: Credentials to validate.
|
|
118
|
-
|
|
119
|
-
Returns:
|
|
120
|
-
True if credentials are valid, False otherwise.
|
|
121
|
-
"""
|
|
122
|
-
try:
|
|
123
|
-
client = await self.create_client(credentials)
|
|
124
|
-
async with client:
|
|
125
|
-
# Try to verify credentials by getting current user
|
|
126
|
-
# This would need to be implemented in the client
|
|
127
|
-
return await client.is_authenticated()
|
|
128
|
-
except Exception as e:
|
|
129
|
-
logger.error(f"Error validating LinkedIn credentials: {e}")
|
|
130
|
-
return False
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
"""LinkedIn post manager for handling post operations."""
|
|
2
|
-
|
|
3
|
-
import logging
|
|
4
|
-
from typing import Any
|
|
5
|
-
|
|
6
|
-
from marqetive.core.base_manager import BasePostManager
|
|
7
|
-
from marqetive.platforms.linkedin.client import LinkedInClient
|
|
8
|
-
from marqetive.platforms.linkedin.factory import LinkedInAccountFactory
|
|
9
|
-
from marqetive.platforms.models import AuthCredentials, Post, PostCreateRequest
|
|
10
|
-
|
|
11
|
-
logger = logging.getLogger(__name__)
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class LinkedInPostManager(BasePostManager):
|
|
15
|
-
"""Manager for LinkedIn post operations.
|
|
16
|
-
|
|
17
|
-
Coordinates post creation, media uploads, and progress tracking for LinkedIn.
|
|
18
|
-
|
|
19
|
-
Example:
|
|
20
|
-
>>> manager = LinkedInPostManager()
|
|
21
|
-
>>> credentials = AuthCredentials(
|
|
22
|
-
... platform="linkedin",
|
|
23
|
-
... access_token="token",
|
|
24
|
-
... refresh_token="refresh"
|
|
25
|
-
... )
|
|
26
|
-
>>> request = PostCreateRequest(content="Hello LinkedIn!")
|
|
27
|
-
>>> post = await manager.execute_post(credentials, request)
|
|
28
|
-
>>> print(f"Post URN: {post.post_id}")
|
|
29
|
-
"""
|
|
30
|
-
|
|
31
|
-
def __init__(
|
|
32
|
-
self,
|
|
33
|
-
account_factory: LinkedInAccountFactory | None = None,
|
|
34
|
-
client_id: str | None = None,
|
|
35
|
-
client_secret: str | None = None,
|
|
36
|
-
) -> None:
|
|
37
|
-
"""Initialize LinkedIn post manager.
|
|
38
|
-
|
|
39
|
-
Args:
|
|
40
|
-
account_factory: LinkedIn account factory (creates default if None).
|
|
41
|
-
client_id: LinkedIn OAuth client ID (for default factory).
|
|
42
|
-
client_secret: LinkedIn OAuth client secret (for default factory).
|
|
43
|
-
"""
|
|
44
|
-
if account_factory is None:
|
|
45
|
-
account_factory = LinkedInAccountFactory(
|
|
46
|
-
client_id=client_id,
|
|
47
|
-
client_secret=client_secret,
|
|
48
|
-
)
|
|
49
|
-
|
|
50
|
-
super().__init__(account_factory=account_factory)
|
|
51
|
-
|
|
52
|
-
@property
|
|
53
|
-
def platform_name(self) -> str:
|
|
54
|
-
"""Get platform name."""
|
|
55
|
-
return "linkedin"
|
|
56
|
-
|
|
57
|
-
async def _execute_post_impl(
|
|
58
|
-
self,
|
|
59
|
-
client: Any,
|
|
60
|
-
request: PostCreateRequest,
|
|
61
|
-
credentials: AuthCredentials, # noqa: ARG002
|
|
62
|
-
) -> Post:
|
|
63
|
-
"""Execute LinkedIn post creation.
|
|
64
|
-
|
|
65
|
-
Args:
|
|
66
|
-
client: LinkedInClient instance.
|
|
67
|
-
request: Post creation request.
|
|
68
|
-
credentials: LinkedIn credentials.
|
|
69
|
-
|
|
70
|
-
Returns:
|
|
71
|
-
Created Post object.
|
|
72
|
-
"""
|
|
73
|
-
if not isinstance(client, LinkedInClient):
|
|
74
|
-
raise TypeError(f"Expected LinkedInClient, got {type(client)}")
|
|
75
|
-
|
|
76
|
-
# Handle media uploads with progress tracking
|
|
77
|
-
media_ids: list[str] = []
|
|
78
|
-
if request.media_urls:
|
|
79
|
-
self._progress_tracker.emit_start(
|
|
80
|
-
"upload_media",
|
|
81
|
-
total=len(request.media_urls),
|
|
82
|
-
message=f"Uploading {len(request.media_urls)} media files...",
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
for idx, media_url in enumerate(request.media_urls):
|
|
86
|
-
if self.is_cancelled():
|
|
87
|
-
raise InterruptedError("Post creation was cancelled")
|
|
88
|
-
|
|
89
|
-
self._progress_tracker.emit_progress(
|
|
90
|
-
"upload_media",
|
|
91
|
-
progress=idx,
|
|
92
|
-
total=len(request.media_urls),
|
|
93
|
-
message=f"Uploading media {idx + 1}/{len(request.media_urls)}...",
|
|
94
|
-
)
|
|
95
|
-
|
|
96
|
-
media_attachment = await client.upload_media(
|
|
97
|
-
media_url=media_url,
|
|
98
|
-
media_type="image", # Default to image
|
|
99
|
-
alt_text=None,
|
|
100
|
-
)
|
|
101
|
-
media_ids.append(media_attachment.media_id)
|
|
102
|
-
|
|
103
|
-
self._progress_tracker.emit_complete(
|
|
104
|
-
"upload_media",
|
|
105
|
-
message="All media uploaded successfully",
|
|
106
|
-
)
|
|
107
|
-
|
|
108
|
-
# Create post with progress tracking
|
|
109
|
-
self._progress_tracker.emit_progress(
|
|
110
|
-
"execute_post",
|
|
111
|
-
progress=50,
|
|
112
|
-
total=100,
|
|
113
|
-
message="Creating LinkedIn post...",
|
|
114
|
-
)
|
|
115
|
-
|
|
116
|
-
# Use the client to create the post
|
|
117
|
-
post = await client.create_post(request)
|
|
118
|
-
|
|
119
|
-
return post
|
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
"""TikTok account factory for managing credentials and client creation."""
|
|
2
|
-
|
|
3
|
-
import logging
|
|
4
|
-
import os
|
|
5
|
-
from collections.abc import Callable
|
|
6
|
-
from datetime import datetime, timedelta
|
|
7
|
-
|
|
8
|
-
from marqetive.core.account_factory import BaseAccountFactory
|
|
9
|
-
from marqetive.platforms.exceptions import PlatformAuthError
|
|
10
|
-
from marqetive.platforms.models import AccountStatus, AuthCredentials
|
|
11
|
-
from marqetive.platforms.tiktok.client import TikTokClient
|
|
12
|
-
from marqetive.utils.oauth import fetch_tiktok_token, refresh_tiktok_token
|
|
13
|
-
|
|
14
|
-
logger = logging.getLogger(__name__)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
class TikTokAccountFactory(BaseAccountFactory):
|
|
18
|
-
"""Factory for creating and managing TikTok accounts and clients.
|
|
19
|
-
|
|
20
|
-
This factory handles the instantiation of TikTokClients, manages OAuth
|
|
21
|
-
credentials, and provides a mechanism for token refreshing.
|
|
22
|
-
"""
|
|
23
|
-
|
|
24
|
-
def __init__(
|
|
25
|
-
self,
|
|
26
|
-
client_id: str | None = None,
|
|
27
|
-
client_secret: str | None = None,
|
|
28
|
-
on_status_update: Callable[[str, AccountStatus], None] | None = None,
|
|
29
|
-
) -> None:
|
|
30
|
-
"""Initialize TikTok account factory.
|
|
31
|
-
|
|
32
|
-
Args:
|
|
33
|
-
client_id: TikTok App client ID (uses TIKTOK_CLIENT_ID env if None).
|
|
34
|
-
client_secret: TikTok App client secret (uses TIKTOK_CLIENT_SECRET env if None).
|
|
35
|
-
on_status_update: Optional callback when account status changes.
|
|
36
|
-
"""
|
|
37
|
-
super().__init__(on_status_update=on_status_update)
|
|
38
|
-
self.client_id = client_id or os.getenv("TIKTOK_CLIENT_ID")
|
|
39
|
-
self.client_secret = client_secret or os.getenv("TIKTOK_CLIENT_SECRET")
|
|
40
|
-
|
|
41
|
-
if not self.client_id or not self.client_secret:
|
|
42
|
-
logger.warning(
|
|
43
|
-
"TikTok client_id/client_secret not provided. "
|
|
44
|
-
"Token refresh may not work."
|
|
45
|
-
)
|
|
46
|
-
|
|
47
|
-
@property
|
|
48
|
-
def platform_name(self) -> str:
|
|
49
|
-
"""Get the platform name."""
|
|
50
|
-
return "tiktok"
|
|
51
|
-
|
|
52
|
-
async def get_credentials_from_auth_code(
|
|
53
|
-
self,
|
|
54
|
-
auth_code: str,
|
|
55
|
-
redirect_uri: str,
|
|
56
|
-
code_verifier: str | None = None,
|
|
57
|
-
) -> AuthCredentials:
|
|
58
|
-
"""Get credentials from an authorization code.
|
|
59
|
-
|
|
60
|
-
This method exchanges an authorization code for an access token and
|
|
61
|
-
formats it into the standard AuthCredentials object.
|
|
62
|
-
|
|
63
|
-
Args:
|
|
64
|
-
auth_code: The authorization code received from TikTok.
|
|
65
|
-
redirect_uri: The redirect URI used in the initial auth request.
|
|
66
|
-
code_verifier: The PKCE code verifier, if used.
|
|
67
|
-
|
|
68
|
-
Returns:
|
|
69
|
-
An AuthCredentials object for the user.
|
|
70
|
-
|
|
71
|
-
Raises:
|
|
72
|
-
PlatformAuthError: If the token exchange fails.
|
|
73
|
-
"""
|
|
74
|
-
if not self.client_id or not self.client_secret:
|
|
75
|
-
raise PlatformAuthError(
|
|
76
|
-
"TikTok client_id and client_secret are required.",
|
|
77
|
-
platform=self.platform_name,
|
|
78
|
-
)
|
|
79
|
-
|
|
80
|
-
token_data = await fetch_tiktok_token(
|
|
81
|
-
code=auth_code,
|
|
82
|
-
client_id=self.client_id,
|
|
83
|
-
client_secret=self.client_secret,
|
|
84
|
-
redirect_uri=redirect_uri,
|
|
85
|
-
code_verifier=code_verifier,
|
|
86
|
-
)
|
|
87
|
-
|
|
88
|
-
expires_at = None
|
|
89
|
-
if "expires_in" in token_data:
|
|
90
|
-
expires_at = datetime.now() + timedelta(seconds=token_data["expires_in"])
|
|
91
|
-
|
|
92
|
-
refresh_expires_at = None
|
|
93
|
-
if "refresh_expires_in" in token_data:
|
|
94
|
-
refresh_expires_at = datetime.now() + timedelta(
|
|
95
|
-
seconds=token_data["refresh_expires_in"]
|
|
96
|
-
)
|
|
97
|
-
|
|
98
|
-
credentials = AuthCredentials(
|
|
99
|
-
platform=self.platform_name,
|
|
100
|
-
access_token=token_data["access_token"],
|
|
101
|
-
refresh_token=token_data.get("refresh_token"),
|
|
102
|
-
expires_at=expires_at,
|
|
103
|
-
scope=token_data.get("scope", []),
|
|
104
|
-
additional_data={
|
|
105
|
-
"open_id": token_data["open_id"],
|
|
106
|
-
"token_type": token_data.get("token_type"),
|
|
107
|
-
"refresh_expires_at": refresh_expires_at,
|
|
108
|
-
},
|
|
109
|
-
)
|
|
110
|
-
return credentials
|
|
111
|
-
|
|
112
|
-
async def refresh_token(self, credentials: AuthCredentials) -> AuthCredentials:
|
|
113
|
-
"""Refresh a TikTok OAuth2 access token.
|
|
114
|
-
|
|
115
|
-
Args:
|
|
116
|
-
credentials: The current credentials containing the refresh token.
|
|
117
|
-
|
|
118
|
-
Returns:
|
|
119
|
-
Updated credentials with a new access token.
|
|
120
|
-
|
|
121
|
-
Raises:
|
|
122
|
-
PlatformAuthError: If refresh fails or credentials are missing.
|
|
123
|
-
"""
|
|
124
|
-
if not self.client_id or not self.client_secret:
|
|
125
|
-
raise PlatformAuthError(
|
|
126
|
-
"TikTok client_id and client_secret are required for token refresh.",
|
|
127
|
-
platform=self.platform_name,
|
|
128
|
-
)
|
|
129
|
-
|
|
130
|
-
if not credentials.refresh_token:
|
|
131
|
-
raise PlatformAuthError(
|
|
132
|
-
"No refresh token available for TikTok.", platform=self.platform_name
|
|
133
|
-
)
|
|
134
|
-
|
|
135
|
-
logger.info("Refreshing TikTok access token...")
|
|
136
|
-
# This function would call the actual TikTok token refresh endpoint
|
|
137
|
-
return await refresh_tiktok_token(
|
|
138
|
-
credentials, self.client_id, self.client_secret
|
|
139
|
-
)
|
|
140
|
-
|
|
141
|
-
async def create_client(self, credentials: AuthCredentials) -> TikTokClient:
|
|
142
|
-
"""Create a TikTok API client.
|
|
143
|
-
|
|
144
|
-
Args:
|
|
145
|
-
credentials: Valid TikTok authentication credentials.
|
|
146
|
-
|
|
147
|
-
Returns:
|
|
148
|
-
An instance of TikTokClient.
|
|
149
|
-
|
|
150
|
-
Raises:
|
|
151
|
-
PlatformAuthError: If credentials are incomplete or invalid.
|
|
152
|
-
"""
|
|
153
|
-
if not credentials.access_token:
|
|
154
|
-
raise PlatformAuthError(
|
|
155
|
-
"Access token is required for TikTok.", platform=self.platform_name
|
|
156
|
-
)
|
|
157
|
-
if (
|
|
158
|
-
not credentials.additional_data
|
|
159
|
-
or "open_id" not in credentials.additional_data
|
|
160
|
-
):
|
|
161
|
-
raise PlatformAuthError(
|
|
162
|
-
"'open_id' must be provided in additional_data for TikTok.",
|
|
163
|
-
platform=self.platform_name,
|
|
164
|
-
)
|
|
165
|
-
|
|
166
|
-
return TikTokClient(credentials=credentials)
|
|
167
|
-
|
|
168
|
-
async def validate_credentials(self, credentials: AuthCredentials) -> bool:
|
|
169
|
-
"""Validate TikTok credentials by making a test API call.
|
|
170
|
-
|
|
171
|
-
Args:
|
|
172
|
-
credentials: The credentials to validate.
|
|
173
|
-
|
|
174
|
-
Returns:
|
|
175
|
-
True if the credentials are valid, False otherwise.
|
|
176
|
-
"""
|
|
177
|
-
try:
|
|
178
|
-
client = await self.create_client(credentials)
|
|
179
|
-
async with client:
|
|
180
|
-
return await client.is_authenticated()
|
|
181
|
-
except PlatformAuthError as e:
|
|
182
|
-
logger.warning(f"TikTok credential validation failed: {e}")
|
|
183
|
-
return False
|
|
184
|
-
except Exception as e:
|
|
185
|
-
logger.error(
|
|
186
|
-
f"An unexpected error occurred during TikTok credential validation: {e}"
|
|
187
|
-
)
|
|
188
|
-
return False
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
"""TikTok post manager for handling video post operations."""
|
|
2
|
-
|
|
3
|
-
import logging
|
|
4
|
-
from typing import Any
|
|
5
|
-
|
|
6
|
-
from marqetive.core.base_manager import BasePostManager
|
|
7
|
-
from marqetive.platforms.models import AuthCredentials, Post, PostCreateRequest
|
|
8
|
-
from marqetive.platforms.tiktok.client import TikTokClient
|
|
9
|
-
from marqetive.platforms.tiktok.factory import TikTokAccountFactory
|
|
10
|
-
|
|
11
|
-
logger = logging.getLogger(__name__)
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class TikTokPostManager(BasePostManager):
|
|
15
|
-
"""Manager for TikTok video post operations.
|
|
16
|
-
|
|
17
|
-
This manager coordinates the multi-step process of posting a video to
|
|
18
|
-
TikTok, including media upload, processing, and the final publishing step.
|
|
19
|
-
It provides progress tracking throughout the operation.
|
|
20
|
-
"""
|
|
21
|
-
|
|
22
|
-
def __init__(
|
|
23
|
-
self,
|
|
24
|
-
account_factory: TikTokAccountFactory | None = None,
|
|
25
|
-
client_id: str | None = None,
|
|
26
|
-
client_secret: str | None = None,
|
|
27
|
-
) -> None:
|
|
28
|
-
"""Initialize the TikTok post manager.
|
|
29
|
-
|
|
30
|
-
Args:
|
|
31
|
-
account_factory: An instance of TikTokAccountFactory. If not provided,
|
|
32
|
-
a default one will be created.
|
|
33
|
-
client_id: TikTok App client ID (for the default factory).
|
|
34
|
-
client_secret: TikTok App client secret (for the default factory).
|
|
35
|
-
"""
|
|
36
|
-
if account_factory is None:
|
|
37
|
-
account_factory = TikTokAccountFactory(
|
|
38
|
-
client_id=client_id,
|
|
39
|
-
client_secret=client_secret,
|
|
40
|
-
)
|
|
41
|
-
super().__init__(account_factory=account_factory)
|
|
42
|
-
|
|
43
|
-
@property
|
|
44
|
-
def platform_name(self) -> str:
|
|
45
|
-
"""Get the platform name."""
|
|
46
|
-
return "tiktok"
|
|
47
|
-
|
|
48
|
-
async def _execute_post_impl(
|
|
49
|
-
self,
|
|
50
|
-
client: Any,
|
|
51
|
-
request: PostCreateRequest,
|
|
52
|
-
credentials: AuthCredentials, # noqa: ARG002
|
|
53
|
-
) -> Post:
|
|
54
|
-
"""Execute the TikTok video post creation process.
|
|
55
|
-
|
|
56
|
-
Args:
|
|
57
|
-
client: An authenticated TikTokClient instance.
|
|
58
|
-
request: The post creation request.
|
|
59
|
-
credentials: The credentials used for authentication.
|
|
60
|
-
|
|
61
|
-
Returns:
|
|
62
|
-
The created Post object representing the TikTok video.
|
|
63
|
-
|
|
64
|
-
Raises:
|
|
65
|
-
TypeError: If the provided client is not a TikTokClient.
|
|
66
|
-
InterruptedError: If the operation is cancelled.
|
|
67
|
-
"""
|
|
68
|
-
if not isinstance(client, TikTokClient):
|
|
69
|
-
raise TypeError(f"Expected TikTokClient, got {type(client)}")
|
|
70
|
-
|
|
71
|
-
# The TikTok posting process is primarily about the video upload.
|
|
72
|
-
# The client's create_post method handles the upload and publish steps.
|
|
73
|
-
# We can wrap it here to provide progress updates.
|
|
74
|
-
|
|
75
|
-
self._progress_tracker.emit_start(
|
|
76
|
-
"execute_post", total=100, message="Starting TikTok post..."
|
|
77
|
-
)
|
|
78
|
-
|
|
79
|
-
if self.is_cancelled():
|
|
80
|
-
raise InterruptedError("Post creation was cancelled before start.")
|
|
81
|
-
|
|
82
|
-
# Media upload progress can be tracked inside the client, but for simplicity,
|
|
83
|
-
# we'll emit high-level progress here.
|
|
84
|
-
self._progress_tracker.emit_progress(
|
|
85
|
-
"execute_post",
|
|
86
|
-
progress=10,
|
|
87
|
-
total=100,
|
|
88
|
-
message="Uploading video to TikTok...",
|
|
89
|
-
)
|
|
90
|
-
|
|
91
|
-
# The `create_post` method in the client will handle the full flow:
|
|
92
|
-
# 1. Upload media
|
|
93
|
-
# 2. Publish post
|
|
94
|
-
# 3. Fetch final post data
|
|
95
|
-
# A more advanced implementation might involve callbacks from the client
|
|
96
|
-
# to the manager for more granular progress updates.
|
|
97
|
-
post = await client.create_post(request)
|
|
98
|
-
|
|
99
|
-
if self.is_cancelled():
|
|
100
|
-
# If cancellation happened during the client call, we might need cleanup.
|
|
101
|
-
# For now, we just raise.
|
|
102
|
-
raise InterruptedError("Post creation was cancelled during execution.")
|
|
103
|
-
|
|
104
|
-
self._progress_tracker.emit_progress(
|
|
105
|
-
"execute_post",
|
|
106
|
-
progress=90,
|
|
107
|
-
total=100,
|
|
108
|
-
message="Finalizing post...",
|
|
109
|
-
)
|
|
110
|
-
|
|
111
|
-
self._progress_tracker.emit_complete(
|
|
112
|
-
"execute_post", message="TikTok post published successfully!"
|
|
113
|
-
)
|
|
114
|
-
|
|
115
|
-
return post
|