From 574b57b54fb525e711f0515fd03959975a1e9ab9 Mon Sep 17 00:00:00 2001 From: OctoNezd Date: Sun, 6 Nov 2016 06:42:44 -0500 Subject: [PATCH 1/7] DownBytes added --- telegram/file.py | 4 ++++ telegram/utils/request.py | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/telegram/file.py b/telegram/file.py index c9c17b77c1e..20973636534 100644 --- a/telegram/file.py +++ b/telegram/file.py @@ -79,3 +79,7 @@ def download(self, custom_path=None): filename = basename(url) self.bot.request.download(url, filename) + + def downbyte(self): + url = self.file_path + return self.bot.request.downbyte(url) diff --git a/telegram/utils/request.py b/telegram/utils/request.py index 0aa071c84d6..f9ef21d8318 100644 --- a/telegram/utils/request.py +++ b/telegram/utils/request.py @@ -219,3 +219,11 @@ def download(self, url, filename): buf = self._request_wrapper('GET', url) with open(filename, 'wb') as fobj: fobj.write(buf) + + def downbyte(self, url): + """Download a file by its URL to bytes. + Args: + url: + The web location we want to retrieve. + """ + return self._request_wrapper('GET', url) From 3d5c4f0a00e48c6304001e4f10850c7ce86130e5 Mon Sep 17 00:00:00 2001 From: OctoNezd Date: Sun, 6 Nov 2016 06:52:11 -0500 Subject: [PATCH 2/7] File.downbyte changed --- telegram/file.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/telegram/file.py b/telegram/file.py index 20973636534..551352f5076 100644 --- a/telegram/file.py +++ b/telegram/file.py @@ -80,6 +80,11 @@ def download(self, custom_path=None): self.bot.request.download(url, filename) - def downbyte(self): + def downbyte(self, file): + """ + Args: + file (func):a file-like object. Must be opened in wb mode. + """ url = self.file_path - return self.bot.request.downbyte(url) + buf = self.bot.request.downbyte(url) + file.write(buf) From 315e12d86d3cd7476f263ebe88eef551e282656c Mon Sep 17 00:00:00 2001 From: OctoNezd Date: Sun, 6 Nov 2016 07:12:19 -0500 Subject: [PATCH 3/7] Changed file.download();Remove downbyte() --- telegram/file.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/telegram/file.py b/telegram/file.py index 551352f5076..ac56470b9eb 100644 --- a/telegram/file.py +++ b/telegram/file.py @@ -65,10 +65,11 @@ def de_json(data, bot): return File(bot=bot, **data) - def download(self, custom_path=None): + def download(self, custom_path=None, out=None): """ Args: - custom_path (str): + custom_path (str): Custom path. Will be ignored out is not None. + out(func): An file-like function. Must be in wb. """ url = self.file_path @@ -77,14 +78,9 @@ def download(self, custom_path=None): filename = custom_path else: filename = basename(url) - - self.bot.request.download(url, filename) - - def downbyte(self, file): - """ - Args: - file (func):a file-like object. Must be opened in wb mode. - """ - url = self.file_path - buf = self.bot.request.downbyte(url) - file.write(buf) + if out: + url = self.file_path + buf = self.bot.request.downbyte(url) + out.write(buf) + else: + self.bot.request.download(url, filename) From b233c5b840621533f0f22d27e71a506bd6a91fdb Mon Sep 17 00:00:00 2001 From: Yan Date: Sun, 6 Nov 2016 15:26:26 +0300 Subject: [PATCH 4/7] Fixed typo --- telegram/file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telegram/file.py b/telegram/file.py index ac56470b9eb..905ad6daffc 100644 --- a/telegram/file.py +++ b/telegram/file.py @@ -68,7 +68,7 @@ def de_json(data, bot): def download(self, custom_path=None, out=None): """ Args: - custom_path (str): Custom path. Will be ignored out is not None. + custom_path (str): Custom path. Will be ignored if out is not None. out(func): An file-like function. Must be in wb. """ From 00d7a14d5e9794069b70ccc42f90fbc4694015e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Tue, 8 Nov 2016 21:08:10 +0100 Subject: [PATCH 5/7] add docstring, make custom_path and out mutually exclusive, rename downbytes to retrieve --- telegram/file.py | 34 ++++++++++++++++++++++++---------- telegram/utils/request.py | 17 +++++++++-------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/telegram/file.py b/telegram/file.py index 905ad6daffc..cffe6c57271 100644 --- a/telegram/file.py +++ b/telegram/file.py @@ -67,20 +67,34 @@ def de_json(data, bot): def download(self, custom_path=None, out=None): """ - Args: - custom_path (str): Custom path. Will be ignored if out is not None. - out(func): An file-like function. Must be in wb. - + Download this file. By default, the file is saved in the current working directory with its + original filename as reported by Telegram. If a ``custom_path`` is supplied, it will be + saved to that path instead. If ``out`` is defined, the file contents will be saved to that + object using the ``out.write`` method. ``custom_path`` and ``out`` are mutually exclusive. + + Keyword Args: + custom_path (Optional[str]): Custom path. + out (Optional[object]): A file-like object. Must be opened in binary mode, if + applicable. + + Raises: + ValueError: If both ``custom_path`` and ``out`` are passed. """ + + if custom_path is not None and out is not None: + raise ValueError('custom_path and out are mutually exclusive') + url = self.file_path - if custom_path: - filename = custom_path - else: - filename = basename(url) if out: - url = self.file_path - buf = self.bot.request.downbyte(url) + buf = self.bot.request.retrieve(url) out.write(buf) + else: + if custom_path: + filename = custom_path + else: + filename = basename(url) + self.bot.request.download(url, filename) + diff --git a/telegram/utils/request.py b/telegram/utils/request.py index f9ef21d8318..6bf8b6b9940 100644 --- a/telegram/utils/request.py +++ b/telegram/utils/request.py @@ -206,6 +206,14 @@ def post(self, url, data, timeout=None): return self._parse(result) + def retrieve(self, url): + """Retrieve the contents of a file by its URL. + Args: + url: + The web location we want to retrieve. + """ + return self._request_wrapper('GET', url) + def download(self, url, filename): """Download a file by its URL. Args: @@ -216,14 +224,7 @@ def download(self, url, filename): The filename within the path to download the file. """ - buf = self._request_wrapper('GET', url) + buf = self.retrieve(url) with open(filename, 'wb') as fobj: fobj.write(buf) - def downbyte(self, url): - """Download a file by its URL to bytes. - Args: - url: - The web location we want to retrieve. - """ - return self._request_wrapper('GET', url) From c6018d12c4d4d47846a2273ca98937c928912fe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Wed, 9 Nov 2016 00:08:24 +0100 Subject: [PATCH 6/7] remove trailing whitespace --- telegram/file.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/telegram/file.py b/telegram/file.py index cffe6c57271..959212cf653 100644 --- a/telegram/file.py +++ b/telegram/file.py @@ -74,16 +74,16 @@ def download(self, custom_path=None, out=None): Keyword Args: custom_path (Optional[str]): Custom path. - out (Optional[object]): A file-like object. Must be opened in binary mode, if + out (Optional[object]): A file-like object. Must be opened in binary mode, if applicable. Raises: ValueError: If both ``custom_path`` and ``out`` are passed. """ - + if custom_path is not None and out is not None: raise ValueError('custom_path and out are mutually exclusive') - + url = self.file_path if out: From 8167bfc2664f5a3385e7cf9d7756cf9d7a43cb99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sun, 18 Dec 2016 02:43:46 +0100 Subject: [PATCH 7/7] run pre-commit hooks --- telegram/file.py | 1 - telegram/utils/request.py | 1 - 2 files changed, 2 deletions(-) diff --git a/telegram/file.py b/telegram/file.py index 959212cf653..e0000d440fd 100644 --- a/telegram/file.py +++ b/telegram/file.py @@ -97,4 +97,3 @@ def download(self, custom_path=None, out=None): filename = basename(url) self.bot.request.download(url, filename) - diff --git a/telegram/utils/request.py b/telegram/utils/request.py index 6bf8b6b9940..3434b352244 100644 --- a/telegram/utils/request.py +++ b/telegram/utils/request.py @@ -227,4 +227,3 @@ def download(self, url, filename): buf = self.retrieve(url) with open(filename, 'wb') as fobj: fobj.write(buf) - 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