Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle 404 errors in fetch_stories #950

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions story/management/commands/fetch_stories.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from urllib.error import HTTPError, URLError
from xml.etree import ElementTree

import requests
Expand Down Expand Up @@ -39,10 +40,17 @@ def handle(self, *args, **options):
story = Story(name=name, post_url=post_url, content=_post, is_story=is_story)

if image_url:
img = NamedTemporaryFile(delete=True)
img.write(urlopen(image_url).read())
img.flush()
story.image.save(image_url.split("/")[-1], File(img))
try:
with NamedTemporaryFile(delete=True) as img:
img.write(urlopen(image_url).read())
img.flush()
story.image.save(image_url.split("/")[-1], File(img))
except HTTPError as e:
print(f"HTTP error when fetching image from {image_url}: {e.code} {e.reason}")
except URLError as e:
print(f"URL error when fetching image from {image_url}: {e.reason}")
except Exception as e:
marksweb marked this conversation as resolved.
Show resolved Hide resolved
print(f"Unexpected error when fetching image from {image_url}: {e}")

story.save()

Expand Down
Loading