Skip to content

Commit

Permalink
Pyrofork: Add GiveawayResult and refactor Giveaway
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <wulan17@nusantararom.org>
  • Loading branch information
wulan17 committed Dec 26, 2023
1 parent c0779b5 commit fb985d4
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 15 deletions.
1 change: 1 addition & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ def get_title_list(s: str) -> list:
StickerSet
Game
Giveaway
GiveawayResult
MessageStory
WebPage
WebPageEmpty
Expand Down
3 changes: 3 additions & 0 deletions pyrogram/enums/message_media_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,8 @@ class MessageMediaType(AutoName):
GIVEAWAY = auto()
"Giveaway media"

GIVEAWAY_RESULT = auto()
"Giveaway result media"

STORY = auto()
"Forwarded story media"
3 changes: 2 additions & 1 deletion pyrogram/types/messages_and_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .document import Document
from .game import Game
from .giveaway import Giveaway
from .giveaway_result import GiveawayResult
from .location import Location
from .media_area import MediaArea
from .media_area_channel_post import MediaAreaChannelPost
Expand Down Expand Up @@ -56,7 +57,7 @@
from .exported_story_link import ExportedStoryLink

__all__ = [
"Animation", "Audio", "Contact", "Document", "Game", "Giveaway", "Location", "MediaArea", "MediaAreaChannelPost", "MediaAreaCoordinates", "Message", "MessageEntity", "Photo", "Thumbnail",
"Animation", "Audio", "Contact", "Document", "Game", "Giveaway", "GiveawayResult", "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"
]
28 changes: 14 additions & 14 deletions pyrogram/types/messages_and_media/giveaway.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Giveaway(Object):
"""A giveaway.
Parameters:
channels (List of :obj:`~pyrogram.types.Chat`):
chats (List of :obj:`~pyrogram.types.Chat`):
List of channel(s) which host the giveaway.
quantity (``int``):
Expand All @@ -45,7 +45,7 @@ class Giveaway(Object):
new_subscribers (``bool``):
True, if the giveaway only for new subscribers.
countries_iso2 (List of ``str``, *optional*):
allowed_countries (List of ``str``, *optional*):
List of ISO country codes which eligible to join the giveaway.
private_channel_ids (List of ``int``, *optional*):
Expand All @@ -56,51 +56,51 @@ def __init__(
self,
*,
client: "pyrogram.Client" = None,
channels: List["types.Chat"],
chats: List["types.Chat"],
quantity: int,
months: int,
expire_date: datetime,
new_subscribers : bool,
countries_iso2: List[str] = None,
allowed_countries: List[str] = None,
private_channel_ids: List[int] = None
):
super().__init__(client)

self.channels = channels
self.chats = chats
self.quantity = quantity
self.months = months
self.expire_date = expire_date
self.new_subscribers = new_subscribers
self.countries_iso2 = countries_iso2
self.allowed_countries = allowed_countries
self.private_channel_ids = private_channel_ids

@staticmethod
async def _parse(client, message: "raw.types.Message") -> "Giveaway":
giveaway: "raw.types.MessageMediaGiveaway" = message.media
channels = []
chats = []
private_ids = []
for raw_channel_id in giveaway.channels:
channel_id = utils.get_channel_id(raw_channel_id)
for raw_chat_id in giveaway.channels:
chat_id = utils.get_channel_id(raw_chat_id)
try:
chat = await client.invoke(
raw.functions.channels.GetChannels(
id=[await client.resolve_peer(channel_id)]
id=[await client.resolve_peer(chat_id)]
)
)
except FloodWait as e:
await asyncio.sleep(e.value)
except Exception:
private_ids.append(channel_id)
private_ids.append(chat_id)
else:
channels.append(types.Chat._parse_chat(client, chat.chats[0]))
chats.append(types.Chat._parse_chat(client, chat.chats[0]))

return Giveaway(
channels=channels,
chats=chats,
quantity=giveaway.quantity,
months=giveaway.months,
expire_date=utils.timestamp_to_datetime(giveaway.until_date),
new_subscribers=giveaway.only_new_subscribers,
countries_iso2=giveaway.countries_iso2 if len(giveaway.countries_iso2) > 0 else None,
allowed_countries=giveaway.countries_iso2 if len(giveaway.countries_iso2) > 0 else None,
private_channel_ids=private_ids if len(private_ids) > 0 else None,
client=client
)
111 changes: 111 additions & 0 deletions pyrogram/types/messages_and_media/giveaway_result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Pyrofork - Telegram MTProto API Client Library for Python
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/Mayuri-Chan>
#
# This file is part of Pyrofork.
#
# Pyrofork 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.
#
# Pyrofork 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 Pyrofork. If not, see <http://www.gnu.org/licenses/>.

import pyrogram

from datetime import datetime
from pyrogram import raw, types, utils
from ..object import Object
from typing import List


class GiveawayResult(Object):
"""A giveaway result.
Parameters:
chat (:obj:`~pyrogram.types.Chat`):
Channel which host the giveaway.
giveaway_message (:obj:`~pyrogram.types.Message`):
The original giveaway message.
quantity (``int``):
Quantity of the giveaway prize.
unclaimed_quantity (``int``):
Quantity of unclaimed giveaway prize.
winners (List of :obj:`~pyrogram.types.User`):
The giveaway winners.
months (``int``):
How long the telegram premium last (in month).
expire_date (:py:obj:`~datetime.datetime`):
Date the giveaway winner(s) choosen.
new_subscribers (``bool``, *optional*):
True, if the giveaway only for new subscribers.
is_refunded (``bool``, *optional*):
True, if the giveaway was refunded.
"""

def __init__(
self,
*,
client: "pyrogram.Client" = None,
chat: "types.Chat",
giveaway_message: "types.Message",
quantity: int,
unclaimed_quantity: int,
winners: List["types.User"],
months: int,
expire_date: datetime,
new_subscribers : bool,
is_refunded: bool = None
):
super().__init__(client)

self.chat = chat
self.giveaway_message = giveaway_message
self.quantity = quantity
self.unclaimed_quantity = unclaimed_quantity
self.winners = winners
self.months = months
self.expire_date = expire_date
self.new_subscribers = new_subscribers
self.is_refunded = is_refunded

@staticmethod
async def _parse(client, message: "raw.types.Message") -> "GiveawayResult":
giveaway_result: "raw.types.MessageMediaGiveawayResults" = message.media
chat_id = utils.get_channel_id(giveaway_result.channel_id)
chat = await client.invoke(
raw.functions.channels.GetChannels(
id=[await client.resolve_peer(chat_id)]
)
)
chat = types.Chat._parse_chat(client, chat.chats[0])
giveaway_message = await client.get_messages(chat_id, giveaway_result.launch_msg_id)
winners = []
for winner in giveaway_result.winners:
winners.append(await client.get_users(winner))

return GiveawayResult(
chat=chat,
giveaway_message=giveaway_message,
quantity=giveaway_result.winners_count,
unclaimed_quantity=giveaway_result.unclaimed_count,
winners=winners,
months=giveaway_result.months,
expire_date=utils.timestamp_to_datetime(giveaway_result.until_date),
new_subscribers=giveaway_result.only_new_subscribers,
is_refunded=giveaway_result.refunded,
client=client
)
10 changes: 10 additions & 0 deletions pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ class Message(Object, Update):
giveaway (:obj:`~pyrogram.types.Giveaway`, *optional*):
Message is a giveaway, information about the giveaway.
giveaway_result (:obj:`~pyrogram.types.GiveawayResult`, *optional*):
Message is a giveaway result, information about the giveaway result.
story (:obj:`~pyrogram.types.MessageStory` | :obj:`~pyrogram.types.Story`, *optional*):
Message is a forwarded story, information about the forwarded story.
Expand Down Expand Up @@ -411,6 +414,7 @@ def __init__(
animation: "types.Animation" = None,
game: "types.Game" = None,
giveaway: "types.Giveaway" = None,
giveaway_result: "types.GiveawayResult" = None,
story: Union["types.MessageStory", "types.Story"] = None,
video: "types.Video" = None,
voice: "types.Voice" = None,
Expand Down Expand Up @@ -506,6 +510,7 @@ def __init__(
self.animation = animation
self.game = game
self.giveaway = giveaway
self.giveaway_result = giveaway_result
self.story = story
self.video = video
self.voice = voice
Expand Down Expand Up @@ -849,6 +854,7 @@ async def _parse(
venue = None
game = None
giveaway = None
giveaway_result = None
story = None
audio = None
voice = None
Expand Down Expand Up @@ -885,6 +891,9 @@ async def _parse(
elif isinstance(media, raw.types.MessageMediaGiveaway):
giveaway = await types.Giveaway._parse(client, message)
media_type = enums.MessageMediaType.GIVEAWAY
elif isinstance(media, raw.types.MessageMediaGiveawayResults):
giveaway_result = await types.GiveawayResult._parse(client, message)
media_type = enums.MessageMediaType.GIVEAWAY_RESULT
elif isinstance(media, raw.types.MessageMediaStory):
story = await types.MessageStory._parse(client, media)
media_type = enums.MessageMediaType.STORY
Expand Down Expand Up @@ -1018,6 +1027,7 @@ async def _parse(
animation=animation,
game=game,
giveaway=giveaway,
giveaway_result=giveaway_result,
story=story,
video=video,
video_note=video_note,
Expand Down

0 comments on commit fb985d4

Please sign in to comment.