Last active
November 10, 2019 13:07
-
-
Save AcrylicShrimp/9760d9b708929ea02266ee7a5aa0c7fb to your computer and use it in GitHub Desktop.
hitomi.la gallery download code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import struct | |
import json | |
import re | |
import ast | |
import os | |
def get_subdomain_from_gallery_id(gallery_id): | |
return chr(97 + gallery_id % 3) | |
def hash_to_url(image_hash): | |
if len(image_hash) < 3: | |
return image_hash | |
return '{}/{}/{}'.format(image_hash[-1], image_hash[-3:-1], image_hash) | |
def convert_gallery_info_to_image_url(gallery_id, gallery_info): | |
if gallery_info['haswebp'] == 1: | |
return 'https://{}a.hitomi.la/webp/{}/{}.webp'.format(get_subdomain_from_gallery_id(int(str(gallery_id)[-1])), gallery_id, gallery_info['name']) | |
else: | |
return 'https://{}a.hitomi.la/images/{}.{}'.format(get_subdomain_from_gallery_id(int(gallery_info['hash'][-3:-1], 16)), hash_to_url(gallery_info['hash']), gallery_info['name'].split('.', maxsplit=1)[1]) | |
def get_gallery_info(gallery_id): | |
js_gallery_info_list = requests.get('https://ltn.hitomi.la/galleries/{}.js'.format(gallery_id)).text | |
python_gallery_info_list = re.search(r'\[[\s\S]+]', js_gallery_info_list).group(0) | |
return ast.literal_eval(python_gallery_info_list) | |
def get_image_url_list(gallery_id, gallery_info): | |
return list([convert_gallery_info_to_image_url(gallery_id, gallery_info) for gallery_info in gallery_info]) | |
def download_image_all(gallery_id, gallery_info, image_url_list): | |
if not os.path.exists(str(gallery_id)): | |
os.mkdir(str(gallery_id)) | |
for index in range(len(gallery_info)): | |
with open('{}/{}'.format(gallery_id, gallery_info[index]['name']), 'wb') as file: | |
file.write(requests.get(image_url_list[index], headers={ | |
'authority': image_url_list[index], | |
'accept': 'image/webp,image/apng,image/*,*/*;q=0.8', | |
'accept-encoding': 'gzip, deflate, br', | |
'accept-language': 'ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7,und;q=0.6,ja;q=0.5', | |
'referer': 'https://hitomi.la/reader/{}.html'.format(gallery_id) | |
}).content) | |
# Below codes demonstrates how to fetch all available gallery ids. | |
# binary = requests.get('https://ltn.hitomi.la/index-all.nozomi').content | |
# gallery_count = len(binary) // 4 | |
# print(gallery_count) | |
# gallery_id_list = struct.unpack('>{}I'.format(gallery_count), binary) | |
# print(gallery_id_list) | |
gallery_id = GALLERY_ID_HERE | |
gallery_info = get_gallery_info(gallery_id) | |
image_url_list = get_image_url_list(gallery_id, gallery_info) | |
download_image_all(gallery_id, gallery_info, image_url_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment