Skip to content

Commit 8a0974b

Browse files
committed
Support stickers for story [#18] Added StorySticker and stickers param for upload_to_story methods
1 parent 8773e8e commit 8a0974b

File tree

6 files changed

+80
-3
lines changed

6 files changed

+80
-3
lines changed

instagrapi/extractors.py

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ def extract_story_v1(data):
242242
]
243243
story["locations"] = []
244244
story["hashtags"] = []
245+
story["stickers"] = []
245246
story["links"] = []
246247
for cta in story.get("story_cta", []):
247248
for link in cta.get("links", []):

instagrapi/mixins/photo.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
PhotoConfigureStoryError, PhotoNotUpload)
1515
from instagrapi.extractors import extract_media_v1
1616
from instagrapi.types import (Location, Media, Story, StoryHashtag, StoryLink,
17-
StoryLocation, StoryMention, Usertag)
17+
StoryLocation, StoryMention, StorySticker, Usertag)
1818
from instagrapi.utils import dumps
1919

2020
try:
@@ -266,6 +266,7 @@ def photo_upload_to_story(
266266
locations: List[StoryLocation] = [],
267267
links: List[StoryLink] = [],
268268
hashtags: List[StoryHashtag] = [],
269+
stickers: List[StorySticker] = [],
269270
) -> Story:
270271
"""
271272
Upload photo as a story and configure it
@@ -286,6 +287,8 @@ def photo_upload_to_story(
286287
URLs for Swipe Up
287288
hashtags: List[StoryHashtag], optional
288289
List of hashtags to be tagged on this upload, default is empty list.
290+
stickers: List[StorySticker], optional
291+
List of stickers to be tagged on this upload, default is empty list.
289292
290293
Returns
291294
-------
@@ -306,6 +309,7 @@ def photo_upload_to_story(
306309
locations,
307310
links,
308311
hashtags,
312+
stickers,
309313
):
310314
media = self.last_json.get("media")
311315
self.expose()
@@ -314,6 +318,7 @@ def photo_upload_to_story(
314318
mentions=mentions,
315319
hashtags=hashtags,
316320
locations=locations,
321+
stickers=stickers,
317322
**extract_media_v1(media).dict()
318323
)
319324
raise PhotoConfigureStoryError(
@@ -330,6 +335,7 @@ def photo_configure_to_story(
330335
locations: List[StoryLocation] = [],
331336
links: List[StoryLink] = [],
332337
hashtags: List[StoryHashtag] = [],
338+
stickers: List[StorySticker] = [],
333339
) -> Dict:
334340
"""
335341
Post configure photo
@@ -352,6 +358,8 @@ def photo_configure_to_story(
352358
URLs for Swipe Up
353359
hashtags: List[StoryHashtag], optional
354360
List of hashtags to be tagged on this upload, default is empty list.
361+
stickers: List[StorySticker], optional
362+
List of stickers to be tagged on this upload, default is empty list.
355363
356364
Returns
357365
-------
@@ -391,6 +399,7 @@ def photo_configure_to_story(
391399
links = [link.dict() for link in links]
392400
data["story_cta"] = dumps([{"links": links}])
393401
tap_models = []
402+
static_models = []
394403
if mentions:
395404
reel_mentions = [
396405
{
@@ -444,7 +453,24 @@ def photo_configure_to_story(
444453
"tap_state_str_id": "location_sticker_vibrant"
445454
}
446455
tap_models.append(item)
456+
if stickers:
457+
for sticker in stickers:
458+
str_id = sticker.id # "gif_Igjf05J559JWuef4N5"
459+
static_models.append({
460+
"x": sticker.x,
461+
"y": sticker.y,
462+
"z": sticker.z,
463+
"width": sticker.width,
464+
"height": sticker.height,
465+
"rotation": sticker.rotation,
466+
"str_id": str_id,
467+
"sticker_type": sticker.type,
468+
})
469+
story_sticker_ids.append(str_id)
470+
if sticker.type == "gif":
471+
data["has_animated_sticker"] = "1"
447472
data["tap_models"] = dumps(tap_models)
473+
data["static_models"] = dumps(static_models)
448474
data["story_sticker_ids"] = dumps(story_sticker_ids)
449475
return self.private_request(
450476
"media/configure_to_story/", self.with_default_data(data)

instagrapi/mixins/video.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
VideoNotUpload)
1414
from instagrapi.extractors import extract_media_v1
1515
from instagrapi.types import (Location, Media, Story, StoryHashtag, StoryLink,
16-
StoryLocation, StoryMention, Usertag)
16+
StoryLocation, StoryMention, StorySticker, Usertag)
1717
from instagrapi.utils import dumps
1818

1919

@@ -320,6 +320,7 @@ def video_upload_to_story(
320320
locations: List[StoryLocation] = [],
321321
links: List[StoryLink] = [],
322322
hashtags: List[StoryHashtag] = [],
323+
stickers: List[StorySticker] = [],
323324
) -> Story:
324325
"""
325326
Upload video as a story and configure it
@@ -340,6 +341,8 @@ def video_upload_to_story(
340341
URLs for Swipe Up
341342
hashtags: List[StoryHashtag], optional
342343
List of hashtags to be tagged on this upload, default is empty list.
344+
stickers: List[StorySticker], optional
345+
List of stickers to be tagged on this upload, default is empty list.
343346
344347
Returns
345348
-------
@@ -367,6 +370,7 @@ def video_upload_to_story(
367370
locations,
368371
links,
369372
hashtags,
373+
stickers,
370374
)
371375
except Exception as e:
372376
if "Transcode not finished yet" in str(e):
@@ -385,6 +389,7 @@ def video_upload_to_story(
385389
mentions=mentions,
386390
hashtags=hashtags,
387391
locations=locations,
392+
stickers=stickers,
388393
**extract_media_v1(media).dict()
389394
)
390395
raise VideoConfigureStoryError(
@@ -403,6 +408,7 @@ def video_configure_to_story(
403408
locations: List[StoryLocation] = [],
404409
links: List[StoryLink] = [],
405410
hashtags: List[StoryHashtag] = [],
411+
stickers: List[StorySticker] = [],
406412
) -> Dict:
407413
"""
408414
Story Configure for Photo
@@ -429,6 +435,8 @@ def video_configure_to_story(
429435
URLs for Swipe Up
430436
hashtags: List[StoryHashtag], optional
431437
List of hashtags to be tagged on this upload, default is empty list.
438+
stickers: List[StorySticker], optional
439+
List of stickers to be tagged on this upload, default is empty list.
432440
433441
Returns
434442
-------
@@ -483,6 +491,7 @@ def video_configure_to_story(
483491
links = [link.dict() for link in links]
484492
data["story_cta"] = dumps([{"links": links}])
485493
tap_models = []
494+
static_models = []
486495
if mentions:
487496
reel_mentions = []
488497
text_metadata = []
@@ -550,7 +559,24 @@ def video_configure_to_story(
550559
"tap_state_str_id": "location_sticker_vibrant"
551560
}
552561
tap_models.append(item)
562+
if stickers:
563+
for sticker in stickers:
564+
str_id = sticker.id # "gif_Igjf05J559JWuef4N5"
565+
static_models.append({
566+
"x": sticker.x,
567+
"y": sticker.y,
568+
"z": sticker.z,
569+
"width": sticker.width,
570+
"height": sticker.height,
571+
"rotation": sticker.rotation,
572+
"str_id": str_id,
573+
"sticker_type": sticker.type,
574+
})
575+
story_sticker_ids.append(str_id)
576+
if sticker.type == "gif":
577+
data["has_animated_sticker"] = "1"
553578
data["tap_models"] = dumps(tap_models)
579+
data["static_models"] = dumps(static_models)
554580
data["story_sticker_ids"] = dumps(story_sticker_ids)
555581
return self.private_request(
556582
"media/configure_to_story/?video=1", self.with_default_data(data)

instagrapi/types.py

+12
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ class StoryLocation(BaseModel):
159159
height: Optional[float]
160160

161161

162+
class StorySticker(BaseModel):
163+
id: str
164+
type: Optional[str] = 'gif'
165+
x: float
166+
y: float
167+
z: Optional[int] = 1000005
168+
width: float
169+
height: float
170+
rotation: Optional[float] = 0.0
171+
172+
162173
class StoryBuild(BaseModel):
163174
mentions: List[StoryMention]
164175
path: FilePath
@@ -183,6 +194,7 @@ class Story(BaseModel):
183194
links: List[StoryLink]
184195
hashtags: List[StoryHashtag]
185196
locations: List[StoryLocation]
197+
stickers: List[StorySticker]
186198

187199

188200
class DirectMessage(BaseModel):

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
setup(
3131
name='instagrapi',
32-
version='1.4.3',
32+
version='1.4.4',
3333
author='Mikhail Andreev',
3434
author_email='[email protected]',
3535
license='MIT',

tests.py

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
StoryLocation,
2424
StoryMention,
2525
StoryHashtag,
26+
StorySticker,
2627
User,
2728
UserShort,
2829
Usertag
@@ -1127,6 +1128,16 @@ def test_upload_photo_story(self):
11271128
)
11281129
)
11291130
]
1131+
stickers = [
1132+
StorySticker(
1133+
id="Igjf05J559JWuef4N5",
1134+
type="gif",
1135+
x=0.5,
1136+
y=0.5,
1137+
width=0.4,
1138+
height=0.08
1139+
)
1140+
]
11301141
try:
11311142
story = self.api.photo_upload_to_story(
11321143
path,
@@ -1135,6 +1146,7 @@ def test_upload_photo_story(self):
11351146
links=links,
11361147
hashtags=hashtags,
11371148
locations=locations,
1149+
stickers=stickers
11381150
)
11391151
self.assertIsInstance(story, Story)
11401152
self.assertTrue(story)

0 commit comments

Comments
 (0)