Spaces:
Sleeping
Sleeping
added example env and a utils file
Browse files- example.env +1 -0
- utils.py +33 -0
example.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
SAMPLE_PDF=
|
utils.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from typing import Optional
|
| 3 |
+
from urllib.parse import unquote
|
| 4 |
+
|
| 5 |
+
def get_filename_from_cd(cd: Optional[str]) -> Optional[str]:
|
| 6 |
+
"""
|
| 7 |
+
Get filename from content-disposition header.
|
| 8 |
+
"""
|
| 9 |
+
if not cd:
|
| 10 |
+
return None
|
| 11 |
+
fname = [x.strip() for x in cd.split(';') if x.strip().startswith('filename=')]
|
| 12 |
+
return unquote(fname[0].split('=')[1]) if fname else None
|
| 13 |
+
|
| 14 |
+
def download_file(url: str) -> None:
|
| 15 |
+
"""
|
| 16 |
+
Download a file from a URL and save it with the server-provided name.
|
| 17 |
+
"""
|
| 18 |
+
response = requests.get(url)
|
| 19 |
+
response.raise_for_status() # Raises an HTTPError if the HTTP request returned an unsuccessful status code
|
| 20 |
+
|
| 21 |
+
filename = get_filename_from_cd(response.headers.get('content-disposition'))
|
| 22 |
+
|
| 23 |
+
if filename:
|
| 24 |
+
with open(filename, 'wb') as file:
|
| 25 |
+
file.write(response.content)
|
| 26 |
+
print(f"File downloaded and saved as: {filename}")
|
| 27 |
+
else:
|
| 28 |
+
print("Filename could not be determined from the server.")
|
| 29 |
+
|
| 30 |
+
if __name__ == "__main__":
|
| 31 |
+
# Example Usage
|
| 32 |
+
url = "YOUR_FILE_URL_HERE"
|
| 33 |
+
download_file(url)
|