]> git.lizzy.rs Git - nhentai.git/commitdiff
write ComicInfo.xml for CBZ files
authorAnh Nhan Nguyen <anhnhan@outlook.com>
Tue, 21 Apr 2020 11:23:50 +0000 (13:23 +0200)
committerAnh Nhan Nguyen <anhnhan@outlook.com>
Tue, 21 Apr 2020 11:23:50 +0000 (13:23 +0200)
nhentai/serializer.py
nhentai/utils.py
requirements.txt

index 8e7db1d6e611cdeed2c0cb0d6cd82089c9df88cb..fc33cdafa0706c4d30a502389789a610950a2368 100644 (file)
@@ -1,9 +1,11 @@
 # coding: utf-8
 import json
 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,
                 'subtitle': doujinshi.info.subtitle}
     if doujinshi.info.date:
@@ -28,6 +30,48 @@ def serialize(doujinshi, dir):
         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():
     lst = []
     output_dir = "./"
index f9f5946ce4f201a3f4e4652b309c0cd85d10739b..421abb84d3e7fa7f5092fa43025cd0e6f9706904 100644 (file)
@@ -12,7 +12,7 @@ import sqlite3
 
 from nhentai import constant
 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):
@@ -86,7 +86,7 @@ def generate_html(output_dir='.', doujinshi_obj=None):
     js = readfile('viewer/scripts.js')
 
     if doujinshi_obj is not None:
-        serialize(doujinshi_obj, doujinshi_dir)
+        serialize_json(doujinshi_obj, doujinshi_dir)
         name = doujinshi_obj.name
         if sys.version_info < (3, 0):
             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:
                 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:
-        logger.warning('Writen HTML Viewer failed ({})'.format(str(e)))
+        logger.warning('Writing HTML Viewer failed ({})'.format(str(e)))
 
 
 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)
     if image_html == '':
-        logger.warning('None index.html found, --gen-main paused.')
+        logger.warning('No index.html found, --gen-main paused.')
         return
     try:
         data = main.format(STYLES=css, SCRIPTS=js, PICTURE=image_html)
@@ -163,14 +163,15 @@ def generate_main_html(output_dir='./'):
         shutil.copy(os.path.dirname(__file__)+'/viewer/logo.png', './')
         set_js_database()
         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:
-        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):
     if doujinshi_obj is not None:
         doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
+        serialize_comicxml(doujinshi_obj, doujinshi_dir)
         cbz_filename = os.path.join(os.path.join(doujinshi_dir, '..'), '{}.cbz'.format(doujinshi_obj.filename))
     else:
         cbz_filename = './doujinshi.cbz'
@@ -188,7 +189,7 @@ def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
     if rm_origin_dir:
         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):
index 2b55b04482214ecfe34c81481ce50864047bd58c..3fbad9d9ea794df26a0fbbcbbb242627aaf566e8 100644 (file)
@@ -4,3 +4,4 @@ BeautifulSoup4>=4.0.0
 threadpool>=1.2.7
 tabulate>=0.7.5
 future>=0.15.2
+iso8601 >= 0.1