Last active
August 26, 2026 04:53
-
-
Save marethyu/62ecff061e0d43b779ebf873ce2f0c41 to your computer and use it in GitHub Desktop.
Script to fetch the latest chapter of a series in Comicwalker
This file contains hidden or 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 itertools | |
| import json | |
| import os | |
| import sys | |
| import requests | |
| from bs4 import BeautifulSoup | |
| CW_API_ENDPOINT = 'https://comic-walker.com/api' | |
| SERIES_URL = 'https://comic-walker.com/detail/KC_000604_S?episodeType=first' | |
| # SERIES_URL = 'https://comic-walker.com/detail/KC_006775_S?episodeType=first' | |
| HEADERS = { | |
| 'User-Agent': ( | |
| 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) ' | |
| 'AppleWebKit/537.36 (KHTML, like Gecko) ' | |
| 'Chrome/96.0.4664.110 Safari/537.36' | |
| ) | |
| } | |
| def get_latest_episode_id(cw_url): | |
| response = requests.get(cw_url, headers=HEADERS) | |
| response.raise_for_status() | |
| soup = BeautifulSoup(response.content, 'html.parser') | |
| next_data = soup.find(id='__NEXT_DATA__') | |
| if next_data is None or not next_data.contents: | |
| raise RuntimeError('ComicWalker page is missing __NEXT_DATA__ metadata') | |
| data = json.loads(next_data.contents[0]) | |
| query = data['props']['pageProps']['dehydratedState']['queries'][0] | |
| latest_episodes = query['state']['data']['latestEpisodes'] | |
| results = latest_episodes['result'] | |
| if not results: | |
| raise RuntimeError('ComicWalker returned no episodes') | |
| return results[0]['id'] | |
| def generate_key(drm_hash): | |
| return bytes.fromhex(drm_hash[:16]) | |
| def main(): | |
| if len(sys.argv) < 2: | |
| program_name = os.path.basename(sys.argv[0]) | |
| print(f'Usage: {program_name} output-directory') | |
| return 0 | |
| episode_id = get_latest_episode_id(SERIES_URL) | |
| output_directory = sys.argv[1] | |
| print(f'Latest chapter id: {episode_id}') | |
| url = ( | |
| f'{CW_API_ENDPOINT}/contents/viewer?episodeId={episode_id}' | |
| '&imageSizeType=width%3A1284' | |
| ) | |
| response = requests.get(url, headers=HEADERS) | |
| response.raise_for_status() | |
| raw_images = response.json()['manuscripts'] | |
| os.makedirs(output_directory, exist_ok=True) | |
| print(f'Starting to download the latest chapter to {output_directory}') | |
| page_count = len(raw_images) | |
| for page_index, raw_image in enumerate(raw_images): | |
| print(f'Downloading page {page_index + 1}/{page_count}') | |
| xor_key = generate_key(raw_image['drmHash']) | |
| response = requests.get(raw_image['drmImageUrl']) | |
| response.raise_for_status() | |
| raw_data = response.content | |
| output_path = os.path.join(output_directory, f'{page_index}.jpg') | |
| with open(output_path, 'wb') as output_file: | |
| decoded_data = bytes( | |
| value ^ key_byte | |
| for value, key_byte in zip( | |
| raw_data, | |
| itertools.cycle(xor_key), | |
| ) | |
| ) | |
| output_file.write(decoded_data) | |
| return 0 | |
| if __name__ == '__main__': | |
| sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment