#!/usr/bin/env python3 import sys import os import re import subprocess def usage(): print("read_manga.py manga_directory [start_at_chapter]") print("examples:") print(" read_manga.py /home/adam/Manga/Naruto \"Chapter 10\"") exit(1) if len(sys.argv) < 2: usage() def chapter_sort_func(ch): match1 = re.search(r"Chapter ([0-9.]+)", ch) if match1 and len(match1.groups()) >= 1: return float(match1.groups()[0]) match2 = re.search(r"^([0-9.]+)", ch) if match2 and len(match2.groups()) >= 1: return float(match2.groups()[0]) raise Exception("Failed to sort. Unexpected chapter name format: {}".format(ch)) def image_sort_func(ch): return int(ch[0:ch.rfind(".")]) start_at_chapter = None if len(sys.argv) >= 3: start_at_chapter = sys.argv[2] manga_directory = sys.argv[1] chapters = [] for chapter in os.listdir(manga_directory): chapters.append(chapter) chapters_by_oldest = sorted(chapters, key=chapter_sort_func) start_index = 0 if start_at_chapter: found_chapter = False index = 0 for chapter in chapters_by_oldest: if chapter == start_at_chapter: start_index = index found_chapter = True break index += 1 if not found_chapter: print("Failed to find chapter %s in list of chapters: %s" % (start_at_chapter, str(chapters_by_oldest))) index = 0 images_str = [] for chapter in chapters_by_oldest: if index < start_index: index += 1 continue images = [] image_dir = os.path.join(manga_directory, chapter) for image in os.listdir(image_dir): # Ignore "in_progress", "finished" and "session_id". We only want image files. if image.find(".") != -1: images.append(image) images = sorted(images, key=image_sort_func) for image in images: images_str.append(os.path.join(image_dir, image)) index += 1 process = subprocess.Popen(["sxiv", "-i", "-s", "f", "-f"], stdin=subprocess.PIPE) process.communicate("\n".join(images_str).encode())