Skip to content

Commit 9d2c1ca

Browse files
committed
switched from urllib to requests
1 parent 42546f7 commit 9d2c1ca

File tree

5 files changed

+44
-42
lines changed

5 files changed

+44
-42
lines changed

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ package_dir=
2929
=src
3030
packages=ffmpeg_downloader
3131
install_requires =
32+
requests
3233
tqdm
3334
# patool;platform_system!='Linux'
3435
# pylzma

src/ffmpeg_downloader/_download_helper.py

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,38 @@
1-
from urllib import request
2-
from contextlib import contextmanager
3-
import os, stat, ssl
1+
import os, stat
2+
import requests
43

5-
ctx = ssl.create_default_context()
6-
ctx.check_hostname = False
7-
ctx.verify_mode = ssl.CERT_NONE
84

9-
10-
@contextmanager
11-
def download_base(url, content_type, timeout=None):
12-
with request.urlopen(url, timeout=timeout or 5.0, context=ctx) as response:
13-
# pprint(response.headers.get_content_type())
14-
if response.headers.get_content_type() != content_type:
15-
raise RuntimeError(f'"{url}" is not the expected content type.')
16-
try:
17-
nbytes = int(response.getheader("content-length"))
18-
except:
19-
nbytes = 0
20-
yield response, nbytes
5+
def download_base(url, content_type, timeout=None, stream=True):
6+
response = requests.get(url, timeout=timeout, stream=stream)
7+
if response.headers.get("content-type", "text/plain") != content_type:
8+
raise RuntimeError(f'"{url}" is not the expected content type.')
9+
return response
2110

2211

2312
def download_info(url, content_type, timeout=None):
24-
with download_base(url, content_type, timeout) as (response, nbytes):
25-
info = response.read().decode("utf-8")
26-
return info
13+
response = download_base(url, content_type, timeout, stream=False)
14+
return response.text
2715

2816

2917
def download_file(outfile, url, content_type, progress=None, timeout=None):
3018

31-
with download_base(url, content_type, timeout) as (response, nbytes):
32-
if progress:
33-
progress(0, nbytes)
34-
35-
blksz = nbytes // 32 or 1024 * 1024
36-
with open(outfile, "wb") as f:
37-
nread = 0
38-
while True:
39-
b = response.read(blksz)
40-
if not b:
41-
break
42-
f.write(b)
43-
nread += len(b)
44-
if progress:
45-
progress(nread, nbytes)
19+
response = download_base(url, content_type, timeout)
20+
21+
nbytes = int(response.headers["Content-Length"])
22+
23+
if progress:
24+
progress(0, nbytes)
25+
26+
blksz = nbytes // 32 or 1024 * 1024
27+
with open(outfile, "wb") as f:
28+
nread = 0
29+
for b in response.iter_content(chunk_size=blksz):
30+
if not b:
31+
break
32+
f.write(b)
33+
nread += len(b)
34+
if progress:
35+
progress(nread, nbytes)
4636

4737
return nbytes
4838

src/ffmpeg_downloader/_linux.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from tempfile import TemporaryDirectory
22
from ._download_helper import download_info, download_file, chmod
3-
import re, ssl, tarfile, os, shutil
3+
import re, tarfile, os, shutil
44
from os import path
55

66
home_url = "https://johnvansickle.com/ffmpeg"

src/ffmpeg_downloader/_macos.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ def download_n_install(install_dir, progress=None):
1717
ntotal = 0
1818

1919
def get_nbytes(cmd):
20-
with download_base(f"{home_url}/getrelease/{cmd}/zip", "application/zip") as (
21-
_,
22-
nbytes,
23-
):
24-
return nbytes
20+
response = download_base(f"{home_url}/getrelease/{cmd}/zip", "application/zip")
21+
nbytes = int(response.headers["content-length"])
22+
response.close()
23+
return nbytes
2524

2625
nfiles = [get_nbytes(cmd) for cmd in ("ffmpeg", "ffprobe")]
2726
ntotal = sum(nfiles)

tests/_test_downloads.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from ffmpeg_downloader import _win32, _macos, _linux
2+
import tempfile,os
3+
4+
with tempfile.TemporaryDirectory() as dir:
5+
6+
for mod in (_win32,_macos,_linux):
7+
mname = mod.__name__
8+
dstdir = os.path.join(dir,mname)
9+
os.mkdir(dstdir)
10+
print(dstdir)
11+
print(mod.get_version())
12+
mod.download_n_install(dstdir)

0 commit comments

Comments
 (0)