forked from VJBots/VJ-FILTER-BOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_properties.py
60 lines (51 loc) · 1.95 KB
/
file_properties.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#Thanks @Lazydeveloperr helping this journey
from pyrogram import Client
from typing import Any, Optional
from pyrogram.types import Message
from pyrogram.file_id import FileId
from pyrogram.raw.types.messages import Messages
from server.exceptions import FIleNotFound
async def parse_file_id(message: "Message") -> Optional[FileId]:
media = get_media_from_message(message)
if media:
return FileId.decode(media.file_id)
async def parse_file_unique_id(message: "Messages") -> Optional[str]:
media = get_media_from_message(message)
if media:
return media.file_unique_id
async def get_file_ids(client: Client, chat_id: int, id: int) -> Optional[FileId]:
message = await client.get_messages(chat_id, id)
if message.empty:
raise FIleNotFound
media = get_media_from_message(message)
file_unique_id = await parse_file_unique_id(message)
file_id = await parse_file_id(message)
setattr(file_id, "file_size", getattr(media, "file_size", 0))
setattr(file_id, "mime_type", getattr(media, "mime_type", ""))
setattr(file_id, "file_name", getattr(media, "file_name", ""))
setattr(file_id, "unique_id", file_unique_id)
return file_id
def get_media_from_message(message: "Message") -> Any:
media_types = (
"audio",
"document",
"photo",
"sticker",
"animation",
"video",
"voice",
"video_note",
)
for attr in media_types:
media = getattr(message, attr, None)
if media:
return media
def get_hash(media_msg: Message) -> str:
media = get_media_from_message(media_msg)
return getattr(media, "file_unique_id", "")[:6]
def get_name(media_msg: Message) -> str:
media = get_media_from_message(media_msg)
return getattr(media, 'file_name', "")
def get_media_file_size(m):
media = get_media_from_message(m)
return getattr(media, "file_size", 0)