Skip to content

Commit bc6e2d3

Browse files
committed
Add .get_file() to Audio, Document, PhotoSize, Sticker, Video, VideoNote and Voice
1 parent 58674e8 commit bc6e2d3

File tree

8 files changed

+198
-12
lines changed

8 files changed

+198
-12
lines changed

telegram/bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ def get_file(self, file_or_file_id, timeout=None, **kwargs):
13121312
moment, bots can download files of up to 20MB in size. The file can then be downloaded
13131313
with :attr:`telegram.File.download`. It is guaranteed that the link will be
13141314
valid for at least 1 hour. When the link expires, a new one can be requested by
1315-
calling getFile again.
1315+
calling get_file again.
13161316
13171317
Args:
13181318
file_or_file_id (:obj:`str` | :class:`telegram.Audio` | :class:`telegram.Document` | :class:`telegram.PhotoSize` | :class:`telegram.Sticker` | :class:`telegram.Video` | :class:`telegram.VideoNote` | :class:`telegram.Voice`):

telegram/files/audio.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Audio(TelegramObject):
3232
title (:obj:`str`): Optional. Title of the audio as defined by sender or by audio tags.
3333
mime_type (:obj:`str`): Optional. MIME type of the file as defined by sender.
3434
file_size (:obj:`int`): Optional. File size.
35+
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.
3536
3637
Args:
3738
file_id (:obj:`str`): Unique identifier for this file.
@@ -41,6 +42,7 @@ class Audio(TelegramObject):
4142
title (:obj:`str`, optional): Title of the audio as defined by sender or by audio tags.
4243
mime_type (:obj:`str`, optional): MIME type of the file as defined by sender.
4344
file_size (:obj:`int`, optional): File size.
45+
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.
4446
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
4547
4648
"""
@@ -52,6 +54,7 @@ def __init__(self,
5254
title=None,
5355
mime_type=None,
5456
file_size=None,
57+
bot=None,
5558
**kwargs):
5659
# Required
5760
self.file_id = str(file_id)
@@ -61,6 +64,7 @@ def __init__(self,
6164
self.title = title
6265
self.mime_type = mime_type
6366
self.file_size = file_size
67+
self.bot = bot
6468

6569
self._id_attrs = (self.file_id,)
6670

@@ -69,4 +73,27 @@ def de_json(cls, data, bot):
6973
if not data:
7074
return None
7175

72-
return cls(**data)
76+
return cls(bot=bot, **data)
77+
78+
def get_file(self, timeout=None, **kwargs):
79+
"""
80+
Use this method to get basic info about the audio file and prepare it for downloading.
81+
For the moment, bots can download files of up to 20MB in size. The file can then be
82+
downloaded with :attr:`telegram.File.download`. It is guaranteed that the link will be
83+
valid for at least 1 hour. When the link expires, a new one can be requested by
84+
calling get_file again.
85+
86+
Args:
87+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
88+
the read timeout from the server (instead of the one specified during creation of
89+
the connection pool).
90+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
91+
92+
Returns:
93+
:class:`telegram.File`
94+
95+
Raises:
96+
:class:`telegram.TelegramError`
97+
98+
"""
99+
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)

telegram/files/document.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ class Document(TelegramObject):
3030
file_name (:obj:`str`): Original filename.
3131
mime_type (:obj:`str`): Optional. MIME type of the file.
3232
file_size (:obj:`int`): Optional. File size.
33+
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.
3334
3435
Args:
3536
file_id (:obj:`str`): Unique file identifier
3637
thumb (:class:`telegram.PhotoSize`, optional): Document thumbnail as defined by sender.
3738
file_name (:obj:`str`, optional): Original filename as defined by sender.
3839
mime_type (:obj:`str`, optional): MIME type of the file as defined by sender.
3940
file_size (:obj:`int`, optional): File size.
41+
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.
4042
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
4143
4244
"""
@@ -48,6 +50,7 @@ def __init__(self,
4850
file_name=None,
4951
mime_type=None,
5052
file_size=None,
53+
bot=None,
5154
**kwargs):
5255
# Required
5356
self.file_id = str(file_id)
@@ -56,6 +59,7 @@ def __init__(self,
5659
self.file_name = file_name
5760
self.mime_type = mime_type
5861
self.file_size = file_size
62+
self.bot = bot
5963

6064
self._id_attrs = (self.file_id,)
6165

@@ -68,4 +72,27 @@ def de_json(cls, data, bot):
6872

6973
data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)
7074

71-
return cls(**data)
75+
return cls(**data, bot=bot)
76+
77+
def get_file(self, timeout=None, **kwargs):
78+
"""
79+
Use this method to get basic info about the document and prepare it for downloading. For
80+
the moment, bots can download files of up to 20MB in size. The file can then be downloaded
81+
with :attr:`telegram.File.download`. It is guaranteed that the link will be
82+
valid for at least 1 hour. When the link expires, a new one can be requested by
83+
calling get_file again.
84+
85+
Args:
86+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
87+
the read timeout from the server (instead of the one specified during creation of
88+
the connection pool).
89+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
90+
91+
Returns:
92+
:class:`telegram.File`
93+
94+
Raises:
95+
:class:`telegram.TelegramError`
96+
97+
"""
98+
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)

telegram/files/photosize.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,26 @@ class PhotoSize(TelegramObject):
2929
width (:obj:`int`): Photo width.
3030
height (:obj:`int`): Photo height.
3131
file_size (:obj:`int`): Optional. File size.
32+
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.
3233
3334
Args:
3435
file_id (:obj:`str`): Unique identifier for this file.
3536
width (:obj:`int`): Photo width.
3637
height (:obj:`int`): Photo height.
3738
file_size (:obj:`int`, optional): File size.
39+
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.
3840
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
3941
4042
"""
4143

42-
def __init__(self, file_id, width, height, file_size=None, **kwargs):
44+
def __init__(self, file_id, width, height, file_size=None, bot=None, **kwargs):
4345
# Required
4446
self.file_id = str(file_id)
4547
self.width = int(width)
4648
self.height = int(height)
4749
# Optionals
4850
self.file_size = file_size
51+
self.bot = bot
4952

5053
self._id_attrs = (self.file_id,)
5154

@@ -54,7 +57,7 @@ def de_json(cls, data, bot):
5457
if not data:
5558
return None
5659

57-
return cls(**data)
60+
return cls(bot=bot, **data)
5861

5962
@classmethod
6063
def de_list(cls, data, bot):
@@ -66,3 +69,26 @@ def de_list(cls, data, bot):
6669
photos.append(cls.de_json(photo, bot))
6770

6871
return photos
72+
73+
def get_file(self, timeout=None, **kwargs):
74+
"""
75+
Use this method to get basic info about the photo (only one size) and prepare it for
76+
downloading. For the moment, bots can download files of up to 20MB in size. The file can
77+
then be downloaded with :attr:`telegram.File.download`. It is guaranteed that the link
78+
will be valid for at least 1 hour. When the link expires, a new one can be requested by
79+
calling get_file again.
80+
81+
Args:
82+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
83+
the read timeout from the server (instead of the one specified during creation of
84+
the connection pool).
85+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
86+
87+
Returns:
88+
:class:`telegram.File`
89+
90+
Raises:
91+
:class:`telegram.TelegramError`
92+
93+
"""
94+
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)

telegram/files/sticker.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Sticker(TelegramObject):
3535
mask_position (:class:`telegram.MaskPosition`): Optional. For mask stickers, the position
3636
where the mask should be placed.
3737
file_size (:obj:`int`): Optional. File size.
38+
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.
3839
3940
Args:
4041
file_id (:obj:`str`): Unique identifier for this file.
@@ -48,7 +49,8 @@ class Sticker(TelegramObject):
4849
mask_position (:class:`telegram.MaskPosition`, optional): For mask stickers, the
4950
position where the mask should be placed.
5051
file_size (:obj:`int`, optional): File size.
51-
**kwargs (obj:`dict`): Arbitrary keyword arguments.
52+
**kwargs (obj:`dict`): Arbitrary keyword arguments.7
53+
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.
5254
5355
"""
5456

@@ -61,6 +63,7 @@ def __init__(self,
6163
file_size=None,
6264
set_name=None,
6365
mask_position=None,
66+
bot=None,
6467
**kwargs):
6568
# Required
6669
self.file_id = str(file_id)
@@ -72,6 +75,7 @@ def __init__(self,
7275
self.file_size = file_size
7376
self.set_name = set_name
7477
self.mask_position = mask_position
78+
self.bot = bot
7579

7680
self._id_attrs = (self.file_id,)
7781

@@ -85,7 +89,7 @@ def de_json(cls, data, bot):
8589
data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)
8690
data['mask_position'] = MaskPosition.de_json(data.get('mask_position'), bot)
8791

88-
return cls(**data)
92+
return cls(bot=bot, **data)
8993

9094
@classmethod
9195
def de_list(cls, data, bot):
@@ -94,6 +98,29 @@ def de_list(cls, data, bot):
9498

9599
return [cls.de_json(d, bot) for d in data]
96100

101+
def get_file(self, timeout=None, **kwargs):
102+
"""
103+
Use this method to get basic info about the sticker file and prepare it for downloading.
104+
For the moment, bots can download files of up to 20MB in size. The file can then be
105+
downloaded with :attr:`telegram.File.download`. It is guaranteed that the link will be
106+
valid for at least 1 hour. When the link expires, a new one can be requested by
107+
calling get_file again.
108+
109+
Args:
110+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
111+
the read timeout from the server (instead of the one specified during creation of
112+
the connection pool).
113+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
114+
115+
Returns:
116+
:class:`telegram.File`
117+
118+
Raises:
119+
:class:`telegram.TelegramError`
120+
121+
"""
122+
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)
123+
97124

98125
class StickerSet(TelegramObject):
99126
"""This object represents a sticker set.

telegram/files/video.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Video(TelegramObject):
3232
thumb (:class:`telegram.PhotoSize`): Optional. Video thumbnail.
3333
mime_type (:obj:`str`): Optional. Mime type of a file as defined by sender.
3434
file_size (:obj:`int`): Optional. File size.
35+
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.
3536
3637
Args:
3738
file_id (:obj:`str`): Unique identifier for this file.
@@ -41,6 +42,7 @@ class Video(TelegramObject):
4142
thumb (:class:`telegram.PhotoSize`, optional): Video thumbnail.
4243
mime_type (:obj:`str`, optional): Mime type of a file as defined by sender.
4344
file_size (:obj:`int`, optional): File size.
45+
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.
4446
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
4547
4648
"""
@@ -53,6 +55,7 @@ def __init__(self,
5355
thumb=None,
5456
mime_type=None,
5557
file_size=None,
58+
bot=None,
5659
**kwargs):
5760
# Required
5861
self.file_id = str(file_id)
@@ -63,6 +66,7 @@ def __init__(self,
6366
self.thumb = thumb
6467
self.mime_type = mime_type
6568
self.file_size = file_size
69+
self.bot = bot
6670

6771
self._id_attrs = (self.file_id,)
6872

@@ -75,4 +79,27 @@ def de_json(cls, data, bot):
7579

7680
data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)
7781

78-
return cls(**data)
82+
return cls(bot=bot, **data)
83+
84+
def get_file(self, timeout=None, **kwargs):
85+
"""
86+
Use this method to get basic info about the video file and prepare it for downloading.
87+
For the moment, bots can download files of up to 20MB in size. The file can then be
88+
downloaded with :attr:`telegram.File.download`. It is guaranteed that the link will be
89+
valid for at least 1 hour. When the link expires, a new one can be requested by
90+
calling get_file again.
91+
92+
Args:
93+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
94+
the read timeout from the server (instead of the one specified during creation of
95+
the connection pool).
96+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
97+
98+
Returns:
99+
:class:`telegram.File`
100+
101+
Raises:
102+
:class:`telegram.TelegramError`
103+
104+
"""
105+
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)

telegram/files/videonote.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,28 @@ class VideoNote(TelegramObject):
3030
duration (:obj:`int`): Duration of the video in seconds as defined by sender.
3131
thumb (:class:`telegram.PhotoSize`): Optional. Video thumbnail.
3232
file_size (:obj:`int`): Optional. File size.
33+
bot (:class:`telegram.Bot`): Optional. The Bot to use for instance methods.
3334
3435
Args:
3536
file_id (:obj:`str`): Unique identifier for this file.
3637
length (:obj:`int`): Video width and height as defined by sender.
3738
duration (:obj:`int`): Duration of the video in seconds as defined by sender.
3839
thumb (:class:`telegram.PhotoSize`, optional): Video thumbnail.
3940
file_size (:obj:`int`, optional): File size.
41+
bot (:class:`telegram.Bot`, optional): The Bot to use for instance methods.
4042
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
4143
4244
"""
4345

44-
def __init__(self, file_id, length, duration, thumb=None, file_size=None, **kwargs):
46+
def __init__(self, file_id, length, duration, thumb=None, file_size=None, bot=None, **kwargs):
4547
# Required
4648
self.file_id = str(file_id)
4749
self.length = int(length)
4850
self.duration = int(duration)
4951
# Optionals
5052
self.thumb = thumb
5153
self.file_size = file_size
54+
self.bot = bot
5255

5356
self._id_attrs = (self.file_id,)
5457

@@ -61,4 +64,27 @@ def de_json(cls, data, bot):
6164

6265
data['thumb'] = PhotoSize.de_json(data.get('thumb'), bot)
6366

64-
return cls(**data)
67+
return cls(bot=bot, **data)
68+
69+
def get_file(self, timeout=None, **kwargs):
70+
"""
71+
Use this method to get basic info about the video message file and prepare it for
72+
downloading. For the moment, bots can download files of up to 20MB in size. The file can
73+
then be downloaded with :attr:`telegram.File.download`. It is guaranteed that the link
74+
will be valid for at least 1 hour. When the link expires, a new one can be requested by
75+
calling get_file again.
76+
77+
Args:
78+
timeout (:obj:`int` | :obj:`float`, optional): If this value is specified, use it as
79+
the read timeout from the server (instead of the one specified during creation of
80+
the connection pool).
81+
**kwargs (:obj:`dict`): Arbitrary keyword arguments.
82+
83+
Returns:
84+
:class:`telegram.File`
85+
86+
Raises:
87+
:class:`telegram.TelegramError`
88+
89+
"""
90+
return self.bot.get_file(self.file_id, timeout=timeout, **kwargs)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy