Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ebb2556

Browse files
committed
Fix download of URLs with UTF-8 chars in path
refs python-telegram-bot#650
1 parent 564d361 commit ebb2556

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
future>=0.15.2
1+
future>=0.16.0
22
certifi

telegram/files/file.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
# You should have received a copy of the GNU Lesser Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
1919
"""This module contains an object that represents a Telegram File."""
20-
2120
from os.path import basename
2221

22+
from future.backports.urllib import parse as urllib_parse
23+
2324
from telegram import TelegramObject
2425

2526

@@ -46,8 +47,7 @@ def __init__(self, file_id, bot, file_size=None, file_path=None, **kwargs):
4647

4748
# Optionals
4849
self.file_size = file_size
49-
if file_path:
50-
self.file_path = str(file_path)
50+
self.file_path = file_path
5151

5252
self.bot = bot
5353

@@ -91,7 +91,10 @@ def download(self, custom_path=None, out=None, timeout=None):
9191
if custom_path is not None and out is not None:
9292
raise ValueError('custom_path and out are mutually exclusive')
9393

94-
url = self.file_path
94+
# Convert any UTF-8 char into a url encoded ASCII string.
95+
sres = urllib_parse.urlsplit(self.file_path)
96+
url = urllib_parse.urlunsplit(urllib_parse.SplitResult(
97+
sres.scheme, sres.netloc, urllib_parse.quote(sres.path), sres.query, sres.fragment))
9598

9699
if out:
97100
buf = self.bot.request.retrieve(url)
@@ -101,6 +104,6 @@ def download(self, custom_path=None, out=None, timeout=None):
101104
if custom_path:
102105
filename = custom_path
103106
else:
104-
filename = basename(url)
107+
filename = basename(self.file_path)
105108

106109
self.bot.request.download(url, filename, timeout=timeout)

0 commit comments

Comments
 (0)