From eb5b04393b2b65de12560ebfaa2599e92f469324 Mon Sep 17 00:00:00 2001 From: boyska Date: Thu, 12 Nov 2020 01:07:31 +0100 Subject: [PATCH] feed: always downloads with resume what if the server doesn't support range? we don't support it! --- feed | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/feed b/feed index c96ebc1..25e473d 100755 --- a/feed +++ b/feed @@ -396,6 +396,18 @@ def get_parser(): return p +def downloader(url, dest): + headers = {} + if os.path.exists(dest): + headers["Range"] = "bytes=%d-" % os.stat(dest).st_size + r = requests.get(url, stream=True, headers=headers) + if r.status_code == 416: # range not satisfiable + return + with open(dest, "ab") as f: + for chunk in r.iter_content(chunk_size=2 << 15): + f.write(chunk) + + def put(audio, copy=False): if not copy: for url in audio.urls: @@ -411,7 +423,7 @@ def put(audio, copy=False): ).rstrip() dest = os.path.join(destdir, fname) os.makedirs(destdir, exist_ok=True) - fname, headers = urllib.request.urlretrieve(url, dest) + downloader(url, dest) print("file://%s" % os.path.realpath(fname)) else: # FIXME: file:// urls are just copied