Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancing download_single_file to Handle Non-Existent Files #11

Open
eggry opened this issue May 16, 2024 · 0 comments
Open

Enhancing download_single_file to Handle Non-Existent Files #11

eggry opened this issue May 16, 2024 · 0 comments

Comments

@eggry
Copy link

eggry commented May 16, 2024

def download_single_file(url: str, fname: str, pbar: tqdm):
global sess
resp = sess.get(url, stream=True)
with open(fname, 'wb') as file:
for data in resp.iter_content(chunk_size=1024):
size = file.write(data)
pbar.update(size)

When the download_single_file function encounters a URL that points to a non-existent file, it currently does not raise an error and proceeds as if downloading a regular file. As a result, the downloaded file is merely a webpage saying “文件不存在”.

A notable challenge is that the server still responds with a status code of 200 instead of 404 in such case. An ugly-but-effective way solution involves checking for redirection in resp: an exist file will undergo a redirection from files/?dl=1 to seafhttp/files/. In contrast, a Not Found response is served directly from files/?dl=1 without any redirection.

def download_single_file(url: str, fname: str, pbar: tqdm):
    global sess
    resp = sess.get(url, stream=True)
    if not resp.history:
        raise ValueError("File may non-exist!")
    with open(fname, 'wb') as file:
        for data in resp.iter_content(chunk_size=1024):
            size = file.write(data)
            pbar.update(size)

Wonder if there are more elegant solutions...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant