Skip to content

Commit

Permalink
Pyrofork: Add media_areas field to Story
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <wulan17@nusantararom.org>
  • Loading branch information
wulan17 committed Dec 24, 2023
1 parent 0c74d30 commit fa83697
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 2 deletions.
3 changes: 3 additions & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ def get_title_list(s: str) -> list:
StorySkipped
StoriesPrivacyRules
StoryViews
MediaArea
MediaAreaChannelPost
MediaAreaCoordinates
""",
pyromod="""
Pyromod
Expand Down
5 changes: 4 additions & 1 deletion pyrogram/types/messages_and_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
from .game import Game
from .giveaway import Giveaway
from .location import Location
from .media_area import MediaArea
from .media_area_channel_post import MediaAreaChannelPost
from .media_area_coordinates import MediaAreaCoordinates
from .message import Message
from .message_entity import MessageEntity
from .photo import Photo
Expand Down Expand Up @@ -53,7 +56,7 @@
from .exported_story_link import ExportedStoryLink

__all__ = [
"Animation", "Audio", "Contact", "Document", "Game", "Giveaway", "Location", "Message", "MessageEntity", "Photo", "Thumbnail",
"Animation", "Audio", "Contact", "Document", "Game", "Giveaway", "Location", "MediaArea", "MediaAreaChannelPost", "MediaAreaCoordinates", "Message", "MessageEntity", "Photo", "Thumbnail",
"StrippedThumbnail", "Poll", "PollOption", "Sticker", "StickerSet", "Venue", "Video", "VideoNote", "Voice", "WebPage", "WebPageEmpty", "WebPagePreview", "Dice",
"Reaction", "WebAppData", "MessageReactions", "MessageStory", "Story", "StoryDeleted", "StorySkipped", "StoryViews", "StoryForwardHeader", "StoriesPrivacyRules", "ExportedStoryLink"
]
46 changes: 46 additions & 0 deletions pyrogram/types/messages_and_media/media_area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

import pyrogram

from pyrogram import raw, types
from ..object import Object


class MediaArea(Object):
"""Content of a media areas in story.
It should be one of:
- :obj:`~pyrogram.types.MediaAreaChannelPost`
"""

def __init__(
self,
coordinates: "types.MediaAreaCoordinates"
):
super().__init__()

self.coordinates = coordinates

async def _parse(
client: "pyrogram.Client",
media_area: "raw.base.MediaArea"
):
if isinstance(media_area, raw.types.MediaAreaChannelPost):
return await types.MediaAreaChannelPost._parse(client, media_area)
70 changes: 70 additions & 0 deletions pyrogram/types/messages_and_media/media_area_channel_post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

import pyrogram

from pyrogram import raw, types, utils

from .media_area import MediaArea

class MediaAreaChannelPost(MediaArea):
"""A channel post media area.
Parameters:
coordinates (:obj:`~pyrogram.types.MediaAreaCoordinates`):
Media area coordinates.
chat (:obj:`~pyrogram.types.Chat`):
Media area width.
message_id (``int``):
Media area height.
"""

def __init__(
self,
coordinates: "types.MediaAreaCoordinates",
chat: "types.Chat",
message_id: int
):
super().__init__(coordinates=coordinates)

self.coordinates = coordinates
self.chat = chat
self.message_id = message_id

async def _parse(
client: "pyrogram.Client",
media_area: "raw.types.MediaAreaChannelPost"
):
channel_id = utils.get_channel_id(media_area.channel_id)
chat = types.Chat._parse_chat(
client,
(
await client.invoke(
raw.functions.channels.GetChannels(
id=[await client.resolve_peer(channel_id)]
)
)
)[0]
)
return MediaAreaChannelPost(
coordinates=types.MediaAreaCoordinates._parse(media_area.coordinates),
chat=chat,
message_id=media_area.msg_id
)
68 changes: 68 additions & 0 deletions pyrogram/types/messages_and_media/media_area_coordinates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from pyrogram import types, raw
from ..object import Object


class MediaAreaCoordinates(Object):
"""A coordinates of media area.
Parameters:
x (``float``):
X position of media area.
y (``float``):
Y position of media area.
width (``float``):
Media area width.
height (``float``):
Media area height.
rotation (``float``):
Media area rotation.
"""

def __init__(
self,
x: float,
y: float,
width: float,
height: float,
rotation: float
):
super().__init__()

self.x = x
self.y = y
self.width = width
self.height = height
self.rotation = rotation

def _parse(
media_area_cordinates: "raw.types.MediaAreaCoordinates"
) -> "MediaAreaCoordinates":
return MediaAreaCoordinates(
x=media_area_cordinates.x,
y=media_area_cordinates.y,
width=media_area_cordinates.w,
height=media_area_cordinates.h,
rotation=media_area_cordinates.rotation
)
14 changes: 13 additions & 1 deletion pyrogram/types/messages_and_media/story.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@ class Story(Object, Update):
denied_users (List of ``int``, *optional*):
List of user_id whos denied to view the story.
media_areas (List of :obj:`~pyrogram.types.MediaArea`, *optional*):
List of :obj:`~pyrogram.types.MediaArea` object in story.
"""

# TODO: Add Media Areas, fix Allowed Chats
# TODO: fix Allowed Chats

def __init__(
self,
Expand Down Expand Up @@ -130,6 +133,7 @@ def __init__(
forward_from: "types.StoryForwardHeader" = None,
allowed_users: List[int] = None,
denied_users: List[int] = None,
media_areas: List["types.MediaArea"] = None
#allowed_chats: List[int] = None,
#denied_chats: List[int] = None
):
Expand Down Expand Up @@ -158,6 +162,7 @@ def __init__(
self.forward_from = forward_from
self.allowed_users = allowed_users
self.denied_users = denied_users
self.media_areas = media_areas
#self.allowed_chats = allowed_chats
#self.denied_chats = denied_chats

Expand Down Expand Up @@ -241,6 +246,12 @@ async def _parse(
if stories.fwd_from is not None:
forward_from = await types.StoryForwardHeader._parse(client, stories.fwd_from)

if stories.media_areas is not None and len(stories.media_areas) > 0:
media_areas = [
await types.MediaArea._parse(client, media_area)
for media_area in stories.media_areas
]

return Story(
id=stories.id,
from_user=from_user,
Expand All @@ -267,6 +278,7 @@ async def _parse(
#denied_chats=denied_chats,
allowed_users=allowed_users,
denied_users=denied_users,
media_areas=media_areas,
client=client
)

Expand Down

0 comments on commit fa83697

Please sign in to comment.