mirror of
				https://github.com/RicterZ/nhentai.git
				synced 2025-11-04 02:50:55 +01:00 
			
		
		
		
	use config.json
This commit is contained in:
		@@ -2,7 +2,9 @@
 | 
				
			|||||||
from __future__ import print_function
 | 
					from __future__ import print_function
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
from optparse import OptionParser
 | 
					from optparse import OptionParser
 | 
				
			||||||
 | 
					
 | 
				
			||||||
try:
 | 
					try:
 | 
				
			||||||
    from itertools import ifilter as filter
 | 
					    from itertools import ifilter as filter
 | 
				
			||||||
except ImportError:
 | 
					except ImportError:
 | 
				
			||||||
@@ -35,7 +37,29 @@ def banner():
 | 
				
			|||||||
''' % __version__)
 | 
					''' % __version__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def load_config():
 | 
				
			||||||
 | 
					    if not os.path.exists(constant.NHENTAI_CONFIG_FILE):
 | 
				
			||||||
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
 | 
					        with open(constant.NHENTAI_CONFIG_FILE, 'r') as f:
 | 
				
			||||||
 | 
					            constant.CONFIG = json.load(f)
 | 
				
			||||||
 | 
					    except json.JSONDecodeError:
 | 
				
			||||||
 | 
					        logger.error('Failed to load config file.')
 | 
				
			||||||
 | 
					        write_config()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def write_config():
 | 
				
			||||||
 | 
					    if not os.path.exists(constant.NHENTAI_HOME):
 | 
				
			||||||
 | 
					        os.mkdir(constant.NHENTAI_HOME)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with open(constant.NHENTAI_CONFIG_FILE, 'w') as f:
 | 
				
			||||||
 | 
					        f.write(json.dumps(constant.CONFIG))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def cmd_parser():
 | 
					def cmd_parser():
 | 
				
			||||||
 | 
					    load_config()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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]'
 | 
				
			||||||
@@ -128,83 +152,35 @@ def cmd_parser():
 | 
				
			|||||||
        logger.info('Download history cleaned.')
 | 
					        logger.info('Download history cleaned.')
 | 
				
			||||||
        exit(0)
 | 
					        exit(0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if os.path.exists(constant.NHENTAI_COOKIE):
 | 
					    # --- set config ---
 | 
				
			||||||
        with open(constant.NHENTAI_COOKIE, 'r') as f:
 | 
					 | 
				
			||||||
            constant.COOKIE = f.read()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if args.cookie:
 | 
					    if args.cookie:
 | 
				
			||||||
        try:
 | 
					        constant.CONFIG['cookie'] = args.cookie
 | 
				
			||||||
            if not os.path.exists(constant.NHENTAI_HOME):
 | 
					 | 
				
			||||||
                os.mkdir(constant.NHENTAI_HOME)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            with open(constant.NHENTAI_COOKIE, 'w') as f:
 | 
					 | 
				
			||||||
                f.write(args.cookie)
 | 
					 | 
				
			||||||
        except Exception as e:
 | 
					 | 
				
			||||||
            logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
 | 
					 | 
				
			||||||
            exit(1)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        logger.info('Cookie saved.')
 | 
					        logger.info('Cookie saved.')
 | 
				
			||||||
 | 
					        write_config()
 | 
				
			||||||
        exit(0)
 | 
					        exit(0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if os.path.exists(constant.NHENTAI_LANGUAGE) and not args.language:
 | 
					 | 
				
			||||||
        with open(constant.NHENTAI_LANGUAGE, 'r') as f:
 | 
					 | 
				
			||||||
            constant.LANGUAGE = f.read()
 | 
					 | 
				
			||||||
            args.language = f.read()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if args.language:
 | 
					    if args.language:
 | 
				
			||||||
        try:
 | 
					        constant.CONFIG['language'] = args.language
 | 
				
			||||||
            if not os.path.exists(constant.NHENTAI_HOME):
 | 
					        logger.info('LANGUAGE now set to \'{0}\''.format(args.language))
 | 
				
			||||||
                os.mkdir(constant.NHENTAI_HOME)
 | 
					        write_config()
 | 
				
			||||||
 | 
					 | 
				
			||||||
            with open(constant.NHENTAI_LANGUAGE, 'w') as f:
 | 
					 | 
				
			||||||
                f.write(args.language)
 | 
					 | 
				
			||||||
        except Exception as e:
 | 
					 | 
				
			||||||
            logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
 | 
					 | 
				
			||||||
            exit(1)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        logger.info('Default language now is {}.'.format(args.language))
 | 
					 | 
				
			||||||
        exit(0)
 | 
					        exit(0)
 | 
				
			||||||
 | 
					        # TODO: search without language
 | 
				
			||||||
    if args.clean_language:
 | 
					 | 
				
			||||||
        try:
 | 
					 | 
				
			||||||
            if not os.path.exists(constant.NHENTAI_HOME):
 | 
					 | 
				
			||||||
                os.mkdir(constant.NHENTAI_HOME)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            with open(constant.NHENTAI_LANGUAGE, 'w') as f:
 | 
					 | 
				
			||||||
                f.close()
 | 
					 | 
				
			||||||
        except Exception as e:
 | 
					 | 
				
			||||||
            logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
 | 
					 | 
				
			||||||
            exit(1)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        logger.info('Language now is DEFAULT')
 | 
					 | 
				
			||||||
        exit(0)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if os.path.exists(constant.NHENTAI_PROXY):
 | 
					 | 
				
			||||||
        with open(constant.NHENTAI_PROXY, 'r') as f:
 | 
					 | 
				
			||||||
            link = f.read()
 | 
					 | 
				
			||||||
            constant.PROXY = {'http': link, 'https': link}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if args.proxy:
 | 
					    if args.proxy:
 | 
				
			||||||
        try:
 | 
					        proxy_url = urlparse(args.proxy)
 | 
				
			||||||
            if not os.path.exists(constant.NHENTAI_HOME):
 | 
					        if proxy_url.scheme not in ('http', 'https'):
 | 
				
			||||||
                os.mkdir(constant.NHENTAI_HOME)
 | 
					            logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
 | 
				
			||||||
 | 
					        constant.CONFIG['proxy'] = {
 | 
				
			||||||
            proxy_url = urlparse(args.proxy)
 | 
					            'http': args.proxy,
 | 
				
			||||||
            if proxy_url.scheme not in ('http', 'https'):
 | 
					            'https': args.proxy,
 | 
				
			||||||
                logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
 | 
					        }
 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                with open(constant.NHENTAI_PROXY, 'w') as f:
 | 
					 | 
				
			||||||
                    f.write(args.proxy)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        except Exception as e:
 | 
					 | 
				
			||||||
            logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
 | 
					 | 
				
			||||||
            exit(1)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        logger.info('Proxy \'{0}\' saved.'.format(args.proxy))
 | 
					        logger.info('Proxy \'{0}\' saved.'.format(args.proxy))
 | 
				
			||||||
 | 
					        write_config()
 | 
				
			||||||
        exit(0)
 | 
					        exit(0)
 | 
				
			||||||
 | 
					    # --- end set config ---
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if args.favorites:
 | 
					    if args.favorites:
 | 
				
			||||||
        if not constant.COOKIE:
 | 
					        if not constant.CONFIG['cookie']:
 | 
				
			||||||
            logger.warning('Cookie has not been set, please use `nhentai --cookie \'COOKIE\'` to set it.')
 | 
					            logger.warning('Cookie has not been set, please use `nhentai --cookie \'COOKIE\'` to set it.')
 | 
				
			||||||
            exit(1)
 | 
					            exit(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,16 +1,19 @@
 | 
				
			|||||||
#!/usr/bin/env python2.7
 | 
					#!/usr/bin/env python2.7
 | 
				
			||||||
# coding: utf-8
 | 
					# coding: utf-8
 | 
				
			||||||
from __future__ import unicode_literals, print_function
 | 
					from __future__ import unicode_literals, print_function
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
import signal
 | 
					import signal
 | 
				
			||||||
import platform
 | 
					import platform
 | 
				
			||||||
import time
 | 
					import time
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from nhentai import constant
 | 
				
			||||||
from nhentai.cmdline import cmd_parser, banner
 | 
					from nhentai.cmdline import cmd_parser, banner
 | 
				
			||||||
from nhentai.parser import doujinshi_parser, search_parser, print_doujinshi, favorites_parser
 | 
					from nhentai.parser import doujinshi_parser, search_parser, print_doujinshi, favorites_parser
 | 
				
			||||||
from nhentai.doujinshi import Doujinshi
 | 
					from nhentai.doujinshi import Doujinshi
 | 
				
			||||||
from nhentai.downloader import Downloader
 | 
					from nhentai.downloader import Downloader
 | 
				
			||||||
from nhentai.logger import logger
 | 
					from nhentai.logger import logger
 | 
				
			||||||
from nhentai.constant import BASE_URL, LANGUAGE
 | 
					from nhentai.constant import NHENTAI_CONFIG_FILE, 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, \
 | 
				
			||||||
    paging, check_cookie, signal_handler, DB
 | 
					    paging, check_cookie, signal_handler, DB
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -20,10 +23,9 @@ def main():
 | 
				
			|||||||
    options = cmd_parser()
 | 
					    options = cmd_parser()
 | 
				
			||||||
    logger.info('Using mirror: {0}'.format(BASE_URL))
 | 
					    logger.info('Using mirror: {0}'.format(BASE_URL))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    from nhentai.constant import PROXY
 | 
					    # CONFIG['proxy'] will be changed after cmd_parser()
 | 
				
			||||||
    # constant.PROXY will be changed after cmd_parser()
 | 
					    if constant.CONFIG['proxy']:
 | 
				
			||||||
    if PROXY:
 | 
					        logger.info('Using proxy: {0}'.format(constant.CONFIG['proxy']))
 | 
				
			||||||
        logger.info('Using proxy: {0}'.format(PROXY))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # check your cookie
 | 
					    # check your cookie
 | 
				
			||||||
    check_cookie()
 | 
					    check_cookie()
 | 
				
			||||||
@@ -41,9 +43,9 @@ def main():
 | 
				
			|||||||
        doujinshis = favorites_parser(page=page_list)
 | 
					        doujinshis = favorites_parser(page=page_list)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    elif options.keyword:
 | 
					    elif options.keyword:
 | 
				
			||||||
        if LANGUAGE:
 | 
					        if constant.CONFIG['language']:
 | 
				
			||||||
            logger.info('Using default language: {0}'.format(LANGUAGE))
 | 
					            logger.info('Using default language: {0}'.format(constant.CONFIG['language']))
 | 
				
			||||||
            options.keyword += ', language:{}'.format(LANGUAGE)
 | 
					            options.keyword += ' language:{}'.format(constant.CONFIG['language'])
 | 
				
			||||||
        doujinshis = search_parser(options.keyword, sorting=options.sorting, page=page_list,
 | 
					        doujinshis = search_parser(options.keyword, sorting=options.sorting, page=page_list,
 | 
				
			||||||
                                   is_page_all=options.page_all)
 | 
					                                   is_page_all=options.page_all)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,12 +26,12 @@ u = urlparse(BASE_URL)
 | 
				
			|||||||
IMAGE_URL = '%s://i.%s/galleries' % (u.scheme, u.hostname)
 | 
					IMAGE_URL = '%s://i.%s/galleries' % (u.scheme, u.hostname)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
NHENTAI_HOME = os.path.join(os.getenv('HOME', tempfile.gettempdir()), '.nhentai')
 | 
					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_LANGUAGE = os.path.join(NHENTAI_HOME, 'language')
 | 
					 | 
				
			||||||
NHENTAI_HISTORY = os.path.join(NHENTAI_HOME, 'history.sqlite3')
 | 
					NHENTAI_HISTORY = os.path.join(NHENTAI_HOME, 'history.sqlite3')
 | 
				
			||||||
 | 
					NHENTAI_CONFIG_FILE = os.path.join(NHENTAI_HOME, 'config.json')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
PROXY = {}
 | 
					CONFIG = {
 | 
				
			||||||
 | 
					    'proxy': {},
 | 
				
			||||||
 | 
					    'cookie': '',
 | 
				
			||||||
 | 
					    'language': '',
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
COOKIE = ''
 | 
					 | 
				
			||||||
LANGUAGE = ''
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,6 @@
 | 
				
			|||||||
# coding: utf-8
 | 
					# coding: utf-8
 | 
				
			||||||
from __future__ import unicode_literals, print_function
 | 
					from __future__ import unicode_literals, print_function
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import sys
 | 
					 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
import re
 | 
					import re
 | 
				
			||||||
import time
 | 
					import time
 | 
				
			||||||
@@ -204,10 +203,9 @@ def search_parser(keyword, sorting, page, is_page_all=False):
 | 
				
			|||||||
        init_response = request('get', url.replace('%2B', '+')).json()
 | 
					        init_response = request('get', url.replace('%2B', '+')).json()
 | 
				
			||||||
        page = range(1, init_response['num_pages']+1)
 | 
					        page = range(1, init_response['num_pages']+1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    total = '/{0}'.format(page[-1]) if is_page_all else ''
 | 
				
			||||||
    for p in page:
 | 
					    for p in page:
 | 
				
			||||||
        i = 0
 | 
					        i = 0
 | 
				
			||||||
        if is_page_all:
 | 
					 | 
				
			||||||
            total = '/{0}'.format(page[-1])
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        logger.info('Searching doujinshis using keywords "{0}" on page {1}{2}'.format(keyword, p, total))
 | 
					        logger.info('Searching doujinshis using keywords "{0}" on page {1}{2}'.format(keyword, p, total))
 | 
				
			||||||
        while i < 3:
 | 
					        while i < 3:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,9 +20,9 @@ def request(method, url, **kwargs):
 | 
				
			|||||||
    session.headers.update({
 | 
					    session.headers.update({
 | 
				
			||||||
        'Referer': constant.LOGIN_URL,
 | 
					        'Referer': constant.LOGIN_URL,
 | 
				
			||||||
        'User-Agent': 'nhentai command line client (https://github.com/RicterZ/nhentai)',
 | 
					        'User-Agent': 'nhentai command line client (https://github.com/RicterZ/nhentai)',
 | 
				
			||||||
        'Cookie': constant.COOKIE
 | 
					        'Cookie': constant.CONFIG['cookie']
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
    return getattr(session, method)(url, proxies=constant.PROXY, verify=False, **kwargs)
 | 
					    return getattr(session, method)(url, proxies=constant.CONFIG['proxy'], verify=False, **kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def check_cookie():
 | 
					def check_cookie():
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user