Skip to content

Commit

Permalink
More comprehensive error catching when downloading TikTok images
Browse files Browse the repository at this point in the history
  • Loading branch information
stijn-uva committed Jan 16, 2025
1 parent 2987fd4 commit b60e8cf
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions processors/visualisation/download_tiktok.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,18 +395,18 @@ def collect_image(url, user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_
"""
try:
response = requests.get(url, stream=True, timeout=20, headers={"User-Agent": user_agent})
except requests.exceptions.RequestException as e:
raise FileNotFoundError(f"Unable to download TikTok image via {url} ({e}), skipping")

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

# Process images
image_io = BytesIO(response.content)
try:
picture = Image.open(image_io)
except UnidentifiedImageError:
picture = Image.open(image_io.raw)
# Process images
image_io = BytesIO(response.content)
try:
picture = Image.open(image_io)
except UnidentifiedImageError:
picture = Image.open(image_io.raw)
except (ConnectionError, requests.exceptions.RequestException) as e:
raise FileNotFoundError(f"Unable to download TikTok image via {url} ({e}), skipping")

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

0 comments on commit b60e8cf

Please sign in to comment.