Pyrofork: Add GiveawayResult and refactor Giveaway

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
wulan17 2023-12-26 14:50:33 +07:00
parent c0779b52ff
commit fb985d4d30
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5
6 changed files with 141 additions and 15 deletions

View file

@ -464,6 +464,7 @@ def pyrogram_api():
StickerSet StickerSet
Game Game
Giveaway Giveaway
GiveawayResult
MessageStory MessageStory
WebPage WebPage
WebPageEmpty WebPageEmpty

View file

@ -72,5 +72,8 @@ class MessageMediaType(AutoName):
GIVEAWAY = auto() GIVEAWAY = auto()
"Giveaway media" "Giveaway media"
GIVEAWAY_RESULT = auto()
"Giveaway result media"
STORY = auto() STORY = auto()
"Forwarded story media" "Forwarded story media"

View file

@ -23,6 +23,7 @@ from .dice import Dice
from .document import Document from .document import Document
from .game import Game from .game import Game
from .giveaway import Giveaway from .giveaway import Giveaway
from .giveaway_result import GiveawayResult
from .location import Location from .location import Location
from .media_area import MediaArea from .media_area import MediaArea
from .media_area_channel_post import MediaAreaChannelPost from .media_area_channel_post import MediaAreaChannelPost
@ -56,7 +57,7 @@ from .story_views import StoryViews
from .exported_story_link import ExportedStoryLink from .exported_story_link import ExportedStoryLink
__all__ = [ __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", "StrippedThumbnail", "Poll", "PollOption", "Sticker", "StickerSet", "Venue", "Video", "VideoNote", "Voice", "WebPage", "WebPageEmpty", "WebPagePreview", "Dice",
"Reaction", "WebAppData", "MessageReactions", "MessageStory", "Story", "StoryDeleted", "StorySkipped", "StoryViews", "StoryForwardHeader", "StoriesPrivacyRules", "ExportedStoryLink" "Reaction", "WebAppData", "MessageReactions", "MessageStory", "Story", "StoryDeleted", "StorySkipped", "StoryViews", "StoryForwardHeader", "StoriesPrivacyRules", "ExportedStoryLink"
] ]

View file

@ -30,7 +30,7 @@ class Giveaway(Object):
"""A giveaway. """A giveaway.
Parameters: Parameters:
channels (List of :obj:`~pyrogram.types.Chat`): chats (List of :obj:`~pyrogram.types.Chat`):
List of channel(s) which host the giveaway. List of channel(s) which host the giveaway.
quantity (``int``): quantity (``int``):
@ -45,7 +45,7 @@ class Giveaway(Object):
new_subscribers (``bool``): new_subscribers (``bool``):
True, if the giveaway only for new subscribers. 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. List of ISO country codes which eligible to join the giveaway.
private_channel_ids (List of ``int``, *optional*): private_channel_ids (List of ``int``, *optional*):
@ -56,51 +56,51 @@ class Giveaway(Object):
self, self,
*, *,
client: "pyrogram.Client" = None, client: "pyrogram.Client" = None,
channels: List["types.Chat"], chats: List["types.Chat"],
quantity: int, quantity: int,
months: int, months: int,
expire_date: datetime, expire_date: datetime,
new_subscribers : bool, new_subscribers : bool,
countries_iso2: List[str] = None, allowed_countries: List[str] = None,
private_channel_ids: List[int] = None private_channel_ids: List[int] = None
): ):
super().__init__(client) super().__init__(client)
self.channels = channels self.chats = chats
self.quantity = quantity self.quantity = quantity
self.months = months self.months = months
self.expire_date = expire_date self.expire_date = expire_date
self.new_subscribers = new_subscribers self.new_subscribers = new_subscribers
self.countries_iso2 = countries_iso2 self.allowed_countries = allowed_countries
self.private_channel_ids = private_channel_ids self.private_channel_ids = private_channel_ids
@staticmethod @staticmethod
async def _parse(client, message: "raw.types.Message") -> "Giveaway": async def _parse(client, message: "raw.types.Message") -> "Giveaway":
giveaway: "raw.types.MessageMediaGiveaway" = message.media giveaway: "raw.types.MessageMediaGiveaway" = message.media
channels = [] chats = []
private_ids = [] private_ids = []
for raw_channel_id in giveaway.channels: for raw_chat_id in giveaway.channels:
channel_id = utils.get_channel_id(raw_channel_id) chat_id = utils.get_channel_id(raw_chat_id)
try: try:
chat = await client.invoke( chat = await client.invoke(
raw.functions.channels.GetChannels( raw.functions.channels.GetChannels(
id=[await client.resolve_peer(channel_id)] id=[await client.resolve_peer(chat_id)]
) )
) )
except FloodWait as e: except FloodWait as e:
await asyncio.sleep(e.value) await asyncio.sleep(e.value)
except Exception: except Exception:
private_ids.append(channel_id) private_ids.append(chat_id)
else: else:
channels.append(types.Chat._parse_chat(client, chat.chats[0])) chats.append(types.Chat._parse_chat(client, chat.chats[0]))
return Giveaway( return Giveaway(
channels=channels, chats=chats,
quantity=giveaway.quantity, quantity=giveaway.quantity,
months=giveaway.months, months=giveaway.months,
expire_date=utils.timestamp_to_datetime(giveaway.until_date), expire_date=utils.timestamp_to_datetime(giveaway.until_date),
new_subscribers=giveaway.only_new_subscribers, 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, private_channel_ids=private_ids if len(private_ids) > 0 else None,
client=client client=client
) )

View file

@ -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
)

View file

@ -198,6 +198,9 @@ class Message(Object, Update):
giveaway (:obj:`~pyrogram.types.Giveaway`, *optional*): giveaway (:obj:`~pyrogram.types.Giveaway`, *optional*):
Message is a giveaway, information about the giveaway. 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*): story (:obj:`~pyrogram.types.MessageStory` | :obj:`~pyrogram.types.Story`, *optional*):
Message is a forwarded story, information about the forwarded story. Message is a forwarded story, information about the forwarded story.
@ -411,6 +414,7 @@ class Message(Object, Update):
animation: "types.Animation" = None, animation: "types.Animation" = None,
game: "types.Game" = None, game: "types.Game" = None,
giveaway: "types.Giveaway" = None, giveaway: "types.Giveaway" = None,
giveaway_result: "types.GiveawayResult" = None,
story: Union["types.MessageStory", "types.Story"] = None, story: Union["types.MessageStory", "types.Story"] = None,
video: "types.Video" = None, video: "types.Video" = None,
voice: "types.Voice" = None, voice: "types.Voice" = None,
@ -506,6 +510,7 @@ class Message(Object, Update):
self.animation = animation self.animation = animation
self.game = game self.game = game
self.giveaway = giveaway self.giveaway = giveaway
self.giveaway_result = giveaway_result
self.story = story self.story = story
self.video = video self.video = video
self.voice = voice self.voice = voice
@ -849,6 +854,7 @@ class Message(Object, Update):
venue = None venue = None
game = None game = None
giveaway = None giveaway = None
giveaway_result = None
story = None story = None
audio = None audio = None
voice = None voice = None
@ -885,6 +891,9 @@ class Message(Object, Update):
elif isinstance(media, raw.types.MessageMediaGiveaway): elif isinstance(media, raw.types.MessageMediaGiveaway):
giveaway = await types.Giveaway._parse(client, message) giveaway = await types.Giveaway._parse(client, message)
media_type = enums.MessageMediaType.GIVEAWAY 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): elif isinstance(media, raw.types.MessageMediaStory):
story = await types.MessageStory._parse(client, media) story = await types.MessageStory._parse(client, media)
media_type = enums.MessageMediaType.STORY media_type = enums.MessageMediaType.STORY
@ -1018,6 +1027,7 @@ class Message(Object, Update):
animation=animation, animation=animation,
game=game, game=game,
giveaway=giveaway, giveaway=giveaway,
giveaway_result=giveaway_result,
story=story, story=story,
video=video, video=video,
video_note=video_note, video_note=video_note,