Merge branch 'develop' into asyncio

# Conflicts:
#	pyrogram/__init__.py
#	pyrogram/client/ext/base_client.py
#	pyrogram/client/methods/messages/delete_messages.py
#	pyrogram/client/types/messages_and_media/message.py
This commit is contained in:
Dan 2019-04-30 11:51:36 +02:00
commit 952f9bc503
21 changed files with 1935 additions and 38 deletions

View File

@ -1,6 +1,7 @@
## Include
include COPYING COPYING.lesser NOTICE requirements.txt
recursive-include compiler *.py *.tl *.tsv *.txt
recursive-include pyrogram mime.types
## Exclude
prune pyrogram/api/errors/exceptions

View File

@ -1313,3 +1313,6 @@ langpack.getLanguages#42c6978f lang_pack:string = Vector<LangPackLanguage>;
langpack.getLanguage#6a596502 lang_pack:string lang_code:string = LangPackLanguage;
// LAYER 97
// Ports
channels.exportInvite#c7560885 channel:InputChannel = ExportedChatInvite;

View File

@ -117,6 +117,9 @@ InputMessageContent
.. autoclass:: ChatMembers
:members:
.. autoclass:: ChatPermissions
:members:
.. autoclass:: Dialog
:members:

View File

@ -24,8 +24,7 @@ if sys.version_info[:3] in [(3, 5, 0), (3, 5, 1), (3, 5, 2)]:
# Monkey patch the standard "typing" module because Python versions from 3.5.0 to 3.5.2 have a broken one.
sys.modules["typing"] = typing
__version__ = "0.12.0.async"
__version__ = "0.13.0.async"
__license__ = "GNU Lesser General Public License v3 or later (LGPLv3+)"
__copyright__ = "Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>".replace(
"\xe8", "e" if sys.getfilesystemencoding() != "utf-8" else "\xe8"

View File

@ -869,18 +869,20 @@ class Client(Methods, BaseClient):
file_name = file_name or getattr(media, "file_name", None)
if not file_name:
if media_type == 3:
extension = ".ogg"
elif media_type in (4, 10, 13):
extension = mimetypes.guess_extension(media.mime_type) or ".mp4"
elif media_type == 5:
extension = mimetypes.guess_extension(media.mime_type) or ".unknown"
elif media_type == 8:
extension = ".webp"
elif media_type == 9:
extension = mimetypes.guess_extension(media.mime_type) or ".mp3"
elif media_type in (0, 1, 2):
guessed_extension = self.guess_extension(media.mime_type)
if media_type in (0, 1, 2):
extension = ".jpg"
elif media_type == 3:
extension = guessed_extension or ".ogg"
elif media_type in (4, 10, 13):
extension = guessed_extension or ".mp4"
elif media_type == 5:
extension = guessed_extension or ".zip"
elif media_type == 8:
extension = guessed_extension or ".webp"
elif media_type == 9:
extension = guessed_extension or ".mp3"
else:
continue
@ -1732,3 +1734,13 @@ class Client(Methods, BaseClient):
return ""
else:
return file_name
def guess_mime_type(self, filename: str):
extension = os.path.splitext(filename)[1]
return self.extensions_to_mime_types.get(extension)
def guess_extension(self, mime_type: str):
extensions = self.mime_types_to_extensions.get(mime_type)
if extensions:
return extensions.split(" ")[0]

View File

@ -16,6 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import os
import asyncio
import platform
import re
@ -66,6 +67,20 @@ class BaseClient:
13: "video_note"
}
mime_types_to_extensions = {}
extensions_to_mime_types = {}
with open("{}/mime.types".format(os.path.dirname(__file__)), "r", encoding="UTF-8") as f:
for match in re.finditer(r"^([^#\s]+)\s+(.+)$", f.read(), flags=re.M):
mime_type, extensions = match.groups()
extensions = [".{}".format(ext) for ext in extensions.split(" ")]
for ext in extensions:
extensions_to_mime_types[ext] = mime_type
mime_types_to_extensions[mime_type] = " ".join(extensions)
def __init__(self):
self.is_bot = None
self.dc_id = None
@ -131,3 +146,9 @@ class BaseClient:
async def answer_inline_query(self, *args, **kwargs):
pass
def guess_mime_type(self, *args, **kwargs):
pass
def guess_extension(self, *args, **kwargs):
pass

File diff suppressed because it is too large Load Diff

View File

@ -45,10 +45,3 @@ class InlineQueryHandler(Handler):
def __init__(self, callback: callable, filters=None):
super().__init__(callback, filters)
def check(self, callback_query):
return (
self.filters(callback_query)
if callable(self.filters)
else True
)

View File

@ -31,6 +31,13 @@ class ExportChatInviteLink(BaseClient):
You must be an administrator in the chat for this to work and have the appropriate admin rights.
.. note ::
Each administrator in a chat generates their own invite links. Bots can't use invite links generated by
other administrators. If you want your bot to work with invite links, it will need to generate its own link
using this method after this the link will become available to the bot via the :meth:`get_chat` method.
If your bot needs to generate a new invite link replacing its previous one, use this method again.
Args:
chat_id (``int`` | ``str``):
Unique identifier for the target chat or username of the target channel/supergroup

View File

@ -48,7 +48,7 @@ class DeleteMessages(BaseClient):
Defaults to True.
Returns:
True on success.
True on success, False otherwise.
Raises:
:class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error.
@ -71,4 +71,6 @@ class DeleteMessages(BaseClient):
)
)
return True
# Deleting messages you don't have right onto, won't raise any error.
# Check for pts_count, which is 0 in case deletes fail.
return bool(r.pts_count)

View File

@ -123,7 +123,7 @@ class EditMessageMedia(BaseClient):
functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id),
media=types.InputMediaUploadedDocument(
mime_type="video/mp4",
mime_type=self.guess_mime_type(media.media) or "video/mp4",
thumb=None if media.thumb is None else self.save_file(media.thumb),
file=await self.save_file(media.media),
attributes=[
@ -182,7 +182,7 @@ class EditMessageMedia(BaseClient):
functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id),
media=types.InputMediaUploadedDocument(
mime_type="audio/mpeg",
mime_type=self.guess_mime_type(media.media) or "audio/mpeg",
thumb=None if media.thumb is None else self.save_file(media.thumb),
file=await self.save_file(media.media),
attributes=[
@ -240,7 +240,7 @@ class EditMessageMedia(BaseClient):
functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id),
media=types.InputMediaUploadedDocument(
mime_type="video/mp4",
mime_type=self.guess_mime_type(media.media) or "video/mp4",
thumb=None if media.thumb is None else self.save_file(media.thumb),
file=await self.save_file(media.media),
attributes=[
@ -300,7 +300,7 @@ class EditMessageMedia(BaseClient):
functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id),
media=types.InputMediaUploadedDocument(
mime_type="application/zip",
mime_type=self.guess_mime_type(media.media) or "application/zip",
thumb=None if media.thumb is None else self.save_file(media.thumb),
file=await self.save_file(media.media),
attributes=[

View File

@ -135,7 +135,7 @@ class SendAnimation(BaseClient):
thumb = None if thumb is None else await self.save_file(thumb)
file = await self.save_file(animation, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument(
mime_type="video/mp4",
mime_type=self.guess_mime_type(animation) or "video/mp4",
file=file,
thumb=thumb,
attributes=[

View File

@ -136,7 +136,7 @@ class SendAudio(BaseClient):
thumb = None if thumb is None else await self.save_file(thumb)
file = await self.save_file(audio, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument(
mime_type="audio/mpeg",
mime_type=self.guess_mime_type(audio) or "audio/mpeg",
file=file,
thumb=thumb,
attributes=[

View File

@ -122,7 +122,7 @@ class SendDocument(BaseClient):
thumb = None if thumb is None else await self.save_file(thumb)
file = await self.save_file(document, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument(
mime_type="application/zip",
mime_type=self.guess_mime_type(document) or "application/zip",
file=file,
thumb=thumb,
attributes=[

View File

@ -129,7 +129,7 @@ class SendMediaGroup(BaseClient):
media=types.InputMediaUploadedDocument(
file=await self.save_file(i.media),
thumb=None if i.thumb is None else self.save_file(i.thumb),
mime_type="video/mp4",
mime_type=self.guess_mime_type(i.media) or "video/mp4",
attributes=[
types.DocumentAttributeVideo(
supports_streaming=i.supports_streaming or None,

View File

@ -104,7 +104,7 @@ class SendSticker(BaseClient):
if os.path.exists(sticker):
file = await self.save_file(sticker, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument(
mime_type="image/webp",
mime_type=self.guess_mime_type(sticker) or "image/webp",
file=file,
attributes=[
types.DocumentAttributeFilename(file_name=os.path.basename(sticker))

View File

@ -139,7 +139,7 @@ class SendVideo(BaseClient):
thumb = None if thumb is None else await self.save_file(thumb)
file = await self.save_file(video, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument(
mime_type="video/mp4",
mime_type=self.guess_mime_type(video) or "video/mp4",
file=file,
thumb=thumb,
attributes=[

View File

@ -120,7 +120,7 @@ class SendVideoNote(BaseClient):
thumb = None if thumb is None else await self.save_file(thumb)
file = await self.save_file(video_note, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument(
mime_type="video/mp4",
mime_type=self.guess_mime_type(video_note) or "video/mp4",
file=file,
thumb=thumb,
attributes=[

View File

@ -119,7 +119,7 @@ class SendVoice(BaseClient):
if os.path.exists(voice):
file = await self.save_file(voice, progress=progress, progress_args=progress_args)
media = types.InputMediaUploadedDocument(
mime_type="audio/mpeg",
mime_type=self.guess_mime_type(voice) or "audio/mpeg",
file=file,
attributes=[
types.DocumentAttributeAudio(

View File

@ -2729,19 +2729,17 @@ class Message(PyrogramType, Update):
Defaults to True.
Returns:
True on success.
True on success, False otherwise.
Raises:
:class:`RPCError <pyrogram.RPCError>`
"""
await self._client.delete_messages(
return await self._client.delete_messages(
chat_id=self.chat.id,
message_ids=self.message_id,
revoke=revoke
)
return True
async def click(self, x: int or str, y: int = None, quote: bool = None):
"""Bound method *click* of :obj:`Message <pyrogram.Message>`.

View File

@ -171,6 +171,9 @@ setup(
},
python_requires="~=3.4",
packages=find_packages(exclude=["compiler*"]),
package_data={
"pyrogram.client.ext": ["mime.types"]
},
zip_safe=False,
install_requires=read("requirements.txt"),
extras_require={