mirror of
				https://github.com/RicterZ/nhentai.git
				synced 2025-11-04 02:50:55 +01:00 
			
		
		
		
	Merge pull request #116 from AnhNhan/master
write ComicInfo.xml for CBZ files
This commit is contained in:
		@@ -38,7 +38,7 @@ def banner():
 | 
				
			|||||||
def cmd_parser():
 | 
					def cmd_parser():
 | 
				
			||||||
    parser = OptionParser('\n  nhentai --search [keyword] --download'
 | 
					    parser = OptionParser('\n  nhentai --search [keyword] --download'
 | 
				
			||||||
                          '\n  NHENTAI=http://h.loli.club nhentai --id [ID ...]'
 | 
					                          '\n  NHENTAI=http://h.loli.club nhentai --id [ID ...]'
 | 
				
			||||||
                          '\n  nhentai --file [filename]'    
 | 
					                          '\n  nhentai --file [filename]'
 | 
				
			||||||
                          '\n\nEnvironment Variable:\n'
 | 
					                          '\n\nEnvironment Variable:\n'
 | 
				
			||||||
                          '  NHENTAI                 nhentai mirror url')
 | 
					                          '  NHENTAI                 nhentai mirror url')
 | 
				
			||||||
    # operation options
 | 
					    # operation options
 | 
				
			||||||
@@ -95,6 +95,8 @@ def cmd_parser():
 | 
				
			|||||||
                      help='generate a main viewer contain all the doujin in the folder')
 | 
					                      help='generate a main viewer contain all the doujin in the folder')
 | 
				
			||||||
    parser.add_option('--cbz', '-C', dest='is_cbz', action='store_true',
 | 
					    parser.add_option('--cbz', '-C', dest='is_cbz', action='store_true',
 | 
				
			||||||
                      help='generate Comic Book CBZ File')
 | 
					                      help='generate Comic Book CBZ File')
 | 
				
			||||||
 | 
					    parser.add_option('--comic-info', dest='write_comic_info', action='store_true',
 | 
				
			||||||
 | 
					                      help='when generating Comic Book CBZ File, also write ComicInfo.xml')
 | 
				
			||||||
    parser.add_option('--rm-origin-dir', dest='rm_origin_dir', action='store_true', default=False,
 | 
					    parser.add_option('--rm-origin-dir', dest='rm_origin_dir', action='store_true', default=False,
 | 
				
			||||||
                      help='remove downloaded doujinshi dir when generated CBZ file.')
 | 
					                      help='remove downloaded doujinshi dir when generated CBZ file.')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,6 +28,7 @@ def main():
 | 
				
			|||||||
    check_cookie()
 | 
					    check_cookie()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    index = 0
 | 
					    index = 0
 | 
				
			||||||
 | 
					    index_value = None
 | 
				
			||||||
    doujinshis = []
 | 
					    doujinshis = []
 | 
				
			||||||
    doujinshi_ids = []
 | 
					    doujinshi_ids = []
 | 
				
			||||||
    doujinshi_list = []
 | 
					    doujinshi_list = []
 | 
				
			||||||
@@ -43,18 +44,23 @@ def main():
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    elif options.artist:
 | 
					    elif options.artist:
 | 
				
			||||||
        index = 1
 | 
					        index = 1
 | 
				
			||||||
 | 
					        index_value = options.artist
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    elif options.character:
 | 
					    elif options.character:
 | 
				
			||||||
        index = 2
 | 
					        index = 2
 | 
				
			||||||
 | 
					        index_value = options.character
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    elif options.parody:
 | 
					    elif options.parody:
 | 
				
			||||||
        index = 3
 | 
					        index = 3
 | 
				
			||||||
 | 
					        index_value = options.parody
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    elif options.group:
 | 
					    elif options.group:
 | 
				
			||||||
        index = 4
 | 
					        index = 4
 | 
				
			||||||
 | 
					        index_value = options.group
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    elif options.language:
 | 
					    elif options.language:
 | 
				
			||||||
        index = 5
 | 
					        index = 5
 | 
				
			||||||
 | 
					        index_value = options.language
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    elif options.keyword:
 | 
					    elif options.keyword:
 | 
				
			||||||
        doujinshis = search_parser(options.keyword, sorting=options.sorting, page=options.page)
 | 
					        doujinshis = search_parser(options.keyword, sorting=options.sorting, page=options.page)
 | 
				
			||||||
@@ -63,7 +69,7 @@ def main():
 | 
				
			|||||||
        doujinshi_ids = options.id
 | 
					        doujinshi_ids = options.id
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if index:
 | 
					    if index:
 | 
				
			||||||
        doujinshis = tag_parser(options.language, max_page=options.max_page, index=index)
 | 
					        doujinshis = tag_parser(index_value, max_page=options.max_page, index=index)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    print_doujinshi(doujinshis)
 | 
					    print_doujinshi(doujinshis)
 | 
				
			||||||
    if options.is_download and doujinshis:
 | 
					    if options.is_download and doujinshis:
 | 
				
			||||||
@@ -103,7 +109,7 @@ def main():
 | 
				
			|||||||
            if not options.is_nohtml and not options.is_cbz:
 | 
					            if not options.is_nohtml and not options.is_cbz:
 | 
				
			||||||
                generate_html(options.output_dir, doujinshi)
 | 
					                generate_html(options.output_dir, doujinshi)
 | 
				
			||||||
            elif options.is_cbz:
 | 
					            elif options.is_cbz:
 | 
				
			||||||
                generate_cbz(options.output_dir, doujinshi, options.rm_origin_dir)
 | 
					                generate_cbz(options.output_dir, doujinshi, options.rm_origin_dir, options.write_comic_info)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if options.main_viewer:
 | 
					        if options.main_viewer:
 | 
				
			||||||
            generate_main_html(options.output_dir)
 | 
					            generate_main_html(options.output_dir)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,9 +1,11 @@
 | 
				
			|||||||
# coding: utf-8
 | 
					# coding: utf-8
 | 
				
			||||||
import json
 | 
					import json
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
 | 
					from iso8601 import parse_date
 | 
				
			||||||
 | 
					from xml.sax.saxutils import escape
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def serialize(doujinshi, dir):
 | 
					def serialize_json(doujinshi, dir):
 | 
				
			||||||
    metadata = {'title': doujinshi.name,
 | 
					    metadata = {'title': doujinshi.name,
 | 
				
			||||||
                'subtitle': doujinshi.info.subtitle}
 | 
					                'subtitle': doujinshi.info.subtitle}
 | 
				
			||||||
    if doujinshi.info.date:
 | 
					    if doujinshi.info.date:
 | 
				
			||||||
@@ -28,6 +30,48 @@ def serialize(doujinshi, dir):
 | 
				
			|||||||
        json.dump(metadata, f, separators=','':')
 | 
					        json.dump(metadata, f, separators=','':')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def serialize_comicxml(doujinshi, dir):
 | 
				
			||||||
 | 
					    with open(os.path.join(dir, 'ComicInfo.xml'), 'w') as f:
 | 
				
			||||||
 | 
					        f.write('<?xml version="1.0" encoding="utf-8"?>\n')
 | 
				
			||||||
 | 
					        f.write('<ComicInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\n')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        xml_write_simple_tag(f, 'Manga', 'Yes')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        xml_write_simple_tag(f, 'Title', doujinshi.name)
 | 
				
			||||||
 | 
					        xml_write_simple_tag(f, 'Summary', doujinshi.info.subtitle)
 | 
				
			||||||
 | 
					        xml_write_simple_tag(f, 'PageCount', doujinshi.pages)
 | 
				
			||||||
 | 
					        xml_write_simple_tag(f, 'URL', doujinshi.url)
 | 
				
			||||||
 | 
					        xml_write_simple_tag(f, 'NhentaiId', doujinshi.id)
 | 
				
			||||||
 | 
					        xml_write_simple_tag(f, 'Genre', doujinshi.info.categories)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        xml_write_simple_tag(f, 'BlackAndWhite', 'No' if doujinshi.info.tags and 'full color' in doujinshi.info.tags else 'Yes')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if doujinshi.info.date:
 | 
				
			||||||
 | 
					            dt = parse_date(doujinshi.info.date)
 | 
				
			||||||
 | 
					            xml_write_simple_tag(f, 'Year', dt.year)
 | 
				
			||||||
 | 
					            xml_write_simple_tag(f, 'Month', dt.month)
 | 
				
			||||||
 | 
					            xml_write_simple_tag(f, 'Day', dt.day)
 | 
				
			||||||
 | 
					        if doujinshi.info.parodies:
 | 
				
			||||||
 | 
					            xml_write_simple_tag(f, 'Series', doujinshi.info.parodies)
 | 
				
			||||||
 | 
					        if doujinshi.info.characters:
 | 
				
			||||||
 | 
					            xml_write_simple_tag(f, 'Characters', doujinshi.info.characters)
 | 
				
			||||||
 | 
					        if doujinshi.info.tags:
 | 
				
			||||||
 | 
					            xml_write_simple_tag(f, 'Tags', doujinshi.info.tags)
 | 
				
			||||||
 | 
					        if doujinshi.info.artists:
 | 
				
			||||||
 | 
					            xml_write_simple_tag(f, 'Writer', ' & '.join([i.strip() for i in doujinshi.info.artists.split(',')]))
 | 
				
			||||||
 | 
					        # if doujinshi.info.groups:
 | 
				
			||||||
 | 
					        #     metadata['group'] = [i.strip() for i in doujinshi.info.groups.split(',')]
 | 
				
			||||||
 | 
					        if doujinshi.info.languages:
 | 
				
			||||||
 | 
					            languages = [i.strip() for i in doujinshi.info.languages.split(',')]
 | 
				
			||||||
 | 
					            xml_write_simple_tag(f, 'Translated', 'Yes' if 'translated' in languages else 'No')
 | 
				
			||||||
 | 
					            [xml_write_simple_tag(f, 'Language', i) for i in languages if i != 'translated']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        f.write('</ComicInfo>')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def xml_write_simple_tag(f, name, val, indent=1):
 | 
				
			||||||
 | 
					    f.write(f'{"  " * indent}<{name}>{escape(str(val))}</{name}>\n')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def merge_json():
 | 
					def merge_json():
 | 
				
			||||||
    lst = []
 | 
					    lst = []
 | 
				
			||||||
    output_dir = "./"
 | 
					    output_dir = "./"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,7 +12,7 @@ import sqlite3
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
from nhentai import constant
 | 
					from nhentai import constant
 | 
				
			||||||
from nhentai.logger import logger
 | 
					from nhentai.logger import logger
 | 
				
			||||||
from nhentai.serializer import serialize, set_js_database
 | 
					from nhentai.serializer import serialize_json, serialize_comicxml, set_js_database
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def request(method, url, **kwargs):
 | 
					def request(method, url, **kwargs):
 | 
				
			||||||
@@ -86,7 +86,7 @@ def generate_html(output_dir='.', doujinshi_obj=None):
 | 
				
			|||||||
    js = readfile('viewer/scripts.js')
 | 
					    js = readfile('viewer/scripts.js')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if doujinshi_obj is not None:
 | 
					    if doujinshi_obj is not None:
 | 
				
			||||||
        serialize(doujinshi_obj, doujinshi_dir)
 | 
					        serialize_json(doujinshi_obj, doujinshi_dir)
 | 
				
			||||||
        name = doujinshi_obj.name
 | 
					        name = doujinshi_obj.name
 | 
				
			||||||
        if sys.version_info < (3, 0):
 | 
					        if sys.version_info < (3, 0):
 | 
				
			||||||
            name = doujinshi_obj.name.encode('utf-8')
 | 
					            name = doujinshi_obj.name.encode('utf-8')
 | 
				
			||||||
@@ -102,9 +102,9 @@ def generate_html(output_dir='.', doujinshi_obj=None):
 | 
				
			|||||||
            with open(os.path.join(doujinshi_dir, 'index.html'), 'wb') as f:
 | 
					            with open(os.path.join(doujinshi_dir, 'index.html'), 'wb') as f:
 | 
				
			||||||
                f.write(data.encode('utf-8'))
 | 
					                f.write(data.encode('utf-8'))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        logger.log(15, 'HTML Viewer has been write to \'{0}\''.format(os.path.join(doujinshi_dir, 'index.html')))
 | 
					        logger.log(15, 'HTML Viewer has been written to \'{0}\''.format(os.path.join(doujinshi_dir, 'index.html')))
 | 
				
			||||||
    except Exception as e:
 | 
					    except Exception as e:
 | 
				
			||||||
        logger.warning('Writen HTML Viewer failed ({})'.format(str(e)))
 | 
					        logger.warning('Writing HTML Viewer failed ({})'.format(str(e)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def generate_main_html(output_dir='./'):
 | 
					def generate_main_html(output_dir='./'):
 | 
				
			||||||
@@ -150,7 +150,7 @@ def generate_main_html(output_dir='./'):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        image_html += element.format(FOLDER=folder, IMAGE=image, TITLE=title)
 | 
					        image_html += element.format(FOLDER=folder, IMAGE=image, TITLE=title)
 | 
				
			||||||
    if image_html == '':
 | 
					    if image_html == '':
 | 
				
			||||||
        logger.warning('None index.html found, --gen-main paused.')
 | 
					        logger.warning('No index.html found, --gen-main paused.')
 | 
				
			||||||
        return
 | 
					        return
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        data = main.format(STYLES=css, SCRIPTS=js, PICTURE=image_html)
 | 
					        data = main.format(STYLES=css, SCRIPTS=js, PICTURE=image_html)
 | 
				
			||||||
@@ -163,14 +163,16 @@ def generate_main_html(output_dir='./'):
 | 
				
			|||||||
        shutil.copy(os.path.dirname(__file__)+'/viewer/logo.png', './')
 | 
					        shutil.copy(os.path.dirname(__file__)+'/viewer/logo.png', './')
 | 
				
			||||||
        set_js_database()
 | 
					        set_js_database()
 | 
				
			||||||
        logger.log(
 | 
					        logger.log(
 | 
				
			||||||
            15, 'Main Viewer has been write to \'{0}main.html\''.format(output_dir))
 | 
					            15, 'Main Viewer has been written to \'{0}main.html\''.format(output_dir))
 | 
				
			||||||
    except Exception as e:
 | 
					    except Exception as e:
 | 
				
			||||||
        logger.warning('Writen Main Viewer failed ({})'.format(str(e)))
 | 
					        logger.warning('Writing Main Viewer failed ({})'.format(str(e)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
 | 
					def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_comic_info=False):
 | 
				
			||||||
    if doujinshi_obj is not None:
 | 
					    if doujinshi_obj is not None:
 | 
				
			||||||
        doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
 | 
					        doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
 | 
				
			||||||
 | 
					        if write_comic_info:
 | 
				
			||||||
 | 
					            serialize_comicxml(doujinshi_obj, doujinshi_dir)
 | 
				
			||||||
        cbz_filename = os.path.join(os.path.join(doujinshi_dir, '..'), '{}.cbz'.format(doujinshi_obj.filename))
 | 
					        cbz_filename = os.path.join(os.path.join(doujinshi_dir, '..'), '{}.cbz'.format(doujinshi_obj.filename))
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        cbz_filename = './doujinshi.cbz'
 | 
					        cbz_filename = './doujinshi.cbz'
 | 
				
			||||||
@@ -188,7 +190,7 @@ def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
 | 
				
			|||||||
    if rm_origin_dir:
 | 
					    if rm_origin_dir:
 | 
				
			||||||
        shutil.rmtree(doujinshi_dir, ignore_errors=True)
 | 
					        shutil.rmtree(doujinshi_dir, ignore_errors=True)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    logger.log(15, 'Comic Book CBZ file has been write to \'{0}\''.format(doujinshi_dir))
 | 
					    logger.log(15, 'Comic Book CBZ file has been written to \'{0}\''.format(doujinshi_dir))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def format_filename(s):
 | 
					def format_filename(s):
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,3 +4,4 @@ BeautifulSoup4>=4.0.0
 | 
				
			|||||||
threadpool>=1.2.7
 | 
					threadpool>=1.2.7
 | 
				
			||||||
tabulate>=0.7.5
 | 
					tabulate>=0.7.5
 | 
				
			||||||
future>=0.15.2
 | 
					future>=0.15.2
 | 
				
			||||||
 | 
					iso8601 >= 0.1
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user