add sqlite3 db to save download history

This commit is contained in:
RicterZ 2020-04-09 21:07:20 +08:00
parent 49f07de95d
commit ca713197cc
4 changed files with 52 additions and 10 deletions

View 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()

View 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
if options.is_download and doujinshis:
print_doujinshi(doujinshis)
if options.is_download and 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)

View 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 = {}

View 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]