Skip to content

Commit

Permalink
clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
mjishnu committed Nov 18, 2023
1 parent 9481365 commit 26e6552
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
44 changes: 29 additions & 15 deletions pypdl/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import os
import tempfile
import threading
import time
from collections import deque
Expand All @@ -11,8 +10,13 @@

import requests
from reprint import output

from .utls import Multidown, Singledown, timestring, get_filename_from_headers,get_filename_from_url
from .utls import (
Multidown,
Singledown,
get_filename_from_headers,
get_filename_from_url,
timestring,
)


class Downloader:
Expand Down Expand Up @@ -72,19 +76,28 @@ def _download(
multithread (bool): Whether to use multi-threaded download.
"""
# get the header information for the file
head = requests.head(url, timeout=20,
allow_redirects=True,
headers=self.headers,
proxies=self.proxies,
auth=self.auth)
head = requests.head(
url,
timeout=20,
allow_redirects=True,
headers=self.headers,
proxies=self.proxies,
auth=self.auth,
)

# get file name from headers
filename = get_filename_from_headers(head.headers)

# if file name couldn't be retrieved from headers, generate from url
if filename is None:
filename = get_filename_from_url(url)

# if filepath not specified, set filename as filepath
if filepath is None:
filepath = filename

# if filepath is a directory, try to get file name
if os.path.isdir(filepath):
# get file name from headers
filename = get_filename_from_headers(head.headers)
# if file name couldn't be retrieved from headers, generate temporary file
if filename is None:
# generate file name from url
get_filename_from_url(url)
elif os.path.isdir(filepath):
filepath = os.path.join(filepath, filename)

# progress file to keep track of download progress
Expand Down Expand Up @@ -351,6 +364,7 @@ def start_thread():
break
# if there's an error, set the error event and print the error message
except Exception as e:
raise Exception(e)
print(f"Download Error: ({e.__class__.__name__}, {e})")
self._Error.set()

Expand Down
1 change: 1 addition & 0 deletions pypdl/utls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def get_filename_from_headers(headers: CaseInsensitiveDict[str]):
Desired file name
"""
content_disposition = headers.get("Content-Disposition")
print(content_disposition)

if content_disposition and "filename=" in content_disposition:
filename_start = content_disposition.index("filename=") + len("filename=")
Expand Down

0 comments on commit 26e6552

Please sign in to comment.