From c65e210c0301342131fe164ec08c3b1a0033a204 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Fri, 7 Jun 2019 18:48:34 +0200 Subject: [PATCH] Fix FileData namedtuple using Python 3.7+ features --- pyrogram/client/client.py | 10 ++--- pyrogram/client/ext/__init__.py | 2 + pyrogram/client/ext/base_client.py | 5 --- pyrogram/client/ext/file_data.py | 38 +++++++++++++++++++ .../client/methods/messages/download_media.py | 20 +++++----- 5 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 pyrogram/client/ext/file_data.py diff --git a/pyrogram/client/client.py b/pyrogram/client/client.py index 08a3072e..6dc8ce97 100644 --- a/pyrogram/client/client.py +++ b/pyrogram/client/client.py @@ -877,7 +877,7 @@ class Client(Methods, BaseClient): temp_file_path = self.get_file( media_type=data.media_type, dc_id=data.dc_id, - file_id=data.file_id, + document_id=data.document_id, access_hash=data.access_hash, thumb_size=data.thumb_size, peer_id=data.peer_id, @@ -1520,7 +1520,7 @@ class Client(Methods, BaseClient): self, media_type: int, dc_id: int, - file_id: int, + document_id: int, access_hash: int, thumb_size: str, peer_id: int, @@ -1580,21 +1580,21 @@ class Client(Methods, BaseClient): ) elif media_type in (0, 2): location = types.InputPhotoFileLocation( - id=file_id, + id=document_id, access_hash=access_hash, file_reference=b"", thumb_size=thumb_size ) elif media_type == 14: location = types.InputDocumentFileLocation( - id=file_id, + id=document_id, access_hash=access_hash, file_reference=b"", thumb_size=thumb_size ) else: location = types.InputDocumentFileLocation( - id=file_id, + id=document_id, access_hash=access_hash, file_reference=b"", thumb_size="" diff --git a/pyrogram/client/ext/__init__.py b/pyrogram/client/ext/__init__.py index 917c9e62..58897c55 100644 --- a/pyrogram/client/ext/__init__.py +++ b/pyrogram/client/ext/__init__.py @@ -20,3 +20,5 @@ from .base_client import BaseClient from .dispatcher import Dispatcher from .emoji import Emoji from .syncer import Syncer +from .file_data import FileData + diff --git a/pyrogram/client/ext/base_client.py b/pyrogram/client/ext/base_client.py index 9c0d661e..9e7cd677 100644 --- a/pyrogram/client/ext/base_client.py +++ b/pyrogram/client/ext/base_client.py @@ -19,7 +19,6 @@ import os import platform import re -from collections import namedtuple from queue import Queue from threading import Lock @@ -84,10 +83,6 @@ class BaseClient: mime_types_to_extensions[mime_type] = " ".join(extensions) - fields = ("media_type", "dc_id", "file_id", "access_hash", "thumb_size", "peer_id", "volume_id", "local_id", - "is_big", "file_size", "mime_type", "file_name", "date") - FileData = namedtuple("FileData", fields, defaults=(None,) * len(fields)) - def __init__(self): self.is_bot = None self.dc_id = None diff --git a/pyrogram/client/ext/file_data.py b/pyrogram/client/ext/file_data.py new file mode 100644 index 00000000..9a19cd5d --- /dev/null +++ b/pyrogram/client/ext/file_data.py @@ -0,0 +1,38 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2019 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + + +class FileData: + def __init__( + self, *, media_type: int = None, dc_id: int = None, document_id: int = None, access_hash: int = None, + thumb_size: str = None, peer_id: int = None, volume_id: int = None, local_id: int = None, is_big: bool = None, + file_size: int = None, mime_type: str = None, file_name: str = None, date: int = None + ): + self.media_type = media_type + self.dc_id = dc_id + self.document_id = document_id + self.access_hash = access_hash + self.thumb_size = thumb_size + self.peer_id = peer_id + self.volume_id = volume_id + self.local_id = local_id + self.is_big = is_big + self.file_size = file_size + self.mime_type = mime_type + self.file_name = file_name + self.date = date diff --git a/pyrogram/client/methods/messages/download_media.py b/pyrogram/client/methods/messages/download_media.py index 88c9c2d9..d822a88b 100644 --- a/pyrogram/client/methods/messages/download_media.py +++ b/pyrogram/client/methods/messages/download_media.py @@ -22,7 +22,7 @@ from threading import Event from typing import Union import pyrogram -from pyrogram.client.ext import BaseClient, utils +from pyrogram.client.ext import BaseClient, FileData, utils from pyrogram.errors import FileIdInvalid @@ -110,7 +110,7 @@ class DownloadMedia(BaseClient): mime_type = getattr(media, "mime_type", None) date = getattr(media, "date", None) - data = self.FileData( + data = FileData( file_name=file_name, file_size=file_size, mime_type=mime_type, @@ -118,7 +118,7 @@ class DownloadMedia(BaseClient): ) def get_existing_attributes() -> dict: - return dict(filter(lambda x: x[1] is not None, data._asdict().items())) + return dict(filter(lambda x: x[1] is not None, data.__dict__.items())) try: decoded = utils.decode(file_id_str) @@ -128,7 +128,7 @@ class DownloadMedia(BaseClient): unpacked = struct.unpack("