mirror of
https://github.com/Findus23/PaperLibrary-cli.git
synced 2024-09-20 17:03:46 +02:00
better PDF download
This commit is contained in:
parent
8066900ab3
commit
cfee02dbe4
1 changed files with 12 additions and 5 deletions
|
@ -1,6 +1,7 @@
|
|||
import hashlib
|
||||
import os
|
||||
import shutil
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
|
@ -102,14 +103,20 @@ def write_symlinks(api: PaperLibraryAPI, config: Config):
|
|||
|
||||
|
||||
def download_file(api: PaperLibraryAPI, url: str, target_file: Path):
|
||||
print("downloading", url, end=" ", flush=True)
|
||||
r = api.s.get(url)
|
||||
r.raise_for_status()
|
||||
with alive_bar(int(r.headers["Content-Length"])) as bar:
|
||||
file_size = int(r.headers["Content-Length"])
|
||||
if file_size > 30 * 1024 * 1024:
|
||||
with alive_bar(int(r.headers["Content-Length"])) as bar:
|
||||
with target_file.open("wb") as f:
|
||||
for chunk in r.iter_content(1024):
|
||||
bar(1024)
|
||||
f.write(chunk)
|
||||
else:
|
||||
with target_file.open("wb") as f:
|
||||
for chunk in r.iter_content(1024):
|
||||
for _ in range(1024):
|
||||
bar()
|
||||
f.write(chunk)
|
||||
f.write(r.content)
|
||||
print("done")
|
||||
|
||||
|
||||
def hash_file(file: Path, buffer_size=65536) -> str:
|
||||
|
|
Loading…
Reference in a new issue