From b71294eadecc604d4cc074eb9ec6f3a888b97fb7 Mon Sep 17 00:00:00 2001 From: Bobby Date: Wed, 5 Jul 2023 15:42:19 -0400 Subject: Handle MAL Errors --- blog/views.py | 38 +++++++++++++++++++++---------------- static/images/site/sad-failure.gif | Bin 0 -> 1003140 bytes 2 files changed, 22 insertions(+), 16 deletions(-) create mode 100644 static/images/site/sad-failure.gif diff --git a/blog/views.py b/blog/views.py index 9b573491..f1966994 100644 --- a/blog/views.py +++ b/blog/views.py @@ -568,21 +568,27 @@ def anilist(request): def anidata(request): malURL = 'https://myanimelist.net/animelist/crvs' - MALContent = requests.get(malURL).content - MALContent = MALContent.decode('utf-8') - MALParsed = BeautifulSoup(MALContent, 'html.parser') - # remove script tags - for tag in MALParsed(['script', 'meta', 'noscript']): - tag.extract() - - # add myanimelist.net to relative links - for link in MALParsed.find_all('a'): - if link.get('href') and link.get('href')[0] == '/': - link['href'] = 'https://myanimelist.net' + link['href'] - - # make all links open in new tab - link['target'] = '_blank' - - MALContent = MALParsed.prettify() + MAL = requests.get(malURL) + MALContent = MAL.content + MALStatus = MAL.status_code + + if MALStatus != 200: + MALContent = 'Sad Failure

MyAnimeList does not seem to respond at the moment.

Maybe, we go knock on their door instead?

' + else: + MALContent = MALContent.decode('utf-8') + MALParsed = BeautifulSoup(MALContent, 'html.parser') + # remove script tags + for tag in MALParsed(['script', 'meta', 'noscript']): + tag.extract() + + # add myanimelist.net to relative links + for link in MALParsed.find_all('a'): + if link.get('href') and link.get('href')[0] == '/': + link['href'] = 'https://myanimelist.net' + link['href'] + + # make all links open in new tab + link['target'] = '_blank' + + MALContent = MALParsed.prettify() return render(request, 'blog/anidata.html', {'title': 'My Anime List', 'MALContent': MALContent}) diff --git a/static/images/site/sad-failure.gif b/static/images/site/sad-failure.gif new file mode 100644 index 00000000..a0b88513 Binary files /dev/null and b/static/images/site/sad-failure.gif differ -- cgit v1.2.3