Pyrofork: Refactor send_story method

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
wulan17 2024-01-04 19:24:39 +07:00
parent 48c7a5e2f9
commit f1b4d1ca49
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5

View file

@ -18,7 +18,7 @@
import os import os
import re import re
from typing import List, Union from typing import BinaryIO, List, Union
import pyrogram import pyrogram
from pyrogram import enums, raw, types, utils from pyrogram import enums, raw, types, utils
@ -36,9 +36,9 @@ class SendStory:
denied_users: List[int] = None, denied_users: List[int] = None,
#allowed_chats: List[int] = None, #allowed_chats: List[int] = None,
#denied_chats: List[int] = None, #denied_chats: List[int] = None,
animation: str = None, photo: Union[str, BinaryIO] = None,
photo: str = None, video: Union[str, BinaryIO] = None,
video: str = None, file_name: str = None,
pinned: bool = None, pinned: bool = None,
protect_content: bool = None, protect_content: bool = None,
caption: str = None, caption: str = None,
@ -60,13 +60,6 @@ class SendStory:
Unique identifier (int) or username (str) of the target channel. Unique identifier (int) or username (str) of the target channel.
You can also use channel public link in form of *t.me/<username>* (str). You can also use channel public link in form of *t.me/<username>* (str).
animation (``str`` | ``BinaryIO``, *optional*):
Animation to send.
Pass a file_id as string to send a animation that exists on the Telegram servers,
pass an HTTP URL as a string for Telegram to get a animation from the Internet,
pass a file path as string to upload a new animation that exists on your local machine, or
pass a binary file-like object with its attribute ".name" set for in-memory uploads.
photo (``str`` | ``BinaryIO``, *optional*): photo (``str`` | ``BinaryIO``, *optional*):
Photo to send. Photo to send.
Pass a file_id as string to send a photo that exists on the Telegram servers, Pass a file_id as string to send a photo that exists on the Telegram servers,
@ -81,6 +74,10 @@ class SendStory:
pass a file path as string to upload a new video that exists on your local machine, or pass a file path as string to upload a new video that exists on your local machine, or
pass a binary file-like object with its attribute ".name" set for in-memory uploads. pass a binary file-like object with its attribute ".name" set for in-memory uploads.
file_name (``str``, *optional*):
File name of the video sent.
Defaults to file's path basename.
privacy (:obj:`~pyrogram.enums.StoriesPrivacyRules`, *optional*): privacy (:obj:`~pyrogram.enums.StoriesPrivacyRules`, *optional*):
Story privacy. Story privacy.
Defaults to :obj:`~pyrogram.enums.StoriesPrivacyRules.PUBLIC` Defaults to :obj:`~pyrogram.enums.StoriesPrivacyRules.PUBLIC`
@ -148,45 +145,7 @@ class SendStory:
else: else:
privacy_rules = [types.StoriesPrivacyRules(type=enums.StoriesPrivacyRules.PUBLIC)] privacy_rules = [types.StoriesPrivacyRules(type=enums.StoriesPrivacyRules.PUBLIC)]
if animation: if photo:
if isinstance(animation, str):
if os.path.isfile(animation):
file = await self.save_file(animation)
media = raw.types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(animation) or "video/mp4",
file=file,
attributes=[
raw.types.DocumentAttributeVideo(
supports_streaming=True,
duration=0,
w=0,
h=0
),
raw.types.DocumentAttributeAnimated()
]
)
elif re.match("^https?://", animation):
media = raw.types.InputMediaDocumentExternal(
url=animation
)
else:
media = utils.get_input_media_from_file_id(animation, FileType.ANIMATION)
else:
file = await self.save_file(animation)
media = raw.types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(animation) or "video/mp4",
file=file,
attributes=[
raw.types.DocumentAttributeVideo(
supports_streaming=True,
duration=0,
w=0,
h=0
),
raw.types.DocumentAttributeAnimated()
]
)
elif photo:
if isinstance(photo, str): if isinstance(photo, str):
if os.path.isfile(photo): if os.path.isfile(photo):
file = await self.save_file(photo) file = await self.save_file(photo)
@ -225,11 +184,24 @@ class SendStory:
url=video url=video
) )
else: else:
media = utils.get_input_media_from_file_id(video, FileType.VIDEO) video = await self.download_media(video, in_memory=True)
file = await self.save_file(video)
media = raw.types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(file_name or video.name) or "video/mp4",
file=file,
attributes=[
raw.types.DocumentAttributeVideo(
supports_streaming=True,
duration=0,
w=0,
h=0
)
]
)
else: else:
file = await self.save_file(video) file = await self.save_file(video)
media = raw.types.InputMediaUploadedDocument( media = raw.types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(video) or "video/mp4", mime_type=self.guess_mime_type(file_name or video.name) or "video/mp4",
file=file, file=file,
attributes=[ attributes=[
raw.types.DocumentAttributeVideo( raw.types.DocumentAttributeVideo(