aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/utils.py
diff options
context:
space:
mode:
authorSergey M․ <dstftw@gmail.com>2014-09-13 20:59:16 +0700
committerSergey M․ <dstftw@gmail.com>2014-09-13 20:59:16 +0700
commitd05cfe06006c4a44032e95dde047d5e12be8674c (patch)
treec22a5e7c048e7dbef1155beb74fd2f51154c64f1 /youtube_dl/utils.py
parent37419b4f9937f11ed3ca3545a32ed3451eb734ee (diff)
downloadyoutube-dl-d05cfe06006c4a44032e95dde047d5e12be8674c.tar.xz
[YoutubeDL/utils] Clarify rationale for URL escaping in comment, move escape routines to utils and add some tests
Diffstat (limited to 'youtube_dl/utils.py')
-rw-r--r--youtube_dl/utils.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py
index d920c65a4..9124c3621 100644
--- a/youtube_dl/utils.py
+++ b/youtube_dl/utils.py
@@ -1418,6 +1418,24 @@ def uppercase_escape(s):
lambda m: unicode_escape(m.group(0))[0],
s)
+
+def escape_rfc3986(s):
+ """Escape non-ASCII characters as suggested by RFC 3986"""
+ if sys.version_info < (3, 0) and isinstance(s, unicode):
+ s = s.encode('utf-8')
+ return compat_urllib_parse.quote(s, "%/;:@&=+$,!~*'()?#[]") #"%/;:@&=+$,!~*'()?#[]+" #?#[]+
+
+
+def escape_url(url):
+ """Escape URL as suggested by RFC 3986"""
+ url_parsed = compat_urllib_parse_urlparse(url)
+ return url_parsed._replace(
+ path=escape_rfc3986(url_parsed.path),
+ params=escape_rfc3986(url_parsed.params),
+ query=escape_rfc3986(url_parsed.query),
+ fragment=escape_rfc3986(url_parsed.fragment)
+ ).geturl()
+
try:
struct.pack(u'!I', 0)
except TypeError: