Skip to content

Bulk asynchronous downloader for multiple file hosts

Use contextlib.suppress instead of try-except-pass (#433)

Arkadiy Illarionovd1c32ae0f41b
diff --git a/cyberdrop_dl/crawlers/CyberFile_Spider.py b/cyberdrop_dl/crawlers/CyberFile_Spider.py
index b88beb55d..ab5ef0933 100644
--- a/cyberdrop_dl/crawlers/CyberFile_Spider.py
+++ b/cyberdrop_dl/crawlers/CyberFile_Spider.py
@@ -1,3 +1,4 @@
+import contextlib
from bs4 import BeautifulSoup
from yarl import URL
@@ -80,14 +81,11 @@ class CyberFileCrawler:
total_pages = int(soup.select("a[onclick*=loadImages]")[-1].get('onclick').split(',')[2])
listings = soup.select("div[class=fileListing] div")
for listing in listings:
- try:
+ with contextlib.suppress(TypeError):
nodes.append(int(listing.get('folderid')))
- except TypeError:
- pass
- try:
+
+ with contextlib.suppress(TypeError):
contents.append((title, int(listing.get('fileid'))))
- except TypeError:
- pass
if page < total_pages:
contents.extend(await self.get_folder_content(session, url, nodeId, page+1, original_title))
@@ -139,15 +137,12 @@ class CyberFileCrawler:
listings = soup.select("div[class=fileListing] div")
for listing in listings:
title = await make_title_safe(content['page_title'])
- try:
+ with contextlib.suppress(TypeError, AttributeError):
title = title + '/' + await make_title_safe(listing.select_one('span[class=filename]').text)
nodes.append((title, int(listing.get('folderid'))))
- except (TypeError, AttributeError):
- pass
- try:
+
+ with contextlib.suppress(TypeError):
contents.append((title, int(listing.get('fileid'))))
- except TypeError:
- pass
if page < total_pages:
nodes_temp, content_temp = await self.get_shared_ids_and_content(session, url, page + 1)
@@ -177,16 +172,13 @@ class CyberFileCrawler:
total_pages = int(soup.select("a[onclick*=loadImages]")[-1].get('onclick').split(',')[2])
listings = soup.select("div[class=fileListing] div")
for listing in listings:
- try:
+ with contextlib.suppress(TypeError, AttributeError):
filename = listing.select_one('span[class=filename]')
temp_title = title + '/' + await make_title_safe(filename.text)
nodes.append((temp_title, int(listing.get('folderid'))))
- except (TypeError, AttributeError):
- pass
- try:
+
+ with contextlib.suppress(TypeError):
contents.append((title, int(listing.get('fileid'))))
- except TypeError:
- pass
if page < total_pages:
contents.extend(await self.get_shared_content(session, url, nodeId, page + 1, title))
diff --git a/cyberdrop_dl/downloader/downloaders.py b/cyberdrop_dl/downloader/downloaders.py
index 024261a63..a75774652 100644
--- a/cyberdrop_dl/downloader/downloaders.py
+++ b/cyberdrop_dl/downloader/downloaders.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import asyncio
+import contextlib
import itertools
import logging
from http import HTTPStatus
@@ -250,10 +251,8 @@ class Downloader:
if await self.File_Lock.check_lock(filename):
await self.File_Lock.remove_lock(filename)
- try:
+ with contextlib.suppress(Exception):
file_progress.update(file_task, visible=False)
- except Exception:
- pass
if hasattr(e, "message"):
logging.debug(f"\n{media.url} ({e.message})")
diff --git a/cyberdrop_dl/main.py b/cyberdrop_dl/main.py
index b6db4c618..b5260cac9 100644
--- a/cyberdrop_dl/main.py
+++ b/cyberdrop_dl/main.py
@@ -1,5 +1,6 @@
import argparse
import asyncio
+import contextlib
import logging
import re
from pathlib import Path
@@ -294,11 +295,9 @@ def main(args=None):
filemode="w"
)
- try:
+ with contextlib.suppress(RuntimeError):
loop = asyncio.get_event_loop()
loop.run_until_complete(director(args, links))
- except RuntimeError:
- pass
if __name__ == '__main__':