Last active
April 7, 2023 11:56
-
-
Save RozeFound/4be6775f379c5361b2d6d133d4ad0258 to your computer and use it in GitHub Desktop.
Download and solve images from comic-days.com
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 cv2, json, asyncio, numpy | |
| from urllib.parse import urlparse | |
| from aiohttp import ClientSession | |
| def solve(file: bytes, page: int) -> None: | |
| np_array = numpy.frombuffer(file, numpy.uint8) | |
| image = cv2.imdecode(np_array, cv2.IMREAD_COLOR) | |
| new_image = image.copy() | |
| height, width, _ = image.shape | |
| tile_width, tile_height = width // 32 * 8, height // 4 | |
| for x in range(4): | |
| for y in range(4): | |
| s_x, s_y = x * tile_width, y * tile_height | |
| d_x, d_y = x * tile_height, y * tile_width | |
| tile = image[s_y:s_y+tile_height, s_x:s_x+tile_width] | |
| new_image[d_x:d_x+tile_height, d_y:d_y+tile_width] = tile | |
| cv2.imwrite(f"{page:02}.jpg", new_image) | |
| async def process_image(session: ClientSession, page: int, url: str) -> None: | |
| async with session.get(url) as response: | |
| content = await response.content.read() | |
| await asyncio.to_thread(solve, content, page) | |
| async def main() -> None: | |
| futures = [] | |
| with open("HAR.json") as file: | |
| data = json.load(file) | |
| async with ClientSession() as session: | |
| for entry in data['log']['entries']: | |
| url = entry['request']['url'] | |
| parsed_url = urlparse(url) | |
| if parsed_url.netloc == "cdn-img.comic-days.com": | |
| page = parsed_url.path.split('-')[0][-2:] | |
| futures.append(process_image(session, int(page), url)) | |
| await asyncio.gather(*futures) | |
| if __name__ == "__main__": | |
| asyncio.run(main()) |
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 cv2, json, asyncio, numpy | |
| from urllib.parse import urlparse | |
| from aiohttp import ClientSession | |
| from pathlib import Path | |
| def solve(file: bytes, page: int): | |
| np_array = numpy.frombuffer(file, numpy.uint8) | |
| image = cv2.imdecode(np_array, cv2.IMREAD_COLOR) | |
| new_image = image.copy() | |
| height, width, _ = image.shape | |
| tile_width, tile_height = width // 32 * 8, height // 4 | |
| for x in range(4): | |
| for y in range(4): | |
| s_x, s_y = x * tile_width, y * tile_height | |
| d_x, d_y = x * tile_height, y * tile_width | |
| tile = image[s_y:s_y+tile_height, s_x:s_x+tile_width] | |
| new_image[d_x:d_x+tile_height, d_y:d_y+tile_width] = tile | |
| return new_image | |
| async def process_image(session: ClientSession, page: int, url: str, output) -> None: | |
| async with session.get(url) as response: | |
| content = await response.content.read() | |
| output_data = await asyncio.to_thread(solve, content, page) | |
| cv2.imwrite(f"{output}/{page:04}.jpg", output_data) | |
| async def process_har(session: ClientSession, har: dict, output) -> None: | |
| futures = [] | |
| Path(output).mkdir(parents=True, exist_ok=True) | |
| for entry in har['log']['entries']: | |
| url = entry['request']['url'] | |
| parsed_url = urlparse(url) | |
| if parsed_url.netloc == "cdn-img.comic-days.com": | |
| if "link-slot-series" in parsed_url.path: continue | |
| page = parsed_url.path.split('-')[0][-4:] | |
| futures.append(process_image(session, int(page), url, output)) | |
| await asyncio.gather(*futures) | |
| async def main() -> None: | |
| async with ClientSession() as session: | |
| if (har := Path("HAR.json")).exists(): | |
| text = har.read_text(); data = json.loads(text) | |
| process_har(session, data, "Output") | |
| else: | |
| futures = [] | |
| for dir in Path.cwd().glob("*.har"): | |
| text = dir.read_text(); data = json.loads(text) | |
| futures.append(process_har(session, data, dir.stem)) | |
| await asyncio.gather(*futures) | |
| if __name__ == "__main__": | |
| asyncio.run(main()) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use:
5.1 (for single) Save HAR data to "HAR.json" file (click gear, then save HAR data, then rename file to HAR.json)
5.2 (for multiple) Save HAR data to chapter_num.har (click gear, then save HAR data, then type chapter number)
Works for paid chapters as well since you provide links manually.