From b97e707817c0fa1136dbdf96da39db2a645bafd6 Mon Sep 17 00:00:00 2001
From: "DESKTOP-58CH9VE\\Michael" <hiers77@web.de>
Date: Sat, 5 Jun 2021 17:13:18 +0200
Subject: [PATCH] HDoujin-format Metadata file

---
 nhentai/cmdline.py   |  3 +++
 nhentai/command.py   | 11 ++++++++-
 nhentai/doujinshi.py | 19 +++++++++------
 nhentai/utils.py     | 55 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 80 insertions(+), 8 deletions(-)

diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py
index 67140cd..9107c20 100644
--- a/nhentai/cmdline.py
+++ b/nhentai/cmdline.py
@@ -117,6 +117,9 @@ def cmd_parser():
                       help='clean download history')
     parser.add_option('--template', dest='viewer_template', action='store',
                       help='set viewer template', default='')
+	
+    parser.add_option('--meta', dest='generate_metadata', action='store_true', help='generate a Metadata File in HDoujin Format')	
+					  
 
     try:
         sys.argv = [unicode(i.decode(sys.stdin.encoding)) for i in sys.argv]
diff --git a/nhentai/command.py b/nhentai/command.py
index bc987fb..76365d8 100644
--- a/nhentai/command.py
+++ b/nhentai/command.py
@@ -13,7 +13,7 @@ from nhentai.doujinshi import Doujinshi
 from nhentai.downloader import Downloader
 from nhentai.logger import logger
 from nhentai.constant import BASE_URL
-from nhentai.utils import generate_html, generate_cbz, generate_main_html, generate_pdf, \
+from nhentai.utils import generate_html, generate_cbz, generate_main_html, generate_pdf, generate_metadatafile, \
     paging, check_cookie, signal_handler, DB
 
 
@@ -84,14 +84,22 @@ def main():
             if (i + 1) % 10 == 0:
                 logger.info('Progress: %d / %d' % (i + 1, len(doujinshi_ids)))
 
+	
+    		
     if not options.is_show:
         downloader = Downloader(path=options.output_dir, size=options.threads,
                                 timeout=options.timeout, delay=options.delay)
 
         for doujinshi in doujinshi_list:
 
+           					
             doujinshi.downloader = downloader
             doujinshi.download()
+			
+            if options.generate_metadata:
+              table=doujinshi.table            
+              generate_metadatafile(options.output_dir,table,doujinshi)
+			
             if options.is_save_download_history:
                 with DB() as db:
                     db.add_one(doujinshi.id)
@@ -113,6 +121,7 @@ def main():
 
     else:
         [doujinshi.show() for doujinshi in doujinshi_list]
+        
 
 
 signal.signal(signal.SIGINT, signal_handler)
diff --git a/nhentai/doujinshi.py b/nhentai/doujinshi.py
index 0170ad5..06a7706 100644
--- a/nhentai/doujinshi.py
+++ b/nhentai/doujinshi.py
@@ -41,12 +41,9 @@ class Doujinshi(object):
         name_format = name_format.replace('%t', self.name)
         name_format = name_format.replace('%s', self.info.subtitle)
         self.filename = format_filename(name_format)
-
-    def __repr__(self):
-        return '<Doujinshi: {0}>'.format(self.name)
-
-    def show(self):
-        table = [
+		
+		
+        self.table =[
             ["Parodies", self.info.parodies],
             ["Doujinshi", self.name],
             ["Subtitle", self.info.subtitle],
@@ -57,7 +54,15 @@ class Doujinshi(object):
             ["URL", self.url],
             ["Pages", self.pages],
         ]
-        logger.info(u'Print doujinshi information of {0}\n{1}'.format(self.id, tabulate(table)))
+		
+
+    def __repr__(self):
+        return '<Doujinshi: {0}>'.format(self.name)
+
+    def show(self):
+        
+        logger.info(u'Print doujinshi information of {0}\n{1}'.format(self.id, tabulate(self.table)))
+        
 
     def download(self):
         logger.info('Starting to download doujinshi: %s' % self.name)
diff --git a/nhentai/utils.py b/nhentai/utils.py
index 6977297..34c36b7 100644
--- a/nhentai/utils.py
+++ b/nhentai/utils.py
@@ -279,6 +279,58 @@ def paging(page_string):
 
     return page_list
 
+def generate_metadatafile(output_dir, table, doujinshi_obj=None):
+    logger.info("Writing Metadata Info")
+   
+	
+    if doujinshi_obj is not None:
+      doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
+    else:
+      doujinshi_dir = '.'
+    
+    logger.info(doujinshi_dir)	
+	
+    f = open(os.path.join(doujinshi_dir, 'info.txt'), "w", encoding="utf-8")
+
+    fields = ["TITLE", "ORIGINAL TITLE", "AUTHOR", "ARTIST", "CIRCLE", "SCANLATOR", "TRANSLATOR", "PUBLISHER", "DESCRIPTION", "STATUS", "CHAPTERS", "PAGES", "TAGS", "TYPE", "LANGUAGE", "RELEASED", "READING DIRECTION", "CHARACTERS", "SERIES", "PARODY", "URL"]
+
+               
+    for i in range(21):
+      f.write("%s: " % fields[i])
+        
+      if(i==19):
+        f.write("%s" % table[0][1])
+
+      if(i==0):
+        f.write("%s" % table[1][1])
+    
+      if(i==1):
+        f.write("%s" % table[2][1])
+    
+      if(i==17):
+        f.write("%s" % table[3][1])
+    
+      if(i==2):
+        f.write("%s" % table[4][1])
+    
+      if(i==14):
+        f.write("%s" % table[5][1])
+    
+      if(i==12):
+        f.write("%s" % table[6][1])
+    
+      if(i==20):
+        f.write("%s" % table[7][1])
+    
+      if(i==11):
+        f.write("%s" % table[8][1])
+    
+      f.write("\n")
+    
+    f.close()
+    
+    
+    
 
 class DB(object):
     conn = None
@@ -305,3 +357,6 @@ class DB(object):
     def get_all(self):
         data = self.cur.execute('SELECT id FROM download_history')
         return [i[0] for i in data]
+
+        
+        
\ No newline at end of file