def download_gallery(gallery_id, gallery_dir='.'): response = requests.get('{}/g/{}'.format(website_url, gallery_id)) content = clean_str(response.content.decode('utf-8')) soup = BeautifulSoup(content, 'html.parser') # check gallery exists or not if soup.find('h1').contents[0] == '404 – Not Found': return 1 # create directory try: os.makedirs(gallery_dir, exist_ok=True) except FileExistsError: return 2 # get gallery info info = get_gallery_info(soup.find('div', id='info')) out_info = { 'gallery': info, 'created_datetime': datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat()} json_to_file(out_info, filename='info', save_dir=gallery_dir) # download gallery cover cover_div = soup.find('div', id='cover') cover_img_url = cover_div.find('a').find('img').get('data-src') download_image(cover_img_url, filename='cover', save_dir=gallery_dir) # download gallery content images_ids = cover_img_url.split('/') images_id = images_ids[len(images_ids)-2] progress_bar = ProgressBar(info['pages'], length=20, padding=8) for page in range(1, info['pages'] + 1): try: download_image( 'https://i.nhentai.net/galleries/{}/{}.jpg'.format(images_id, page), filename=page, save_dir=gallery_dir) progress_bar.display(page) except: return 3 return 0