instapost 0.1.1__tar.gz → 0.2.1__tar.gz
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.
- {instapost-0.1.1 → instapost-0.2.1}/PKG-INFO +2 -2
- {instapost-0.1.1 → instapost-0.2.1}/README.md +1 -1
- {instapost-0.1.1 → instapost-0.2.1}/instapost/__init__.py +1 -1
- {instapost-0.1.1 → instapost-0.2.1}/instapost/api/base.py +27 -5
- {instapost-0.1.1 → instapost-0.2.1}/instapost/client.py +40 -28
- {instapost-0.1.1 → instapost-0.2.1}/instapost/media/uploader.py +29 -7
- {instapost-0.1.1 → instapost-0.2.1}/instapost/posting/carousel.py +17 -8
- {instapost-0.1.1 → instapost-0.2.1}/instapost/posting/image.py +7 -0
- {instapost-0.1.1 → instapost-0.2.1}/instapost/posting/reel.py +6 -0
- {instapost-0.1.1 → instapost-0.2.1}/pyproject.toml +1 -1
- {instapost-0.1.1 → instapost-0.2.1}/tests/test_client.py +10 -10
- {instapost-0.1.1 → instapost-0.2.1}/tests/test_posting.py +8 -8
- {instapost-0.1.1 → instapost-0.2.1}/.github/workflows/publish-to-pypi.yml +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/.github/workflows/python-package.yml +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/.gitignore +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/LICENSE +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/MANIFEST.in +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/instapost/api/__init__.py +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/instapost/exceptions.py +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/instapost/media/__init__.py +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/instapost/posting/__init__.py +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/logo.png +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/requirements.txt +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/tests/__init__.py +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/tests/conftest.py +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/tests/test_api.py +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/tests/test_exceptions.py +0 -0
- {instapost-0.1.1 → instapost-0.2.1}/tests/test_media_uploader.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: instapost
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.1
|
|
4
4
|
Summary: A Python library for automating Instagram posting (images, reels, carousels)
|
|
5
5
|
Project-URL: Homepage, https://blaya.ia.br
|
|
6
6
|
Project-URL: Repository, https://github.com/pedroluz/instapost
|
|
@@ -96,7 +96,7 @@ poster.post_reel(video="./video.mp4", caption="New reel! 🎬", share_to_feed=Tr
|
|
|
96
96
|
|
|
97
97
|
# Post a carousel
|
|
98
98
|
poster.post_carousel(
|
|
99
|
-
|
|
99
|
+
media_items=[
|
|
100
100
|
{"media": "./photo1.jpg", "type": "IMAGE"},
|
|
101
101
|
{"media": "./video.mp4", "type": "VIDEO"},
|
|
102
102
|
],
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import logging
|
|
1
2
|
import requests
|
|
2
3
|
import time
|
|
3
4
|
from abc import ABC, abstractmethod
|
|
4
5
|
|
|
5
6
|
from ..exceptions import ContainerProcessingError, ContainerTimeoutError
|
|
6
7
|
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
|
|
7
10
|
|
|
8
11
|
class BaseInstagramClient(ABC):
|
|
9
12
|
"""Base class for Instagram API clients."""
|
|
@@ -18,47 +21,65 @@ class BaseInstagramClient(ABC):
|
|
|
18
21
|
"""Make a request to the Graph API."""
|
|
19
22
|
url = f"{self.BASE_URL}/{endpoint}"
|
|
20
23
|
kwargs.setdefault("params", {})["access_token"] = self.access_token
|
|
24
|
+
logger.debug(f"Making {method} request to {endpoint}")
|
|
21
25
|
response = requests.request(method, url, **kwargs)
|
|
22
26
|
response.raise_for_status()
|
|
27
|
+
logger.debug(f"{method} request to {endpoint} successful")
|
|
23
28
|
return response.json()
|
|
24
29
|
|
|
25
30
|
def _create_container(self, params: dict) -> str:
|
|
26
31
|
"""Create a media container and return its ID."""
|
|
32
|
+
logger.info(f"Creating media container with params: {params}")
|
|
27
33
|
result = self._request("POST", "me/media", params=params)
|
|
28
|
-
|
|
34
|
+
container_id = result["id"]
|
|
35
|
+
logger.info(f"Media container created with ID: {container_id}")
|
|
36
|
+
return container_id
|
|
29
37
|
|
|
30
38
|
def _publish_container(self, container_id: str) -> dict:
|
|
31
39
|
"""Publish a media container."""
|
|
32
|
-
|
|
40
|
+
logger.info(f"Publishing media container {container_id}")
|
|
41
|
+
result = self._request(
|
|
33
42
|
"POST",
|
|
34
43
|
"me/media_publish",
|
|
35
44
|
params={"creation_id": container_id},
|
|
36
45
|
)
|
|
46
|
+
logger.info(f"Media container {container_id} published successfully")
|
|
47
|
+
return result
|
|
37
48
|
|
|
38
49
|
def _wait_for_container(self, container_id: str, timeout: int = 60, interval: int = 5) -> None:
|
|
39
50
|
"""Wait for a media container to finish processing."""
|
|
51
|
+
logger.info(f"Waiting for container {container_id} to finish processing (timeout: {timeout}s)")
|
|
40
52
|
elapsed = 0
|
|
41
53
|
while elapsed < timeout:
|
|
42
54
|
result = self._request("GET", container_id, params={"fields": "status_code,status"})
|
|
43
55
|
status = result.get("status_code")
|
|
56
|
+
logger.debug(f"Container {container_id} status: {status}")
|
|
44
57
|
|
|
45
58
|
if status == "FINISHED":
|
|
59
|
+
logger.info(f"Container {container_id} finished processing")
|
|
46
60
|
return
|
|
47
61
|
if status == "ERROR":
|
|
48
|
-
|
|
62
|
+
error_msg = f"Container processing failed: {result.get('status')}"
|
|
63
|
+
logger.error(error_msg)
|
|
64
|
+
raise ContainerProcessingError(error_msg)
|
|
49
65
|
|
|
50
66
|
time.sleep(interval)
|
|
51
67
|
elapsed += interval
|
|
52
68
|
|
|
53
|
-
|
|
69
|
+
error_msg = f"Container {container_id} did not finish processing in {timeout}s"
|
|
70
|
+
logger.error(error_msg)
|
|
71
|
+
raise ContainerTimeoutError(error_msg)
|
|
54
72
|
|
|
55
73
|
def verify(self) -> dict:
|
|
56
74
|
"""Verify credentials and return account info."""
|
|
57
|
-
|
|
75
|
+
logger.info("Verifying Instagram credentials")
|
|
76
|
+
result = self._request(
|
|
58
77
|
"GET",
|
|
59
78
|
"me",
|
|
60
79
|
params={"fields": "id,username,name"},
|
|
61
80
|
)
|
|
81
|
+
logger.info(f"Credentials verified for user: {result.get('username')}")
|
|
82
|
+
return result
|
|
62
83
|
|
|
63
84
|
def publish(self, container_id: str) -> dict:
|
|
64
85
|
"""
|
|
@@ -70,6 +91,7 @@ class BaseInstagramClient(ABC):
|
|
|
70
91
|
Returns:
|
|
71
92
|
API response with the published media ID.
|
|
72
93
|
"""
|
|
94
|
+
logger.info(f"Publishing container {container_id}")
|
|
73
95
|
return self._publish_container(container_id)
|
|
74
96
|
|
|
75
97
|
@abstractmethod
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import logging
|
|
1
2
|
from typing import Optional
|
|
2
3
|
import os
|
|
3
4
|
|
|
@@ -7,6 +8,8 @@ from .posting.reel import ReelPoster
|
|
|
7
8
|
from .posting.carousel import CarouselPoster
|
|
8
9
|
from .media.uploader import MediaUploader
|
|
9
10
|
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
10
13
|
|
|
11
14
|
class InstagramPoster(BaseInstagramClient):
|
|
12
15
|
"""Unified client for posting images, reels, and carousels to Instagram."""
|
|
@@ -26,93 +29,102 @@ class InstagramPoster(BaseInstagramClient):
|
|
|
26
29
|
raise NotImplementedError("Use post_image, post_reel, or post_carousel instead")
|
|
27
30
|
|
|
28
31
|
# Image methods
|
|
29
|
-
def upload_image(self,
|
|
32
|
+
def upload_image(self, image: str, caption: str = "") -> str:
|
|
30
33
|
"""Upload a single image and return container ID."""
|
|
31
|
-
return self._image.upload(
|
|
34
|
+
return self._image.upload(image, caption)
|
|
32
35
|
|
|
33
|
-
def post_image(self,
|
|
36
|
+
def post_image(self, image: str, caption: str = "") -> dict:
|
|
34
37
|
"""Upload and publish a single image.
|
|
35
38
|
|
|
36
39
|
Args:
|
|
37
|
-
|
|
40
|
+
image: URL or local file path to image
|
|
38
41
|
caption: Optional caption
|
|
39
42
|
|
|
40
43
|
Returns:
|
|
41
44
|
API response with published media ID
|
|
42
45
|
"""
|
|
46
|
+
image_display = image[:50] + "..." if len(image) > 50 else image
|
|
47
|
+
logger.info(f"Posting image: {image_display}")
|
|
43
48
|
# Auto-upload if local file
|
|
44
|
-
if os.path.exists(
|
|
45
|
-
|
|
49
|
+
if os.path.exists(image):
|
|
50
|
+
logger.info("Detected local file, uploading to Catbox first")
|
|
51
|
+
image = MediaUploader.upload_image(image)
|
|
46
52
|
|
|
47
|
-
return self._image.post(
|
|
53
|
+
return self._image.post(image, caption)
|
|
48
54
|
|
|
49
55
|
# Reel methods
|
|
50
56
|
def upload_reel(
|
|
51
57
|
self,
|
|
52
|
-
|
|
58
|
+
video: str,
|
|
53
59
|
caption: str = "",
|
|
54
|
-
|
|
60
|
+
cover: Optional[str] = None,
|
|
55
61
|
share_to_feed: bool = True,
|
|
56
62
|
timeout: int = 120,
|
|
57
63
|
) -> str:
|
|
58
64
|
"""Upload a reel and return container ID."""
|
|
59
|
-
return self._reel.upload(
|
|
65
|
+
return self._reel.upload(video, caption, cover, share_to_feed, timeout)
|
|
60
66
|
|
|
61
67
|
def post_reel(
|
|
62
68
|
self,
|
|
63
|
-
|
|
69
|
+
video: str,
|
|
64
70
|
caption: str = "",
|
|
65
|
-
|
|
71
|
+
cover: Optional[str] = None,
|
|
66
72
|
share_to_feed: bool = True,
|
|
67
73
|
timeout: int = 120,
|
|
68
74
|
) -> dict:
|
|
69
75
|
"""Upload and publish a reel.
|
|
70
76
|
|
|
71
77
|
Args:
|
|
72
|
-
|
|
78
|
+
video: URL or local file path to video
|
|
73
79
|
caption: Optional caption
|
|
74
|
-
|
|
80
|
+
cover: Optional URL or local path to cover image
|
|
75
81
|
share_to_feed: Whether to also share to feed
|
|
76
82
|
timeout: Video processing timeout in seconds
|
|
77
83
|
|
|
78
84
|
Returns:
|
|
79
85
|
API response with published media ID
|
|
80
86
|
"""
|
|
87
|
+
video_display = video[:50] + "..." if len(video) > 50 else video
|
|
88
|
+
logger.info(f"Posting reel: {video_display}")
|
|
81
89
|
# Auto-upload if local files
|
|
82
|
-
if os.path.exists(
|
|
83
|
-
|
|
90
|
+
if os.path.exists(video):
|
|
91
|
+
logger.info("Detected local video, uploading to Catbox first")
|
|
92
|
+
video = MediaUploader.upload_video(video)
|
|
84
93
|
|
|
85
|
-
if
|
|
86
|
-
|
|
94
|
+
if cover and os.path.exists(cover):
|
|
95
|
+
logger.info("Detected local cover image, uploading to Catbox first")
|
|
96
|
+
cover = MediaUploader.upload_image(cover)
|
|
87
97
|
|
|
88
|
-
return self._reel.post(
|
|
98
|
+
return self._reel.post(video, caption, cover, share_to_feed, timeout)
|
|
89
99
|
|
|
90
100
|
# Carousel methods
|
|
91
|
-
def upload_carousel(self,
|
|
101
|
+
def upload_carousel(self, media_items: list[dict], caption: str = "") -> str:
|
|
92
102
|
"""Upload a carousel and return container ID."""
|
|
93
|
-
return self._carousel.upload(
|
|
103
|
+
return self._carousel.upload(media_items, caption)
|
|
94
104
|
|
|
95
|
-
def post_carousel(self,
|
|
105
|
+
def post_carousel(self, media_items: list[dict], caption: str = "") -> dict:
|
|
96
106
|
"""Upload and publish a carousel.
|
|
97
107
|
|
|
98
108
|
Args:
|
|
99
|
-
|
|
100
|
-
|
|
109
|
+
media_items: List of dicts with 'media' (local file path or publicly accessible URL)
|
|
110
|
+
and 'type' keys. Type must be 'IMAGE' or 'VIDEO'
|
|
101
111
|
caption: Optional caption
|
|
102
112
|
|
|
103
113
|
Returns:
|
|
104
114
|
API response with published media ID
|
|
105
115
|
"""
|
|
116
|
+
logger.info(f"Posting carousel with {len(media_items)} items")
|
|
106
117
|
# Auto-upload local files
|
|
107
|
-
|
|
108
|
-
for item in
|
|
118
|
+
processed_items = []
|
|
119
|
+
for item in media_items:
|
|
109
120
|
media = item['media']
|
|
110
121
|
if os.path.exists(media):
|
|
122
|
+
logger.info("Detected local file in carousel, uploading to Catbox first")
|
|
111
123
|
media = MediaUploader.upload(media)
|
|
112
124
|
|
|
113
|
-
|
|
125
|
+
processed_items.append({
|
|
114
126
|
'media': media,
|
|
115
127
|
'type': item['type']
|
|
116
128
|
})
|
|
117
129
|
|
|
118
|
-
return self._carousel.post(
|
|
130
|
+
return self._carousel.post(processed_items, caption)
|
|
@@ -3,10 +3,13 @@ Media upload utilities for converting local files to public URLs.
|
|
|
3
3
|
Uses Catbox for all file uploads (images and videos).
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
|
+
import logging
|
|
6
7
|
import os
|
|
7
8
|
import requests
|
|
8
9
|
from pathlib import Path
|
|
9
10
|
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
10
13
|
|
|
11
14
|
class MediaUploader:
|
|
12
15
|
"""Upload local media files to Catbox and return public URLs."""
|
|
@@ -33,17 +36,22 @@ class MediaUploader:
|
|
|
33
36
|
ValueError: If file type is not supported
|
|
34
37
|
requests.RequestException: If upload fails
|
|
35
38
|
"""
|
|
39
|
+
logger.info(f"Uploading media file: {file_path}")
|
|
36
40
|
if not os.path.exists(file_path):
|
|
37
|
-
|
|
41
|
+
error_msg = f"Media file not found: {file_path}"
|
|
42
|
+
logger.error(error_msg)
|
|
43
|
+
raise FileNotFoundError(error_msg)
|
|
38
44
|
|
|
39
45
|
file_ext = Path(file_path).suffix.lower()
|
|
40
46
|
|
|
41
47
|
# Validate file type
|
|
42
48
|
if file_ext not in (cls.SUPPORTED_IMAGES | cls.SUPPORTED_VIDEOS):
|
|
43
|
-
|
|
49
|
+
error_msg = (
|
|
44
50
|
f"Unsupported file type: {file_ext}. "
|
|
45
51
|
f"Supported: {cls.SUPPORTED_IMAGES | cls.SUPPORTED_VIDEOS}"
|
|
46
52
|
)
|
|
53
|
+
logger.error(error_msg)
|
|
54
|
+
raise ValueError(error_msg)
|
|
47
55
|
|
|
48
56
|
return cls._upload_to_catbox(file_path)
|
|
49
57
|
|
|
@@ -58,12 +66,17 @@ class MediaUploader:
|
|
|
58
66
|
Returns:
|
|
59
67
|
Public URL of the uploaded image
|
|
60
68
|
"""
|
|
69
|
+
logger.info(f"Uploading image: {image_path}")
|
|
61
70
|
if not os.path.exists(image_path):
|
|
62
|
-
|
|
71
|
+
error_msg = f"Image file not found: {image_path}"
|
|
72
|
+
logger.error(error_msg)
|
|
73
|
+
raise FileNotFoundError(error_msg)
|
|
63
74
|
|
|
64
75
|
file_ext = Path(image_path).suffix.lower()
|
|
65
76
|
if file_ext not in cls.SUPPORTED_IMAGES:
|
|
66
|
-
|
|
77
|
+
error_msg = f"Unsupported image format: {file_ext}"
|
|
78
|
+
logger.error(error_msg)
|
|
79
|
+
raise ValueError(error_msg)
|
|
67
80
|
|
|
68
81
|
return cls._upload_to_catbox(image_path)
|
|
69
82
|
|
|
@@ -78,12 +91,17 @@ class MediaUploader:
|
|
|
78
91
|
Returns:
|
|
79
92
|
Public URL of the uploaded video
|
|
80
93
|
"""
|
|
94
|
+
logger.info(f"Uploading video: {video_path}")
|
|
81
95
|
if not os.path.exists(video_path):
|
|
82
|
-
|
|
96
|
+
error_msg = f"Video file not found: {video_path}"
|
|
97
|
+
logger.error(error_msg)
|
|
98
|
+
raise FileNotFoundError(error_msg)
|
|
83
99
|
|
|
84
100
|
file_ext = Path(video_path).suffix.lower()
|
|
85
101
|
if file_ext not in cls.SUPPORTED_VIDEOS:
|
|
86
|
-
|
|
102
|
+
error_msg = f"Unsupported video format: {file_ext}"
|
|
103
|
+
logger.error(error_msg)
|
|
104
|
+
raise ValueError(error_msg)
|
|
87
105
|
|
|
88
106
|
return cls._upload_to_catbox(video_path)
|
|
89
107
|
|
|
@@ -101,6 +119,7 @@ class MediaUploader:
|
|
|
101
119
|
Raises:
|
|
102
120
|
requests.RequestException: If upload fails
|
|
103
121
|
"""
|
|
122
|
+
logger.debug(f"Uploading to Catbox: {file_path}")
|
|
104
123
|
with open(file_path, 'rb') as f:
|
|
105
124
|
files = {'fileToUpload': f}
|
|
106
125
|
data = {'reqtype': 'fileupload'}
|
|
@@ -110,6 +129,9 @@ class MediaUploader:
|
|
|
110
129
|
url = response.text.strip()
|
|
111
130
|
|
|
112
131
|
if not url or url.startswith('error'):
|
|
113
|
-
|
|
132
|
+
error_msg = f"Catbox upload failed: {url}"
|
|
133
|
+
logger.error(error_msg)
|
|
134
|
+
raise requests.RequestException(error_msg)
|
|
114
135
|
|
|
136
|
+
logger.info(f"Successfully uploaded to Catbox: {url}")
|
|
115
137
|
return url
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
1
3
|
from ..api.base import BaseInstagramClient
|
|
2
4
|
|
|
5
|
+
logger = logging.getLogger(__name__)
|
|
6
|
+
|
|
3
7
|
|
|
4
8
|
class CarouselPoster(BaseInstagramClient):
|
|
5
9
|
"""Post carousels (multiple images/videos) to Instagram."""
|
|
@@ -7,35 +11,40 @@ class CarouselPoster(BaseInstagramClient):
|
|
|
7
11
|
MIN_ITEMS = 2
|
|
8
12
|
MAX_ITEMS = 10
|
|
9
13
|
|
|
10
|
-
def upload(self,
|
|
14
|
+
def upload(self, media_items: list[dict], caption: str = "") -> str:
|
|
11
15
|
"""
|
|
12
16
|
Upload a carousel to Instagram.
|
|
13
17
|
|
|
14
18
|
Args:
|
|
15
|
-
|
|
16
|
-
|
|
19
|
+
media_items: List of dicts with 'media' (local file path or publicly accessible URL)
|
|
20
|
+
and 'type' ('IMAGE' or 'VIDEO') keys.
|
|
17
21
|
caption: Optional caption for the carousel.
|
|
18
22
|
|
|
19
23
|
Returns:
|
|
20
24
|
Container ID for publishing.
|
|
21
25
|
"""
|
|
22
|
-
|
|
23
|
-
|
|
26
|
+
logger.info(f"Uploading carousel with {len(media_items)} items to Instagram")
|
|
27
|
+
if not self.MIN_ITEMS <= len(media_items) <= self.MAX_ITEMS:
|
|
28
|
+
error_msg = f"Carousel must have between {self.MIN_ITEMS} and {self.MAX_ITEMS} items"
|
|
29
|
+
logger.error(error_msg)
|
|
30
|
+
raise ValueError(error_msg)
|
|
24
31
|
|
|
25
|
-
children_ids = [self._create_child(media) for media in
|
|
32
|
+
children_ids = [self._create_child(media) for media in media_items]
|
|
26
33
|
return self._create_container({
|
|
27
34
|
"media_type": "CAROUSEL",
|
|
28
35
|
"children": ",".join(children_ids),
|
|
29
36
|
"caption": caption,
|
|
30
37
|
})
|
|
31
38
|
|
|
32
|
-
def post(self,
|
|
39
|
+
def post(self, media_items: list[dict], caption: str = "") -> dict:
|
|
33
40
|
"""Upload and publish a carousel."""
|
|
34
|
-
|
|
41
|
+
logger.info("Publishing carousel post")
|
|
42
|
+
return self.publish(self.upload(media_items, caption))
|
|
35
43
|
|
|
36
44
|
def _create_child(self, media: dict) -> str:
|
|
37
45
|
"""Create a child container for a carousel item."""
|
|
38
46
|
media_type = media.get("type", "IMAGE").upper()
|
|
47
|
+
logger.debug(f"Creating carousel child item of type {media_type}")
|
|
39
48
|
params = {"is_carousel_item": "true"}
|
|
40
49
|
|
|
41
50
|
if media_type == "IMAGE":
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
1
3
|
from ..api.base import BaseInstagramClient
|
|
2
4
|
|
|
5
|
+
logger = logging.getLogger(__name__)
|
|
6
|
+
|
|
3
7
|
|
|
4
8
|
class ImagePoster(BaseInstagramClient):
|
|
5
9
|
"""Post single images to Instagram."""
|
|
@@ -15,8 +19,11 @@ class ImagePoster(BaseInstagramClient):
|
|
|
15
19
|
Returns:
|
|
16
20
|
Container ID for publishing.
|
|
17
21
|
"""
|
|
22
|
+
image_display = image[:50] + "..." if len(image) > 50 else image
|
|
23
|
+
logger.info(f"Uploading image to Instagram: {image_display}")
|
|
18
24
|
return self._create_container({"image_url": image, "caption": caption})
|
|
19
25
|
|
|
20
26
|
def post(self, image: str, caption: str = "") -> dict:
|
|
21
27
|
"""Upload and publish a single image."""
|
|
28
|
+
logger.info("Publishing image post")
|
|
22
29
|
return self.publish(self.upload(image, caption))
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import logging
|
|
1
2
|
from typing import Optional
|
|
2
3
|
|
|
3
4
|
from ..api.base import BaseInstagramClient
|
|
4
5
|
|
|
6
|
+
logger = logging.getLogger(__name__)
|
|
7
|
+
|
|
5
8
|
|
|
6
9
|
class ReelPoster(BaseInstagramClient):
|
|
7
10
|
"""Post reels to Instagram."""
|
|
@@ -27,6 +30,8 @@ class ReelPoster(BaseInstagramClient):
|
|
|
27
30
|
Returns:
|
|
28
31
|
Container ID for publishing.
|
|
29
32
|
"""
|
|
33
|
+
video_display = video[:50] + "..." if len(video) > 50 else video
|
|
34
|
+
logger.info(f"Uploading reel to Instagram: {video_display}")
|
|
30
35
|
params = {
|
|
31
36
|
"media_type": "REELS",
|
|
32
37
|
"video_url": video,
|
|
@@ -49,4 +54,5 @@ class ReelPoster(BaseInstagramClient):
|
|
|
49
54
|
timeout: int = 120,
|
|
50
55
|
) -> dict:
|
|
51
56
|
"""Upload and publish a reel."""
|
|
57
|
+
logger.info("Publishing reel post")
|
|
52
58
|
return self.publish(self.upload(video, caption, cover, share_to_feed, timeout))
|
|
@@ -99,7 +99,7 @@ class TestInstagramPoster:
|
|
|
99
99
|
with patch.object(client._reel, 'post', return_value={'media_id': 'post_cover'}):
|
|
100
100
|
result = client.post_reel(
|
|
101
101
|
temp_video_file,
|
|
102
|
-
|
|
102
|
+
cover=temp_image_file,
|
|
103
103
|
share_to_feed=True,
|
|
104
104
|
timeout=120
|
|
105
105
|
)
|
|
@@ -114,7 +114,7 @@ class TestInstagramPoster:
|
|
|
114
114
|
container_id = client.upload_reel(
|
|
115
115
|
'https://example.com/video.mp4',
|
|
116
116
|
caption='Test reel',
|
|
117
|
-
|
|
117
|
+
cover='https://example.com/cover.jpg',
|
|
118
118
|
share_to_feed=False,
|
|
119
119
|
timeout=60
|
|
120
120
|
)
|
|
@@ -125,54 +125,54 @@ class TestInstagramPoster:
|
|
|
125
125
|
def test_upload_carousel(self):
|
|
126
126
|
"""Test uploading a carousel."""
|
|
127
127
|
client = InstagramPoster(access_token='token', ig_user_id='user')
|
|
128
|
-
|
|
128
|
+
media_items = [
|
|
129
129
|
{'media': 'https://example.com/img1.jpg', 'type': 'IMAGE'},
|
|
130
130
|
{'media': 'https://example.com/img2.jpg', 'type': 'IMAGE'},
|
|
131
131
|
]
|
|
132
132
|
|
|
133
133
|
with patch.object(client._carousel, 'upload', return_value='carousel_123'):
|
|
134
|
-
container_id = client.upload_carousel(
|
|
134
|
+
container_id = client.upload_carousel(media_items)
|
|
135
135
|
|
|
136
136
|
assert container_id == 'carousel_123'
|
|
137
137
|
|
|
138
138
|
def test_post_carousel_urls(self):
|
|
139
139
|
"""Test posting a carousel from URLs."""
|
|
140
140
|
client = InstagramPoster(access_token='token', ig_user_id='user')
|
|
141
|
-
|
|
141
|
+
media_items = [
|
|
142
142
|
{'media': 'https://example.com/img1.jpg', 'type': 'IMAGE'},
|
|
143
143
|
{'media': 'https://example.com/img2.jpg', 'type': 'IMAGE'},
|
|
144
144
|
]
|
|
145
145
|
|
|
146
146
|
with patch.object(client._carousel, 'post', return_value={'media_id': 'carousel_post'}):
|
|
147
|
-
result = client.post_carousel(
|
|
147
|
+
result = client.post_carousel(media_items)
|
|
148
148
|
|
|
149
149
|
assert result == {'media_id': 'carousel_post'}
|
|
150
150
|
|
|
151
151
|
def test_post_carousel_local_files(self, temp_image_file):
|
|
152
152
|
"""Test posting a carousel with local files (auto-upload)."""
|
|
153
153
|
client = InstagramPoster(access_token='token', ig_user_id='user')
|
|
154
|
-
|
|
154
|
+
media_items = [
|
|
155
155
|
{'media': temp_image_file, 'type': 'IMAGE'},
|
|
156
156
|
{'media': 'https://example.com/img2.jpg', 'type': 'IMAGE'},
|
|
157
157
|
]
|
|
158
158
|
|
|
159
159
|
with patch('instapost.client.MediaUploader.upload', return_value='https://catbox.moe/img1.jpg'):
|
|
160
160
|
with patch.object(client._carousel, 'post', return_value={'media_id': 'carousel_post_2'}):
|
|
161
|
-
result = client.post_carousel(
|
|
161
|
+
result = client.post_carousel(media_items)
|
|
162
162
|
|
|
163
163
|
assert result == {'media_id': 'carousel_post_2'}
|
|
164
164
|
|
|
165
165
|
def test_post_carousel_mixed_local_and_urls(self, temp_image_file):
|
|
166
166
|
"""Test posting carousel with mix of local files and URLs."""
|
|
167
167
|
client = InstagramPoster(access_token='token', ig_user_id='user')
|
|
168
|
-
|
|
168
|
+
media_items = [
|
|
169
169
|
{'media': temp_image_file, 'type': 'IMAGE'},
|
|
170
170
|
{'media': 'https://example.com/video.mp4', 'type': 'VIDEO'},
|
|
171
171
|
]
|
|
172
172
|
|
|
173
173
|
with patch('instapost.client.MediaUploader.upload', return_value='https://catbox.moe/uploaded.jpg'):
|
|
174
174
|
with patch.object(client._carousel, 'post', return_value={'media_id': 'mixed_carousel'}):
|
|
175
|
-
result = client.post_carousel(
|
|
175
|
+
result = client.post_carousel(media_items, caption='Mixed carousel')
|
|
176
176
|
|
|
177
177
|
assert result == {'media_id': 'mixed_carousel'}
|
|
178
178
|
|
|
@@ -94,46 +94,46 @@ class TestCarouselPoster:
|
|
|
94
94
|
def test_upload_carousel_valid(self):
|
|
95
95
|
"""Test uploading a valid carousel."""
|
|
96
96
|
poster = CarouselPoster(access_token='token', ig_user_id='user')
|
|
97
|
-
|
|
97
|
+
media_items = [
|
|
98
98
|
{'media': 'https://example.com/img1.jpg', 'type': 'IMAGE'},
|
|
99
99
|
{'media': 'https://example.com/img2.jpg', 'type': 'IMAGE'},
|
|
100
100
|
]
|
|
101
101
|
|
|
102
102
|
with patch.object(poster, '_create_child', side_effect=['child_1', 'child_2']):
|
|
103
103
|
with patch.object(poster, '_create_container', return_value='carousel_123'):
|
|
104
|
-
container_id = poster.upload(
|
|
104
|
+
container_id = poster.upload(media_items, caption='Carousel')
|
|
105
105
|
|
|
106
106
|
assert container_id == 'carousel_123'
|
|
107
107
|
|
|
108
108
|
def test_upload_carousel_too_few_items(self):
|
|
109
109
|
"""Test carousel with too few items."""
|
|
110
110
|
poster = CarouselPoster(access_token='token', ig_user_id='user')
|
|
111
|
-
|
|
111
|
+
media_items = [
|
|
112
112
|
{'media': 'https://example.com/img1.jpg', 'type': 'IMAGE'},
|
|
113
113
|
]
|
|
114
114
|
|
|
115
115
|
with pytest.raises(ValueError, match='between'):
|
|
116
|
-
poster.upload(
|
|
116
|
+
poster.upload(media_items)
|
|
117
117
|
|
|
118
118
|
def test_upload_carousel_too_many_items(self):
|
|
119
119
|
"""Test carousel with too many items."""
|
|
120
120
|
poster = CarouselPoster(access_token='token', ig_user_id='user')
|
|
121
|
-
|
|
121
|
+
media_items = [{'media': f'https://example.com/img{i}.jpg', 'type': 'IMAGE'} for i in range(12)]
|
|
122
122
|
|
|
123
123
|
with pytest.raises(ValueError, match='between'):
|
|
124
|
-
poster.upload(
|
|
124
|
+
poster.upload(media_items)
|
|
125
125
|
|
|
126
126
|
def test_upload_carousel_mixed_media(self):
|
|
127
127
|
"""Test carousel with mixed image and video."""
|
|
128
128
|
poster = CarouselPoster(access_token='token', ig_user_id='user')
|
|
129
|
-
|
|
129
|
+
media_items = [
|
|
130
130
|
{'media': 'https://example.com/img1.jpg', 'type': 'IMAGE'},
|
|
131
131
|
{'media': 'https://example.com/video1.mp4', 'type': 'VIDEO'},
|
|
132
132
|
]
|
|
133
133
|
|
|
134
134
|
with patch.object(poster, '_create_child', side_effect=['child_1', 'child_2']):
|
|
135
135
|
with patch.object(poster, '_create_container', return_value='carousel_456'):
|
|
136
|
-
container_id = poster.upload(
|
|
136
|
+
container_id = poster.upload(media_items)
|
|
137
137
|
|
|
138
138
|
assert container_id == 'carousel_456'
|
|
139
139
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|