Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -324,37 +324,40 @@ class PaperDownloader:
|
|
324 |
return None, f"Error processing {doi}: {e}", f"Error processing {doi}: {e}"
|
325 |
|
326 |
async def download_multiple_dois_async(self, dois_text, progress_callback):
|
327 |
-
|
328 |
-
|
329 |
-
return None, "Error: No DOIs provided", "Error: No DOIs provided"
|
330 |
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
|
|
342 |
# Unique filename for zip
|
343 |
filename = f"{str(doi).replace('/', '_').replace('.', '_')}_{i}.pdf"
|
344 |
filepath_unique = os.path.join(self.output_dir, filename)
|
345 |
os.rename(filepath, filepath_unique)
|
346 |
downloaded_files.append(filepath_unique)
|
347 |
downloaded_links.append(f'<a href="https://doi.org/{doi}">{doi}</a>')
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
|
|
|
|
|
|
358 |
|
359 |
def create_zip(self, zip_filename, downloaded_files):
|
360 |
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|
|
|
324 |
return None, f"Error processing {doi}: {e}", f"Error processing {doi}: {e}"
|
325 |
|
326 |
async def download_multiple_dois_async(self, dois_text, progress_callback):
|
327 |
+
if not dois_text:
|
328 |
+
return None, "Error: No DOIs provided", "Error: No DOIs provided"
|
|
|
329 |
|
330 |
+
dois = [doi.strip() for doi in dois_text.split('\n') if doi.strip()]
|
331 |
+
if not dois:
|
332 |
+
return None, "Error: No valid DOIs provided", "Error: No valid DOIs provided"
|
333 |
|
334 |
+
downloaded_files = []
|
335 |
+
failed_dois = []
|
336 |
+
downloaded_links = []
|
337 |
+
|
338 |
+
for i, doi in enumerate(dois):
|
339 |
+
try:
|
340 |
+
filepath, success_message, fail_message = await self.download_single_doi_async(doi, progress_callback)
|
341 |
+
if filepath:
|
342 |
# Unique filename for zip
|
343 |
filename = f"{str(doi).replace('/', '_').replace('.', '_')}_{i}.pdf"
|
344 |
filepath_unique = os.path.join(self.output_dir, filename)
|
345 |
os.rename(filepath, filepath_unique)
|
346 |
downloaded_files.append(filepath_unique)
|
347 |
downloaded_links.append(f'<a href="https://doi.org/{doi}">{doi}</a>')
|
348 |
+
else:
|
349 |
+
failed_dois.append(f'<a href="https://doi.org/{doi}">{doi}</a> - {fail_message}')
|
350 |
+
except Exception as e:
|
351 |
+
failed_dois.append(f'<a href="https://doi.org/{doi}">{doi}</a> - Unexpected error: {str(e)}')
|
352 |
+
continue # Continue to next DOI even if this one fails
|
353 |
+
|
354 |
+
if downloaded_files:
|
355 |
+
zip_filename = 'papers.zip'
|
356 |
+
loop = asyncio.get_running_loop()
|
357 |
+
await loop.run_in_executor(self.executor, lambda: self.create_zip(zip_filename,downloaded_files))
|
358 |
+
logger.info(f"ZIP file created: {zip_filename}")
|
359 |
+
|
360 |
+
return zip_filename if downloaded_files else None, "\n".join(downloaded_links), "\n".join(failed_dois)
|
361 |
|
362 |
def create_zip(self, zip_filename, downloaded_files):
|
363 |
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|