From de8f784f7847d435e539ab5208dfff4568ea51fd Mon Sep 17 00:00:00 2001
From: Dan <14043624+delivrance@users.noreply.github.com>
Date: Thu, 9 Jul 2020 02:22:56 +0200
Subject: [PATCH] Use better checks for local and external files
---
.../client/methods/chats/set_chat_photo.py | 2 +-
.../methods/messages/edit_inline_media.py | 12 ++++++-----
.../methods/messages/edit_message_media.py | 21 ++++++++++---------
.../client/methods/messages/send_animation.py | 5 +++--
.../client/methods/messages/send_audio.py | 5 +++--
.../client/methods/messages/send_document.py | 5 +++--
.../methods/messages/send_media_group.py | 11 +++++-----
.../client/methods/messages/send_photo.py | 5 +++--
.../client/methods/messages/send_sticker.py | 5 +++--
.../client/methods/messages/send_video.py | 5 +++--
.../methods/messages/send_video_note.py | 2 +-
.../client/methods/messages/send_voice.py | 5 +++--
12 files changed, 46 insertions(+), 37 deletions(-)
diff --git a/pyrogram/client/methods/chats/set_chat_photo.py b/pyrogram/client/methods/chats/set_chat_photo.py
index 3a996711..d394322c 100644
--- a/pyrogram/client/methods/chats/set_chat_photo.py
+++ b/pyrogram/client/methods/chats/set_chat_photo.py
@@ -63,7 +63,7 @@ class SetChatPhoto(BaseClient):
"""
peer = self.resolve_peer(chat_id)
- if os.path.exists(photo):
+ if os.path.isfile(photo):
photo = types.InputChatUploadedPhoto(file=self.save_file(photo))
else:
photo = utils.get_input_media_from_file_id(photo, file_ref, 2)
diff --git a/pyrogram/client/methods/messages/edit_inline_media.py b/pyrogram/client/methods/messages/edit_inline_media.py
index 700804d9..f409ae06 100644
--- a/pyrogram/client/methods/messages/edit_inline_media.py
+++ b/pyrogram/client/methods/messages/edit_inline_media.py
@@ -16,6 +16,8 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see .
+import re
+
import pyrogram
from pyrogram.api import functions, types
from pyrogram.client.ext import BaseClient, utils
@@ -72,35 +74,35 @@ class EditInlineMedia(BaseClient):
parse_mode = media.parse_mode
if isinstance(media, InputMediaPhoto):
- if media.media.startswith("http"):
+ if re.match("^https?://", media.media):
media = types.InputMediaPhotoExternal(
url=media.media
)
else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 2)
elif isinstance(media, InputMediaVideo):
- if media.media.startswith("http"):
+ if re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal(
url=media.media
)
else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 4)
elif isinstance(media, InputMediaAudio):
- if media.media.startswith("http"):
+ if re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal(
url=media.media
)
else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 9)
elif isinstance(media, InputMediaAnimation):
- if media.media.startswith("http"):
+ if re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal(
url=media.media
)
else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 10)
elif isinstance(media, InputMediaDocument):
- if media.media.startswith("http"):
+ if re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal(
url=media.media
)
diff --git a/pyrogram/client/methods/messages/edit_message_media.py b/pyrogram/client/methods/messages/edit_message_media.py
index b6fbf933..3ab90744 100644
--- a/pyrogram/client/methods/messages/edit_message_media.py
+++ b/pyrogram/client/methods/messages/edit_message_media.py
@@ -17,6 +17,7 @@
# along with Pyrogram. If not, see .
import os
+import re
from typing import Union
import pyrogram
@@ -78,7 +79,7 @@ class EditMessageMedia(BaseClient):
parse_mode = media.parse_mode
if isinstance(media, InputMediaPhoto):
- if os.path.exists(media.media):
+ if os.path.isfile(media.media):
media = self.send(
functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id),
@@ -95,14 +96,14 @@ class EditMessageMedia(BaseClient):
file_reference=media.photo.file_reference
)
)
- elif media.media.startswith("http"):
+ elif re.match("^https?://", media.media):
media = types.InputMediaPhotoExternal(
url=media.media
)
else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 2)
elif isinstance(media, InputMediaVideo):
- if os.path.exists(media.media):
+ if os.path.isfile(media.media):
media = self.send(
functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id),
@@ -132,14 +133,14 @@ class EditMessageMedia(BaseClient):
file_reference=media.document.file_reference
)
)
- elif media.media.startswith("http"):
+ elif re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal(
url=media.media
)
else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 4)
elif isinstance(media, InputMediaAudio):
- if os.path.exists(media.media):
+ if os.path.isfile(media.media):
media = self.send(
functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id),
@@ -168,14 +169,14 @@ class EditMessageMedia(BaseClient):
file_reference=media.document.file_reference
)
)
- elif media.media.startswith("http"):
+ elif re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal(
url=media.media
)
else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 9)
elif isinstance(media, InputMediaAnimation):
- if os.path.exists(media.media):
+ if os.path.isfile(media.media):
media = self.send(
functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id),
@@ -206,14 +207,14 @@ class EditMessageMedia(BaseClient):
file_reference=media.document.file_reference
)
)
- elif media.media.startswith("http"):
+ elif re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal(
url=media.media
)
else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 10)
elif isinstance(media, InputMediaDocument):
- if os.path.exists(media.media):
+ if os.path.isfile(media.media):
media = self.send(
functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id),
@@ -237,7 +238,7 @@ class EditMessageMedia(BaseClient):
file_reference=media.document.file_reference
)
)
- elif media.media.startswith("http"):
+ elif re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal(
url=media.media
)
diff --git a/pyrogram/client/methods/messages/send_animation.py b/pyrogram/client/methods/messages/send_animation.py
index 288ed04e..c84e0503 100644
--- a/pyrogram/client/methods/messages/send_animation.py
+++ b/pyrogram/client/methods/messages/send_animation.py
@@ -17,6 +17,7 @@
# along with Pyrogram. If not, see .
import os
+import re
from typing import Union
import pyrogram
@@ -163,7 +164,7 @@ class SendAnimation(BaseClient):
file = None
try:
- if os.path.exists(animation):
+ if os.path.isfile(animation):
thumb = None if thumb is None else self.save_file(thumb)
file = self.save_file(animation, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument(
@@ -181,7 +182,7 @@ class SendAnimation(BaseClient):
types.DocumentAttributeAnimated()
]
)
- elif animation.startswith("http"):
+ elif re.match("^https?://", animation):
media = types.InputMediaDocumentExternal(
url=animation
)
diff --git a/pyrogram/client/methods/messages/send_audio.py b/pyrogram/client/methods/messages/send_audio.py
index e271d96c..49fd0e09 100644
--- a/pyrogram/client/methods/messages/send_audio.py
+++ b/pyrogram/client/methods/messages/send_audio.py
@@ -17,6 +17,7 @@
# along with Pyrogram. If not, see .
import os
+import re
from typing import Union
import pyrogram
@@ -163,7 +164,7 @@ class SendAudio(BaseClient):
file = None
try:
- if os.path.exists(audio):
+ if os.path.isfile(audio):
thumb = None if thumb is None else self.save_file(thumb)
file = self.save_file(audio, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument(
@@ -179,7 +180,7 @@ class SendAudio(BaseClient):
types.DocumentAttributeFilename(file_name=file_name or os.path.basename(audio))
]
)
- elif audio.startswith("http"):
+ elif re.match("^https?://", audio):
media = types.InputMediaDocumentExternal(
url=audio
)
diff --git a/pyrogram/client/methods/messages/send_document.py b/pyrogram/client/methods/messages/send_document.py
index 24a754f0..8f15c5ee 100644
--- a/pyrogram/client/methods/messages/send_document.py
+++ b/pyrogram/client/methods/messages/send_document.py
@@ -17,6 +17,7 @@
# along with Pyrogram. If not, see .
import os
+import re
from typing import Union
import pyrogram
@@ -143,7 +144,7 @@ class SendDocument(BaseClient):
file = None
try:
- if os.path.exists(document):
+ if os.path.isfile(document):
thumb = None if thumb is None else self.save_file(thumb)
file = self.save_file(document, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument(
@@ -154,7 +155,7 @@ class SendDocument(BaseClient):
types.DocumentAttributeFilename(file_name=file_name or os.path.basename(document))
]
)
- elif document.startswith("http"):
+ elif re.match("^https?://", document):
media = types.InputMediaDocumentExternal(
url=document
)
diff --git a/pyrogram/client/methods/messages/send_media_group.py b/pyrogram/client/methods/messages/send_media_group.py
index 8571ef4f..9ca4473b 100644
--- a/pyrogram/client/methods/messages/send_media_group.py
+++ b/pyrogram/client/methods/messages/send_media_group.py
@@ -18,13 +18,12 @@
import logging
import os
-import time
+import re
from typing import Union, List
import pyrogram
from pyrogram.api import functions, types
from pyrogram.client.ext import BaseClient, utils
-from pyrogram.errors import FloodWait
log = logging.getLogger(__name__)
@@ -77,7 +76,7 @@ class SendMediaGroup(BaseClient):
for i in media:
if isinstance(i, pyrogram.InputMediaPhoto):
- if os.path.exists(i.media):
+ if os.path.isfile(i.media):
media = self.send(
functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id),
@@ -94,7 +93,7 @@ class SendMediaGroup(BaseClient):
file_reference=media.photo.file_reference
)
)
- elif i.media.startswith("http"):
+ elif re.match("^https?://", i.media):
media = self.send(
functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id),
@@ -114,7 +113,7 @@ class SendMediaGroup(BaseClient):
else:
media = utils.get_input_media_from_file_id(i.media, i.file_ref, 2)
elif isinstance(i, pyrogram.InputMediaVideo):
- if os.path.exists(i.media):
+ if os.path.isfile(i.media):
media = self.send(
functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id),
@@ -142,7 +141,7 @@ class SendMediaGroup(BaseClient):
file_reference=media.document.file_reference
)
)
- elif i.media.startswith("http"):
+ elif re.match("^https?://", i.media):
media = self.send(
functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id),
diff --git a/pyrogram/client/methods/messages/send_photo.py b/pyrogram/client/methods/messages/send_photo.py
index 4d6a18a3..c21bb487 100644
--- a/pyrogram/client/methods/messages/send_photo.py
+++ b/pyrogram/client/methods/messages/send_photo.py
@@ -17,6 +17,7 @@
# along with Pyrogram. If not, see .
import os
+import re
from typing import Union
import pyrogram
@@ -137,13 +138,13 @@ class SendPhoto(BaseClient):
file = None
try:
- if os.path.exists(photo):
+ if os.path.isfile(photo):
file = self.save_file(photo, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedPhoto(
file=file,
ttl_seconds=ttl_seconds
)
- elif photo.startswith("http"):
+ elif re.match("^https?://", photo):
media = types.InputMediaPhotoExternal(
url=photo,
ttl_seconds=ttl_seconds
diff --git a/pyrogram/client/methods/messages/send_sticker.py b/pyrogram/client/methods/messages/send_sticker.py
index 76a42d3d..d9575885 100644
--- a/pyrogram/client/methods/messages/send_sticker.py
+++ b/pyrogram/client/methods/messages/send_sticker.py
@@ -17,6 +17,7 @@
# along with Pyrogram. If not, see .
import os
+import re
from typing import Union
import pyrogram
@@ -113,7 +114,7 @@ class SendSticker(BaseClient):
file = None
try:
- if os.path.exists(sticker):
+ if os.path.isfile(sticker):
file = self.save_file(sticker, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(sticker) or "image/webp",
@@ -122,7 +123,7 @@ class SendSticker(BaseClient):
types.DocumentAttributeFilename(file_name=os.path.basename(sticker))
]
)
- elif sticker.startswith("http"):
+ elif re.match("^https?://", sticker):
media = types.InputMediaDocumentExternal(
url=sticker
)
diff --git a/pyrogram/client/methods/messages/send_video.py b/pyrogram/client/methods/messages/send_video.py
index fc58aa98..9a67bbbb 100644
--- a/pyrogram/client/methods/messages/send_video.py
+++ b/pyrogram/client/methods/messages/send_video.py
@@ -17,6 +17,7 @@
# along with Pyrogram. If not, see .
import os
+import re
from typing import Union
import pyrogram
@@ -160,7 +161,7 @@ class SendVideo(BaseClient):
file = None
try:
- if os.path.exists(video):
+ if os.path.isfile(video):
thumb = None if thumb is None else self.save_file(thumb)
file = self.save_file(video, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument(
@@ -177,7 +178,7 @@ class SendVideo(BaseClient):
types.DocumentAttributeFilename(file_name=file_name or os.path.basename(video))
]
)
- elif video.startswith("http"):
+ elif re.match("^https?://", video):
media = types.InputMediaDocumentExternal(
url=video
)
diff --git a/pyrogram/client/methods/messages/send_video_note.py b/pyrogram/client/methods/messages/send_video_note.py
index 64bde11b..b7acdc01 100644
--- a/pyrogram/client/methods/messages/send_video_note.py
+++ b/pyrogram/client/methods/messages/send_video_note.py
@@ -128,7 +128,7 @@ class SendVideoNote(BaseClient):
file = None
try:
- if os.path.exists(video_note):
+ if os.path.isfile(video_note):
thumb = None if thumb is None else self.save_file(thumb)
file = self.save_file(video_note, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument(
diff --git a/pyrogram/client/methods/messages/send_voice.py b/pyrogram/client/methods/messages/send_voice.py
index 753e3806..23492f53 100644
--- a/pyrogram/client/methods/messages/send_voice.py
+++ b/pyrogram/client/methods/messages/send_voice.py
@@ -17,6 +17,7 @@
# along with Pyrogram. If not, see .
import os
+import re
from typing import Union
import pyrogram
@@ -132,7 +133,7 @@ class SendVoice(BaseClient):
file = None
try:
- if os.path.exists(voice):
+ if os.path.isfile(voice):
file = self.save_file(voice, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(voice) or "audio/mpeg",
@@ -144,7 +145,7 @@ class SendVoice(BaseClient):
)
]
)
- elif voice.startswith("http"):
+ elif re.match("^https?://", voice):
media = types.InputMediaDocumentExternal(
url=voice
)