feat: support storydl from link

This commit is contained in:
New-dev0
2025-06-02 14:49:41 +05:30
parent b1ee7d9733
commit db7abae627

View File

@@ -11,18 +11,19 @@
• `{i}setstory <reply media>`
Set replied media as your story.
• `{i}storydl <username/reply user>`
Download and upload user stories!
• `{i}storydl <username/reply user/story link>`
Download and upload user stories or specific story from link!
"""
import os
import re
from contextlib import suppress
from . import ultroid_cmd, get_string, LOGS
from telethon import TelegramClient
from telethon.tl.functions.channels import GetFullChannelRequest
from telethon.tl.types import User, UserFull, InputPeerSelf, InputPrivacyValueAllowAll, Channel
from telethon.tl.functions.stories import SendStoryRequest
from telethon.tl.types import User, UserFull, InputPeerSelf, InputPrivacyValueAllowAll, Channel, InputUserSelf
from telethon.tl.functions.stories import SendStoryRequest, GetStoriesByIDRequest
from telethon.tl.functions.users import GetFullUserRequest
from telethon.events import NewMessage
@@ -53,20 +54,70 @@ async def setStory(event: NewMessage.Event):
@ultroid_cmd("storydl")
async def downloadUserStories(event: NewMessage.Event):
replied = await event.get_reply_message()
await event.eor(get_string("com_1"))
message = await event.eor(get_string("com_1"))
try:
username = event.text.split(maxsplit=1)[1]
except IndexError:
text_input = event.text.split(maxsplit=1)[1]
# Check if input is a Telegram story link
story_link_pattern = r"https?://t\.me/([^/]+)/s/(\d+)"
match = re.match(story_link_pattern, text_input)
if match:
# Extract username and story ID from link
username = match.group(1)
story_id = int(match.group(2))
try:
# Get the entity for the username
entity = await event.client.get_entity(username)
# Using GetStoriesByIDRequest to fetch the specific story
stories_response = await event.client(
GetStoriesByIDRequest(
entity.id,
id=[story_id]
)
)
print(stories_response)
if not stories_response.stories:
return await message.eor("ERROR: Story not found or expired!")
# Download and upload the story
for story in stories_response.stories:
client: TelegramClient = event.client
file = await client.download_media(story.media)
caption = story.caption if hasattr(story, 'caption') else ""
await message.reply(
caption,
file=file
)
os.remove(file)
return await message.eor("**Uploaded Story!**", time=5)
except Exception as er:
await message.eor(f"ERROR while fetching story: __{er}__")
LOGS.exception(er)
return
# If not a story link, proceed with the original functionality
username = text_input
except IndexError as er:
LOGS.exception
if replied and isinstance(replied.sender, User):
username = replied.sender_id
else:
return await event.eor(
"Please reply to a user or provide username!"
# get_string("story_1")
return await message.eor(
"Please reply to a user, provide username or story link!"
)
with suppress(ValueError):
username = int(username)
stories = None
try:
entity = await event.client.get_entity(username)
if isinstance(entity, Channel):
@@ -80,18 +131,20 @@ async def downloadUserStories(event: NewMessage.Event):
).full_user
stories = full_user.stories
except Exception as er:
await event.eor(f"ERROR: __{er}__")
await message.eor(f"ERROR: __{er}__")
return
if not (stories and stories.stories):
await event.eor("ERROR: Stories not found!")
await message.eor("ERROR: Stories not found!")
return
for story in stories.stories:
for story in stories.stories[:5]:
client: TelegramClient = event.client
file = await client.download_media(story.media)
await event.reply(
story.caption,
caption = story.caption if hasattr(story, 'caption') else ""
await message.reply(
caption,
file=file
)
os.remove(file)
await event.eor("**Uploaded Stories!**", time=5)
await message.eor("**Uploaded Stories!**", time=5)