Skip to content

Commit b60e8cf

Browse files
committed
More comprehensive error catching when downloading TikTok images
1 parent 2987fd4 commit b60e8cf

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

processors/visualisation/download_tiktok.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -395,18 +395,18 @@ def collect_image(url, user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_
395395
"""
396396
try:
397397
response = requests.get(url, stream=True, timeout=20, headers={"User-Agent": user_agent})
398-
except requests.exceptions.RequestException as e:
399-
raise FileNotFoundError(f"Unable to download TikTok image via {url} ({e}), skipping")
400398

401-
if response.status_code != 200 or "image" not in response.headers.get("content-type", ""):
402-
raise FileNotFoundError(f"Unable to download image; status_code:{response.status_code} content-type:{response.headers.get('content-type', '')}")
399+
if response.status_code != 200 or "image" not in response.headers.get("content-type", ""):
400+
raise FileNotFoundError(f"Unable to download image; status_code:{response.status_code} content-type:{response.headers.get('content-type', '')}")
403401

404-
# Process images
405-
image_io = BytesIO(response.content)
406-
try:
407-
picture = Image.open(image_io)
408-
except UnidentifiedImageError:
409-
picture = Image.open(image_io.raw)
402+
# Process images
403+
image_io = BytesIO(response.content)
404+
try:
405+
picture = Image.open(image_io)
406+
except UnidentifiedImageError:
407+
picture = Image.open(image_io.raw)
408+
except (ConnectionError, requests.exceptions.RequestException) as e:
409+
raise FileNotFoundError(f"Unable to download TikTok image via {url} ({e}), skipping")
410410

411411
# Grab extension from response
412412
extension = response.headers["Content-Type"].split("/")[-1]

0 commit comments

Comments
 (0)