Skip to content

Commit c65e210

Browse files
committed
Fix FileData namedtuple using Python 3.7+ features
1 parent 89e6f41 commit c65e210

5 files changed

Lines changed: 55 additions & 20 deletions

File tree

pyrogram/client/client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ def download_worker(self):
877877
temp_file_path = self.get_file(
878878
media_type=data.media_type,
879879
dc_id=data.dc_id,
880-
file_id=data.file_id,
880+
document_id=data.document_id,
881881
access_hash=data.access_hash,
882882
thumb_size=data.thumb_size,
883883
peer_id=data.peer_id,
@@ -1520,7 +1520,7 @@ def get_file(
15201520
self,
15211521
media_type: int,
15221522
dc_id: int,
1523-
file_id: int,
1523+
document_id: int,
15241524
access_hash: int,
15251525
thumb_size: str,
15261526
peer_id: int,
@@ -1580,21 +1580,21 @@ def get_file(
15801580
)
15811581
elif media_type in (0, 2):
15821582
location = types.InputPhotoFileLocation(
1583-
id=file_id,
1583+
id=document_id,
15841584
access_hash=access_hash,
15851585
file_reference=b"",
15861586
thumb_size=thumb_size
15871587
)
15881588
elif media_type == 14:
15891589
location = types.InputDocumentFileLocation(
1590-
id=file_id,
1590+
id=document_id,
15911591
access_hash=access_hash,
15921592
file_reference=b"",
15931593
thumb_size=thumb_size
15941594
)
15951595
else:
15961596
location = types.InputDocumentFileLocation(
1597-
id=file_id,
1597+
id=document_id,
15981598
access_hash=access_hash,
15991599
file_reference=b"",
16001600
thumb_size=""

pyrogram/client/ext/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@
2020
from .dispatcher import Dispatcher
2121
from .emoji import Emoji
2222
from .syncer import Syncer
23+
from .file_data import FileData
24+

pyrogram/client/ext/base_client.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import os
2020
import platform
2121
import re
22-
from collections import namedtuple
2322
from queue import Queue
2423
from threading import Lock
2524

@@ -84,10 +83,6 @@ class StopTransmission(StopIteration):
8483

8584
mime_types_to_extensions[mime_type] = " ".join(extensions)
8685

87-
fields = ("media_type", "dc_id", "file_id", "access_hash", "thumb_size", "peer_id", "volume_id", "local_id",
88-
"is_big", "file_size", "mime_type", "file_name", "date")
89-
FileData = namedtuple("FileData", fields, defaults=(None,) * len(fields))
90-
9186
def __init__(self):
9287
self.is_bot = None
9388
self.dc_id = None

pyrogram/client/ext/file_data.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
20+
class FileData:
21+
def __init__(
22+
self, *, media_type: int = None, dc_id: int = None, document_id: int = None, access_hash: int = None,
23+
thumb_size: str = None, peer_id: int = None, volume_id: int = None, local_id: int = None, is_big: bool = None,
24+
file_size: int = None, mime_type: str = None, file_name: str = None, date: int = None
25+
):
26+
self.media_type = media_type
27+
self.dc_id = dc_id
28+
self.document_id = document_id
29+
self.access_hash = access_hash
30+
self.thumb_size = thumb_size
31+
self.peer_id = peer_id
32+
self.volume_id = volume_id
33+
self.local_id = local_id
34+
self.is_big = is_big
35+
self.file_size = file_size
36+
self.mime_type = mime_type
37+
self.file_name = file_name
38+
self.date = date

pyrogram/client/methods/messages/download_media.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from typing import Union
2323

2424
import pyrogram
25-
from pyrogram.client.ext import BaseClient, utils
25+
from pyrogram.client.ext import BaseClient, FileData, utils
2626
from pyrogram.errors import FileIdInvalid
2727

2828

@@ -110,15 +110,15 @@ def download_media(
110110
mime_type = getattr(media, "mime_type", None)
111111
date = getattr(media, "date", None)
112112

113-
data = self.FileData(
113+
data = FileData(
114114
file_name=file_name,
115115
file_size=file_size,
116116
mime_type=mime_type,
117117
date=date
118118
)
119119

120120
def get_existing_attributes() -> dict:
121-
return dict(filter(lambda x: x[1] is not None, data._asdict().items()))
121+
return dict(filter(lambda x: x[1] is not None, data.__dict__.items()))
122122

123123
try:
124124
decoded = utils.decode(file_id_str)
@@ -128,7 +128,7 @@ def get_existing_attributes() -> dict:
128128
unpacked = struct.unpack("<iiqqib", decoded)
129129
dc_id, peer_id, volume_id, local_id, is_big = unpacked[1:]
130130

131-
data = self.FileData(
131+
data = FileData(
132132
**get_existing_attributes(),
133133
media_type=media_type,
134134
dc_id=dc_id,
@@ -139,25 +139,25 @@ def get_existing_attributes() -> dict:
139139
)
140140
elif media_type in (0, 2, 14):
141141
unpacked = struct.unpack("<iiqqc", decoded)
142-
dc_id, file_id, access_hash, thumb_size = unpacked[1:]
142+
dc_id, document_id, access_hash, thumb_size = unpacked[1:]
143143

144-
data = self.FileData(
144+
data = FileData(
145145
**get_existing_attributes(),
146146
media_type=media_type,
147147
dc_id=dc_id,
148-
file_id=file_id,
148+
document_id=document_id,
149149
access_hash=access_hash,
150150
thumb_size=thumb_size.decode()
151151
)
152152
elif media_type in (3, 4, 5, 8, 9, 10, 13):
153153
unpacked = struct.unpack("<iiqq", decoded)
154-
dc_id, file_id, access_hash = unpacked[1:]
154+
dc_id, document_id, access_hash = unpacked[1:]
155155

156-
data = self.FileData(
156+
data = FileData(
157157
**get_existing_attributes(),
158158
media_type=media_type,
159159
dc_id=dc_id,
160-
file_id=file_id,
160+
document_id=document_id,
161161
access_hash=access_hash
162162
)
163163
else:

0 commit comments

Comments
 (0)