diff --git a/downloader.py b/downloader.py index d02cb19..e46a4a6 100755 --- a/downloader.py +++ b/downloader.py @@ -91,10 +91,16 @@ if not exists(comic_id): while True: # Just going to use `break` to exit print(f"Downloading Comic: {current_comic}") - ## Get url of image - comic_html = bs(requests.get( - f"https://www.gocomics.com/{comic_id}/{current_comic}").content, - "html.parser") + ## Get comic url. If status code not 200, retry. + comic_url = f"https://www.gocomics.com/{comic_id}/{current_comic}" + status = False + while status != 200: + if status != False: + print("Could not get image URL. Retrying...") + comic_html = requests.get(comic_url) + status = comic_html.status_code + comic_html = bs(comic_html.content, "html.parser") + ## Get image url from comic html image_url = comic_html.find( class_="item-comic-image").contents[0].get("src") ## Write image data to file. 'x' means write to new file, 'b' means bin @@ -102,9 +108,18 @@ while True: # Just going to use `break` to exit ## '/' with '-'. The := operator both sets a var and evaluates the ## condition. if not exists(file := f"{comic_id}/{current_comic.replace('/','-')}.gif"): + ## Get comic data. If not success, try again. + status = False + while status != 200: + if status != False: + print("Could not get image. Retrying...") + image = requests.get(image_url) + status = image.status_code + ## Write image to file with open(file, 'xb') as f: - f.write(requests.get(image_url).content) + f.write(image.content) else: + ## If file exists, skip print(f"{file} exists, skipping...") ## Check if there is a next comic. If there is, choose it for next download. ## If there isn't, exit.