]> git.lizzy.rs Git - nhentai.git/commitdiff
add sqlite3 db to save download history
authorRicterZ <ricterzheng@gmail.com>
Thu, 9 Apr 2020 13:07:20 +0000 (21:07 +0800)
committerRicterZ <ricterzheng@gmail.com>
Thu, 9 Apr 2020 13:07:20 +0000 (21:07 +0800)
nhentai/cmdline.py
nhentai/command.py
nhentai/constant.py
nhentai/utils.py

index 1a19de623cd6ebe4531a806b5ef967405c176636..9decc8810c77407dabdcff7068152a772a04589c 100644 (file)
@@ -10,7 +10,7 @@ except ImportError:
 
 import nhentai.constant as constant
 from nhentai import __version__
-from nhentai.utils import urlparse, generate_html, generate_main_html
+from nhentai.utils import urlparse, generate_html, generate_main_html, DB
 from nhentai.logger import logger
 
 try:
@@ -101,8 +101,10 @@ def cmd_parser():
     # nhentai options
     parser.add_option('--cookie', type='str', dest='cookie', action='store',
                       help='set cookie of nhentai to bypass Google recaptcha')
-    parser.add_option('--save-download-states', dest='is_save_download_states', action='store_true',
+    parser.add_option('--save-download-history', dest='is_save_download_history', action='store_true',
                       default=False, help='save downloaded doujinshis, whose will be skipped if you re-download them')
+    parser.add_option('--clean-download-history', action='store_true', default=False, dest='clean_download_history',
+                      help='clean download history')
 
     try:
         sys.argv = [unicode(i.decode(sys.stdin.encoding)) for i in sys.argv]
@@ -124,6 +126,13 @@ def cmd_parser():
         generate_main_html()
         exit(0)
 
+    if args.clean_download_history:
+        with DB() as db:
+            db.clean_all()
+
+        logger.info('Download history cleaned.')
+        exit(0)
+
     if os.path.exists(constant.NHENTAI_COOKIE):
         with open(constant.NHENTAI_COOKIE, 'r') as f:
             constant.COOKIE = f.read()
index e8ee74e0b82b9957e0b19d136b8c23feea99e9a1..8af01a63db91c069d454af2f63e71191d449c8ed 100644 (file)
@@ -4,15 +4,14 @@ from __future__ import unicode_literals, print_function
 import signal
 import platform
 import time
-import multiprocessing
 
 from nhentai.cmdline import cmd_parser, banner
-from nhentai.parser import doujinshi_parser, search_parser, print_doujinshi, favorites_parser, tag_parser, login
+from nhentai.parser import doujinshi_parser, search_parser, print_doujinshi, favorites_parser, tag_parser
 from nhentai.doujinshi import Doujinshi
-from nhentai.downloader import Downloader, init_worker
+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, check_cookie, signal_handler
+from nhentai.utils import generate_html, generate_cbz, generate_main_html, check_cookie, signal_handler, DB
 
 
 def main():
@@ -62,13 +61,15 @@ def main():
     elif not doujinshi_ids:
         doujinshi_ids = options.id
 
+    print_doujinshi(doujinshis)
     if options.is_download and doujinshis:
-        print_doujinshi(doujinshis)
         doujinshi_ids = [i['id'] for i in doujinshis]
 
-        if options.is_save_download_states:
-            # TODO:
-            pass
+        if options.is_save_download_history:
+            with DB() as db:
+                data = set(db.get_all())
+
+            doujinshi_ids = list(set(doujinshi_ids) - data)
 
     if doujinshi_ids:
         for i, id_ in enumerate(doujinshi_ids):
@@ -91,6 +92,9 @@ def main():
 
             doujinshi.downloader = downloader
             doujinshi.download()
+            if options.is_save_download_history:
+                with DB() as db:
+                    db.add_one(doujinshi.id)
 
             if not options.is_nohtml and not options.is_cbz:
                 generate_html(options.output_dir, doujinshi)
index 4cead2eeab7eeecbd8d81ddfc02ff299fcdc5255..c5c7c530e367f9427f6fee5bec79ee7c8a5885b0 100644 (file)
@@ -35,6 +35,7 @@ IMAGE_URL = '%s://i.%s/galleries' % (u.scheme, u.hostname)
 NHENTAI_HOME = os.path.join(os.getenv('HOME', tempfile.gettempdir()), '.nhentai')
 NHENTAI_PROXY = os.path.join(NHENTAI_HOME, 'proxy')
 NHENTAI_COOKIE = os.path.join(NHENTAI_HOME, 'cookie')
+NHENTAI_HISTORY = os.path.join(NHENTAI_HOME, 'history.sqlite3')
 
 PROXY = {}
 
index 3d7e3e739a894846b0c08d0c6ba2f7da60804f19..f9f5946ce4f201a3f4e4652b309c0cd85d10739b 100644 (file)
@@ -8,6 +8,7 @@ import string
 import zipfile
 import shutil
 import requests
+import sqlite3
 
 from nhentai import constant
 from nhentai.logger import logger
@@ -214,3 +215,30 @@ an invalid filename.
 def signal_handler(signal, frame):
     logger.error('Ctrl-C signal received. Stopping...')
     exit(1)
+
+
+class DB(object):
+    conn = None
+    cur = None
+
+    def __enter__(self):
+        self.conn = sqlite3.connect(constant.NHENTAI_HISTORY)
+        self.cur = self.conn.cursor()
+        self.cur.execute('CREATE TABLE IF NOT EXISTS download_history (id text)')
+        self.conn.commit()
+        return self
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        self.conn.close()
+
+    def clean_all(self):
+        self.cur.execute('DELETE FROM download_history WHERE 1')
+        self.conn.commit()
+
+    def add_one(self, data):
+        self.cur.execute('INSERT INTO download_history VALUES (?)', [data])
+        self.conn.commit()
+
+    def get_all(self):
+        data = self.cur.execute('SELECT id FROM download_history')
+        return [i[0] for i in data]