downloader: supports Content-Disposition

This commit is contained in:
boyska 2024-11-02 23:15:52 +01:00
parent e25bf292ca
commit 1aa21e837b

33
feed
View file

@ -511,18 +511,35 @@ def get_parser():
return p
def downloader(url, dest):
def normalize_filename(original: str) -> str:
fname = original.split('/')[-1]
return "".join(
c for c in fname if c.isalnum() or c in list("._-")
).rstrip()
def downloader(url: str, destdir: str, prefix: str) -> str:
r = requests.head(url)
fname = posixpath.basename(urlparse(url).path)
if r.status_code == 200 and 'Content-Disposition' in r.headers:
matches = re.findall(r'''filename="(.+)"''', r.headers['Content-Disposition'])
if matches:
fname = matches[0]
fname = normalize_filename(fname)
dest = os.path.join(destdir, prefix + fname)
headers = {}
mode = "wb"
if os.path.exists(dest):
headers["Range"] = "bytes=%d-" % os.stat(dest).st_size
mode = "ab"
if 'Content-Length' in r.headers and os.stat(dest).st_size < int(r.headers['Content-Length']):
headers["Range"] = "bytes=%d-" % os.stat(dest).st_size
mode = "ab"
r = requests.get(url, stream=True, headers=headers)
if r.status_code == 416: # range not satisfiable
return
return dest
with open(dest, mode) as f:
for chunk in r.iter_content(chunk_size=1 << 16):
f.write(chunk)
return dest
def put(audio, copy=False):
@ -534,13 +551,7 @@ def put(audio, copy=False):
os.makedirs(destdir, exist_ok=True)
for url in audio.urls:
if url.split(":")[0] in ("http", "https"):
fname = posixpath.basename(urlparse(url).path)
# sanitize
fname = "".join(
c for c in fname if c.isalnum() or c in list("._-")
).rstrip()
dest = os.path.join(destdir, "feed-" + fname)
downloader(url, dest)
dest = downloader(url, destdir=destdir, prefix="feed-")
print("file://%s" % os.path.realpath(dest))
elif url.startswith("file:///"):
src = url[len('file://'):]