Use better checks for local and external files

This commit is contained in:
Dan 2020-07-09 02:22:56 +02:00
parent 4a8e6fb855
commit de8f784f78
12 changed files with 46 additions and 37 deletions

View File

@ -63,7 +63,7 @@ class SetChatPhoto(BaseClient):
""" """
peer = self.resolve_peer(chat_id) peer = self.resolve_peer(chat_id)
if os.path.exists(photo): if os.path.isfile(photo):
photo = types.InputChatUploadedPhoto(file=self.save_file(photo)) photo = types.InputChatUploadedPhoto(file=self.save_file(photo))
else: else:
photo = utils.get_input_media_from_file_id(photo, file_ref, 2) photo = utils.get_input_media_from_file_id(photo, file_ref, 2)

View File

@ -16,6 +16,8 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import re
import pyrogram import pyrogram
from pyrogram.api import functions, types from pyrogram.api import functions, types
from pyrogram.client.ext import BaseClient, utils from pyrogram.client.ext import BaseClient, utils
@ -72,35 +74,35 @@ class EditInlineMedia(BaseClient):
parse_mode = media.parse_mode parse_mode = media.parse_mode
if isinstance(media, InputMediaPhoto): if isinstance(media, InputMediaPhoto):
if media.media.startswith("http"): if re.match("^https?://", media.media):
media = types.InputMediaPhotoExternal( media = types.InputMediaPhotoExternal(
url=media.media url=media.media
) )
else: else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 2) media = utils.get_input_media_from_file_id(media.media, media.file_ref, 2)
elif isinstance(media, InputMediaVideo): elif isinstance(media, InputMediaVideo):
if media.media.startswith("http"): if re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=media.media url=media.media
) )
else: else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 4) media = utils.get_input_media_from_file_id(media.media, media.file_ref, 4)
elif isinstance(media, InputMediaAudio): elif isinstance(media, InputMediaAudio):
if media.media.startswith("http"): if re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=media.media url=media.media
) )
else: else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 9) media = utils.get_input_media_from_file_id(media.media, media.file_ref, 9)
elif isinstance(media, InputMediaAnimation): elif isinstance(media, InputMediaAnimation):
if media.media.startswith("http"): if re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=media.media url=media.media
) )
else: else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 10) media = utils.get_input_media_from_file_id(media.media, media.file_ref, 10)
elif isinstance(media, InputMediaDocument): elif isinstance(media, InputMediaDocument):
if media.media.startswith("http"): if re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=media.media url=media.media
) )

View File

@ -17,6 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import os import os
import re
from typing import Union from typing import Union
import pyrogram import pyrogram
@ -78,7 +79,7 @@ class EditMessageMedia(BaseClient):
parse_mode = media.parse_mode parse_mode = media.parse_mode
if isinstance(media, InputMediaPhoto): if isinstance(media, InputMediaPhoto):
if os.path.exists(media.media): if os.path.isfile(media.media):
media = self.send( media = self.send(
functions.messages.UploadMedia( functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id), peer=self.resolve_peer(chat_id),
@ -95,14 +96,14 @@ class EditMessageMedia(BaseClient):
file_reference=media.photo.file_reference file_reference=media.photo.file_reference
) )
) )
elif media.media.startswith("http"): elif re.match("^https?://", media.media):
media = types.InputMediaPhotoExternal( media = types.InputMediaPhotoExternal(
url=media.media url=media.media
) )
else: else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 2) media = utils.get_input_media_from_file_id(media.media, media.file_ref, 2)
elif isinstance(media, InputMediaVideo): elif isinstance(media, InputMediaVideo):
if os.path.exists(media.media): if os.path.isfile(media.media):
media = self.send( media = self.send(
functions.messages.UploadMedia( functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id), peer=self.resolve_peer(chat_id),
@ -132,14 +133,14 @@ class EditMessageMedia(BaseClient):
file_reference=media.document.file_reference file_reference=media.document.file_reference
) )
) )
elif media.media.startswith("http"): elif re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=media.media url=media.media
) )
else: else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 4) media = utils.get_input_media_from_file_id(media.media, media.file_ref, 4)
elif isinstance(media, InputMediaAudio): elif isinstance(media, InputMediaAudio):
if os.path.exists(media.media): if os.path.isfile(media.media):
media = self.send( media = self.send(
functions.messages.UploadMedia( functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id), peer=self.resolve_peer(chat_id),
@ -168,14 +169,14 @@ class EditMessageMedia(BaseClient):
file_reference=media.document.file_reference file_reference=media.document.file_reference
) )
) )
elif media.media.startswith("http"): elif re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=media.media url=media.media
) )
else: else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 9) media = utils.get_input_media_from_file_id(media.media, media.file_ref, 9)
elif isinstance(media, InputMediaAnimation): elif isinstance(media, InputMediaAnimation):
if os.path.exists(media.media): if os.path.isfile(media.media):
media = self.send( media = self.send(
functions.messages.UploadMedia( functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id), peer=self.resolve_peer(chat_id),
@ -206,14 +207,14 @@ class EditMessageMedia(BaseClient):
file_reference=media.document.file_reference file_reference=media.document.file_reference
) )
) )
elif media.media.startswith("http"): elif re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=media.media url=media.media
) )
else: else:
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 10) media = utils.get_input_media_from_file_id(media.media, media.file_ref, 10)
elif isinstance(media, InputMediaDocument): elif isinstance(media, InputMediaDocument):
if os.path.exists(media.media): if os.path.isfile(media.media):
media = self.send( media = self.send(
functions.messages.UploadMedia( functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id), peer=self.resolve_peer(chat_id),
@ -237,7 +238,7 @@ class EditMessageMedia(BaseClient):
file_reference=media.document.file_reference file_reference=media.document.file_reference
) )
) )
elif media.media.startswith("http"): elif re.match("^https?://", media.media):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=media.media url=media.media
) )

View File

@ -17,6 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import os import os
import re
from typing import Union from typing import Union
import pyrogram import pyrogram
@ -163,7 +164,7 @@ class SendAnimation(BaseClient):
file = None file = None
try: try:
if os.path.exists(animation): if os.path.isfile(animation):
thumb = None if thumb is None else self.save_file(thumb) thumb = None if thumb is None else self.save_file(thumb)
file = self.save_file(animation, progress=progress, progress_args=progress_args) file = self.save_file(animation, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument( media = types.InputMediaUploadedDocument(
@ -181,7 +182,7 @@ class SendAnimation(BaseClient):
types.DocumentAttributeAnimated() types.DocumentAttributeAnimated()
] ]
) )
elif animation.startswith("http"): elif re.match("^https?://", animation):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=animation url=animation
) )

View File

@ -17,6 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import os import os
import re
from typing import Union from typing import Union
import pyrogram import pyrogram
@ -163,7 +164,7 @@ class SendAudio(BaseClient):
file = None file = None
try: try:
if os.path.exists(audio): if os.path.isfile(audio):
thumb = None if thumb is None else self.save_file(thumb) thumb = None if thumb is None else self.save_file(thumb)
file = self.save_file(audio, progress=progress, progress_args=progress_args) file = self.save_file(audio, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument( media = types.InputMediaUploadedDocument(
@ -179,7 +180,7 @@ class SendAudio(BaseClient):
types.DocumentAttributeFilename(file_name=file_name or os.path.basename(audio)) types.DocumentAttributeFilename(file_name=file_name or os.path.basename(audio))
] ]
) )
elif audio.startswith("http"): elif re.match("^https?://", audio):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=audio url=audio
) )

View File

@ -17,6 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import os import os
import re
from typing import Union from typing import Union
import pyrogram import pyrogram
@ -143,7 +144,7 @@ class SendDocument(BaseClient):
file = None file = None
try: try:
if os.path.exists(document): if os.path.isfile(document):
thumb = None if thumb is None else self.save_file(thumb) thumb = None if thumb is None else self.save_file(thumb)
file = self.save_file(document, progress=progress, progress_args=progress_args) file = self.save_file(document, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument( media = types.InputMediaUploadedDocument(
@ -154,7 +155,7 @@ class SendDocument(BaseClient):
types.DocumentAttributeFilename(file_name=file_name or os.path.basename(document)) types.DocumentAttributeFilename(file_name=file_name or os.path.basename(document))
] ]
) )
elif document.startswith("http"): elif re.match("^https?://", document):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=document url=document
) )

View File

@ -18,13 +18,12 @@
import logging import logging
import os import os
import time import re
from typing import Union, List from typing import Union, List
import pyrogram import pyrogram
from pyrogram.api import functions, types from pyrogram.api import functions, types
from pyrogram.client.ext import BaseClient, utils from pyrogram.client.ext import BaseClient, utils
from pyrogram.errors import FloodWait
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -77,7 +76,7 @@ class SendMediaGroup(BaseClient):
for i in media: for i in media:
if isinstance(i, pyrogram.InputMediaPhoto): if isinstance(i, pyrogram.InputMediaPhoto):
if os.path.exists(i.media): if os.path.isfile(i.media):
media = self.send( media = self.send(
functions.messages.UploadMedia( functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id), peer=self.resolve_peer(chat_id),
@ -94,7 +93,7 @@ class SendMediaGroup(BaseClient):
file_reference=media.photo.file_reference file_reference=media.photo.file_reference
) )
) )
elif i.media.startswith("http"): elif re.match("^https?://", i.media):
media = self.send( media = self.send(
functions.messages.UploadMedia( functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id), peer=self.resolve_peer(chat_id),
@ -114,7 +113,7 @@ class SendMediaGroup(BaseClient):
else: else:
media = utils.get_input_media_from_file_id(i.media, i.file_ref, 2) media = utils.get_input_media_from_file_id(i.media, i.file_ref, 2)
elif isinstance(i, pyrogram.InputMediaVideo): elif isinstance(i, pyrogram.InputMediaVideo):
if os.path.exists(i.media): if os.path.isfile(i.media):
media = self.send( media = self.send(
functions.messages.UploadMedia( functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id), peer=self.resolve_peer(chat_id),
@ -142,7 +141,7 @@ class SendMediaGroup(BaseClient):
file_reference=media.document.file_reference file_reference=media.document.file_reference
) )
) )
elif i.media.startswith("http"): elif re.match("^https?://", i.media):
media = self.send( media = self.send(
functions.messages.UploadMedia( functions.messages.UploadMedia(
peer=self.resolve_peer(chat_id), peer=self.resolve_peer(chat_id),

View File

@ -17,6 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import os import os
import re
from typing import Union from typing import Union
import pyrogram import pyrogram
@ -137,13 +138,13 @@ class SendPhoto(BaseClient):
file = None file = None
try: try:
if os.path.exists(photo): if os.path.isfile(photo):
file = self.save_file(photo, progress=progress, progress_args=progress_args) file = self.save_file(photo, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedPhoto( media = types.InputMediaUploadedPhoto(
file=file, file=file,
ttl_seconds=ttl_seconds ttl_seconds=ttl_seconds
) )
elif photo.startswith("http"): elif re.match("^https?://", photo):
media = types.InputMediaPhotoExternal( media = types.InputMediaPhotoExternal(
url=photo, url=photo,
ttl_seconds=ttl_seconds ttl_seconds=ttl_seconds

View File

@ -17,6 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import os import os
import re
from typing import Union from typing import Union
import pyrogram import pyrogram
@ -113,7 +114,7 @@ class SendSticker(BaseClient):
file = None file = None
try: try:
if os.path.exists(sticker): if os.path.isfile(sticker):
file = self.save_file(sticker, progress=progress, progress_args=progress_args) file = self.save_file(sticker, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument( media = types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(sticker) or "image/webp", 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)) types.DocumentAttributeFilename(file_name=os.path.basename(sticker))
] ]
) )
elif sticker.startswith("http"): elif re.match("^https?://", sticker):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=sticker url=sticker
) )

View File

@ -17,6 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import os import os
import re
from typing import Union from typing import Union
import pyrogram import pyrogram
@ -160,7 +161,7 @@ class SendVideo(BaseClient):
file = None file = None
try: try:
if os.path.exists(video): if os.path.isfile(video):
thumb = None if thumb is None else self.save_file(thumb) thumb = None if thumb is None else self.save_file(thumb)
file = self.save_file(video, progress=progress, progress_args=progress_args) file = self.save_file(video, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument( media = types.InputMediaUploadedDocument(
@ -177,7 +178,7 @@ class SendVideo(BaseClient):
types.DocumentAttributeFilename(file_name=file_name or os.path.basename(video)) types.DocumentAttributeFilename(file_name=file_name or os.path.basename(video))
] ]
) )
elif video.startswith("http"): elif re.match("^https?://", video):
media = types.InputMediaDocumentExternal( media = types.InputMediaDocumentExternal(
url=video url=video
) )

View File

@ -128,7 +128,7 @@ class SendVideoNote(BaseClient):
file = None file = None
try: try:
if os.path.exists(video_note): if os.path.isfile(video_note):
thumb = None if thumb is None else self.save_file(thumb) thumb = None if thumb is None else self.save_file(thumb)
file = self.save_file(video_note, progress=progress, progress_args=progress_args) file = self.save_file(video_note, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument( media = types.InputMediaUploadedDocument(

View File

@ -17,6 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import os import os
import re
from typing import Union from typing import Union
import pyrogram import pyrogram
@ -132,7 +133,7 @@ class SendVoice(BaseClient):
file = None file = None
try: try:
if os.path.exists(voice): if os.path.isfile(voice):
file = self.save_file(voice, progress=progress, progress_args=progress_args) file = self.save_file(voice, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument( media = types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(voice) or "audio/mpeg", 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( media = types.InputMediaDocumentExternal(
url=voice url=voice
) )