diff options
author | James Taylor <user234683@users.noreply.github.com> | 2018-12-23 02:16:15 -0800 |
---|---|---|
committer | James Taylor <user234683@users.noreply.github.com> | 2018-12-23 02:16:15 -0800 |
commit | b1fff10065a8bb4e3959ad2469527f502940de9b (patch) | |
tree | 518b576d49322271120ec089b42e534e65678dce /youtube/common.py | |
parent | 6217e6f558cb19fb756d6549e218008e76d4d29f (diff) | |
download | yt-local-b1fff10065a8bb4e3959ad2469527f502940de9b.tar.lz yt-local-b1fff10065a8bb4e3959ad2469527f502940de9b.tar.xz yt-local-b1fff10065a8bb4e3959ad2469527f502940de9b.zip |
common: support for POST data in fetch_url
Diffstat (limited to 'youtube/common.py')
-rw-r--r-- | youtube/common.py | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/youtube/common.py b/youtube/common.py index f534d84..cef1620 100644 --- a/youtube/common.py +++ b/youtube/common.py @@ -148,20 +148,19 @@ def decode_content(content, encoding_header): content = gzip.decompress(content) return content -def fetch_url(url, headers=(), timeout=15, report_text=None): - if isinstance(headers, list): - headers += [('Accept-Encoding', 'gzip, br')] - headers = dict(headers) - elif isinstance(headers, tuple): - headers += (('Accept-Encoding', 'gzip, br'),) - headers = dict(headers) - else: - headers = headers.copy() - headers['Accept-Encoding'] = 'gzip, br' +def fetch_url(url, headers=(), timeout=15, report_text=None, data=None): + headers = dict(headers) # Note: Calling dict() on a dict will make a copy + headers['Accept-Encoding'] = 'gzip, br' + if data is not None: + if isinstance(data, str): + data = data.encode('ascii') + elif not isinstance(data, bytes): + data = urllib.parse.urlencode(data).encode('ascii') + start_time = time.time() - req = urllib.request.Request(url, headers=headers) + req = urllib.request.Request(url, data=data, headers=headers) response = urllib.request.urlopen(req, timeout=timeout) response_time = time.time() |