downloader: supports Content-Disposition
This commit is contained in:
parent
e25bf292ca
commit
1aa21e837b
1 changed files with 22 additions and 11 deletions
33
feed
33
feed
|
@ -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://'):]
|
||||
|
|
Loading…
Reference in a new issue