MCPcopy
hub / github.com/PDFMathTranslate/PDFMathTranslate / download_with_limit

Function download_with_limit

pdf2zh/gui.py:149–178  ·  view source on GitHub ↗

This function downloads a file from a URL and saves it to a specified path. Inputs: - url: The URL to download the file from - save_path: The path to save the file to - size_limit: The maximum size of the file to download Returns: - The path of the down

(url: str, save_path: str, size_limit: int)

Source from the content-addressed store, hash-verified

147
148
149def download_with_limit(url: str, save_path: str, size_limit: int) -> str:
150 """
151 This function downloads a file from a URL and saves it to a specified path.
152
153 Inputs:
154 - url: The URL to download the file from
155 - save_path: The path to save the file to
156 - size_limit: The maximum size of the file to download
157
158 Returns:
159 - The path of the downloaded file
160 """
161 chunk_size = 1024
162 total_size = 0
163 with requests.get(url, stream=True, timeout=10) as response:
164 response.raise_for_status()
165 content = response.headers.get("Content-Disposition")
166 try: # filename from header
167 _, params = cgi.parse_header(content)
168 filename = params["filename"]
169 except Exception: # filename from url
170 filename = os.path.basename(url)
171 filename = os.path.splitext(os.path.basename(filename))[0] + ".pdf"
172 with open(save_path / filename, "wb") as file:
173 for chunk in response.iter_content(chunk_size=chunk_size):
174 total_size += len(chunk)
175 if size_limit and total_size > size_limit:
176 raise gr.Error("Exceeds file size limit")
177 file.write(chunk)
178 return save_path / filename
179
180
181def stop_translate_file(state: dict) -> None:

Callers 1

translate_fileFunction · 0.85

Calls 1

getMethod · 0.45

Tested by

no test coverage detected