From 1af195d7274c91e7437420d0cecfb7eca6b0db82 Mon Sep 17 00:00:00 2001 From: RicterZ Date: Wed, 12 Jun 2019 22:45:44 +0800 Subject: [PATCH] add cookie check --- nhentai/command.py | 5 ++++- nhentai/utils.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/nhentai/command.py b/nhentai/command.py index d45d20f..10176ea 100644 --- a/nhentai/command.py +++ b/nhentai/command.py @@ -11,7 +11,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 +from nhentai.utils import generate_html, generate_cbz, generate_main_html, check_cookie def main(): @@ -24,6 +24,9 @@ def main(): if PROXY != {}: logger.info('Using proxy: {0}'.format(PROXY)) + # check your cookie + check_cookie() + doujinshi_ids = [] doujinshi_list = [] diff --git a/nhentai/utils.py b/nhentai/utils.py index 63db14c..3bdf278 100644 --- a/nhentai/utils.py +++ b/nhentai/utils.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals, print_function import sys +import re import os import string import zipfile @@ -22,6 +23,15 @@ def request(method, url, **kwargs): return getattr(session, method)(url, proxies=constant.PROXY, verify=False, **kwargs) +def check_cookie(): + response = request('get', constant.BASE_URL).text + username = re.findall('"/users/\d+/(.*?)"', response) + if not username: + logger.error('Cannot get your username, please check your cookie or use `nhentai --cookie` to set your cookie') + else: + logger.info('Login successfully! Your username: {}'.format(username[0])) + + class _Singleton(type): """ A metaclass that creates a Singleton base class when called. """ _instances = {}