forked from tangyoha/telegram_media_downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_common.py
202 lines (166 loc) · 5.8 KB
/
test_common.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import datetime
import platform
from pyrogram.file_id import PHOTO_TYPES, FileType
class Chat:
def __init__(self, chat_id, chat_title):
self.id = chat_id
self.title = chat_title
class Date:
def __init__(self, date):
self.date = date
def strftime(self, str) -> str:
return ""
class MockMessage:
def __init__(self, **kwargs):
self.id = kwargs.get("id")
self.media = kwargs.get("media")
self.audio = kwargs.get("audio", None)
self.document = kwargs.get("document", None)
self.photo = kwargs.get("photo", None)
self.video = kwargs.get("video", None)
self.voice = kwargs.get("voice", None)
self.video_note = kwargs.get("video_note", None)
self.media_group_id = kwargs.get("media_group_id", None)
self.caption = kwargs.get("caption", None)
self.text = kwargs.get("text", None)
self.empty = kwargs.get("empty", False)
self.from_user = kwargs.get("from_user", None)
self.reply_to_message_id = kwargs.get("reply_to_message_id", None)
if kwargs.get("dis_chat") == None:
self.chat = Chat(
kwargs.get("chat_id", None), kwargs.get("chat_title", None)
)
else:
self.chat = kwargs.get("chat", None)
self.date: datetime = None
if kwargs.get("date") != None:
self.date = kwargs["date"]
class MockUser:
def __init__(self, **kwargs):
self.id = kwargs.get("id", 0)
self.username = kwargs.get("username", "")
class MockAudio:
def __init__(self, **kwargs):
self.file_name = kwargs["file_name"]
self.mime_type = kwargs["mime_type"]
self.file_id = "AUDIO"
if kwargs.get("file_size"):
self.file_size = kwargs["file_size"]
else:
self.file_size = 1024
class MockDocument:
def __init__(self, **kwargs):
self.file_name = kwargs["file_name"]
self.mime_type = kwargs["mime_type"]
self.file_id = "DOCUMENT"
if kwargs.get("file_size"):
self.file_size = kwargs["file_size"]
else:
self.file_size = 1024
class MockPhoto:
def __init__(self, **kwargs):
self.date = kwargs["date"]
self.file_unique_id = kwargs["file_unique_id"]
self.file_id = "PHOTO"
if kwargs.get("file_size"):
self.file_size = kwargs["file_size"]
else:
self.file_size = 1024
class MockVoice:
def __init__(self, **kwargs):
self.mime_type = kwargs["mime_type"]
self.date = kwargs["date"]
self.file_id = "VOICE"
if kwargs.get("file_size"):
self.file_size = kwargs["file_size"]
else:
self.file_size = 1024
class MockVideo:
def __init__(self, **kwargs):
self.file_name = kwargs.get("file_name")
self.mime_type = kwargs["mime_type"]
self.file_id = "VIDEO"
if kwargs.get("file_size"):
self.file_size = kwargs["file_size"]
else:
self.file_size = 1024
if kwargs.get("width"):
self.width = kwargs["width"]
else:
self.width = 1920
if kwargs.get("height"):
self.height = kwargs["height"]
else:
self.height = 1080
if kwargs.get("duration"):
self.duration = kwargs["duration"]
else:
self.duration = 1024
class MockVideoNote:
def __init__(self, **kwargs):
self.mime_type = kwargs["mime_type"]
self.file_id = "VIDEO_NOTE"
self.date = kwargs["date"]
def platform_generic_path(_path: str) -> str:
platform_specific_path: str = _path
if platform.system() == "Windows":
platform_specific_path = platform_specific_path.replace("/", "\\")
return platform_specific_path
def get_file_type(file_id: str):
if file_id == "THUMBNAIL":
return FileType.THUMBNAIL
elif file_id == "CHAT_PHOTO":
return FileType.CHAT_PHOTO
elif file_id == "PHOTO":
return FileType.PHOTO
elif file_id == "VOICE":
return FileType.VOICE
elif file_id == "VIDEO":
return FileType.VIDEO
elif file_id == "DOCUMENT":
return FileType.DOCUMENT
elif file_id == "ENCRYPTED":
return FileType.ENCRYPTED
elif file_id == "TEMP":
return FileType.TEMP
elif file_id == "STICKER":
return FileType.STICKER
elif file_id == "AUDIO":
return FileType.AUDIO
elif file_id == "ANIMATION":
return FileType.ANIMATION
elif file_id == "ENCRYPTED_THUMBNAIL":
return FileType.ENCRYPTED_THUMBNAIL
elif file_id == "WALLPAPER":
return FileType.WALLPAPER
elif file_id == "VIDEO_NOTE":
return FileType.VIDEO_NOTE
elif file_id == "SECURE_RAW":
return FileType.SECURE_RAW
elif file_id == "SECURE":
return FileType.SECURE
elif file_id == "BACKGROUND":
return FileType.BACKGROUND
elif file_id == "DOCUMENT_AS_FILE":
return FileType.DOCUMENT_AS_FILE
raise ValueError("error file id!")
def get_extension(file_id: str, mime_type: str, dot: bool = True):
file_type = get_file_type(file_id=file_id)
guessed_extension = ""
if file_type in PHOTO_TYPES:
extension = "jpg"
elif file_type == FileType.VOICE:
extension = guessed_extension or "ogg"
elif file_type in (FileType.VIDEO, FileType.ANIMATION, FileType.VIDEO_NOTE):
extension = guessed_extension or "mp4"
elif file_type == FileType.DOCUMENT:
extension = guessed_extension or "zip"
elif file_type == FileType.STICKER:
extension = guessed_extension or "webp"
elif file_type == FileType.AUDIO:
extension = guessed_extension or "mp3"
else:
extension = "unknown"
if dot:
extension = "." + extension
return extension