From d8e4f50609d115b516e4428a0c792794accf9620 Mon Sep 17 00:00:00 2001 From: Ricter Zheng Date: Tue, 20 Feb 2024 10:25:44 +0800 Subject: [PATCH] support #291 --- nhentai/cmdline.py | 4 +++- nhentai/command.py | 4 ++++ nhentai/constant.py | 2 ++ nhentai/parser.py | 25 ++++++++++++++++++++----- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py index c71336f..9687d71 100644 --- a/nhentai/cmdline.py +++ b/nhentai/cmdline.py @@ -73,6 +73,8 @@ def cmd_parser(): help='search doujinshi by keyword') parser.add_option('--favorites', '-F', action='store_true', dest='favorites', help='list or download your favorites') + parser.add_option('--artist', '-a', action='store', dest='artist', + help='list doujinshi by artist name') # page options parser.add_option('--page-all', dest='page_all', action='store_true', default=False, @@ -216,7 +218,7 @@ def cmd_parser(): parser.print_help() sys.exit(1) - if not args.keyword and not args.id and not args.favorites: + if not args.keyword and not args.id and not args.favorites and not args.artist: parser.print_help() sys.exit(1) diff --git a/nhentai/command.py b/nhentai/command.py index 300136d..38f87e2 100644 --- a/nhentai/command.py +++ b/nhentai/command.py @@ -57,6 +57,10 @@ def main(): doujinshis = _search_parser(options.keyword, sorting=options.sorting, page=page_list, is_page_all=options.page_all) + elif options.artist: + doujinshis = legacy_search_parser(options.artist, sorting=options.sorting, page=page_list, + is_page_all=options.page_all, type_='ARTIST') + elif not doujinshi_ids: doujinshi_ids = options.id diff --git a/nhentai/constant.py b/nhentai/constant.py index eb80b5e..3d677ad 100644 --- a/nhentai/constant.py +++ b/nhentai/constant.py @@ -11,12 +11,14 @@ BASE_URL = os.getenv('NHENTAI', 'https://nhentai.net') DETAIL_URL = f'{BASE_URL}/g' LEGACY_SEARCH_URL = f'{BASE_URL}/search/' SEARCH_URL = f'{BASE_URL}/api/galleries/search' +ARTIST_URL = f'{BASE_URL}/artist/' TAG_API_URL = f'{BASE_URL}/api/galleries/tagged' LOGIN_URL = f'{BASE_URL}/login/' CHALLENGE_URL = f'{BASE_URL}/challenge' FAV_URL = f'{BASE_URL}/favorites/' + IMAGE_URL = f'{urlparse(BASE_URL).scheme}://i.{urlparse(BASE_URL).hostname}/galleries' NHENTAI_HOME = os.path.join(os.getenv('HOME', tempfile.gettempdir()), '.nhentai') diff --git a/nhentai/parser.py b/nhentai/parser.py index 361d29d..064822b 100644 --- a/nhentai/parser.py +++ b/nhentai/parser.py @@ -135,6 +135,7 @@ def doujinshi_parser(id_, counter=0): logger.warning(f'Error: {e}, ignored') return None + print(response) html = BeautifulSoup(response, 'html.parser') doujinshi_info = html.find('div', attrs={'id': 'info'}) @@ -240,13 +241,21 @@ def print_doujinshi(doujinshi_list): print(tabulate(tabular_data=doujinshi_list, headers=headers, tablefmt='rst')) -def legacy_search_parser(keyword, sorting, page, is_page_all=False): +def legacy_search_parser(keyword, sorting, page, is_page_all=False, type_='SEARCH'): logger.info(f'Searching doujinshis of keyword {keyword}') result = [] + if type_ not in ('SEARCH', 'ARTIST', ): + raise ValueError('Invalid type') + if is_page_all: - response = request('get', url=constant.LEGACY_SEARCH_URL, - params={'q': keyword, 'page': 1, 'sort': sorting}).content + if type_ == 'SEARCH': + response = request('get', url=constant.LEGACY_SEARCH_URL, + params={'q': keyword, 'page': 1, 'sort': sorting}).content + else: + url = constant.ARTIST_URL + keyword + '/' + ('' if sorting == 'recent' else sorting) + response = request('get', url=url, params={'page': 1}).content + html = BeautifulSoup(response, 'lxml') pagination = html.find(attrs={'class': 'pagination'}) last_page = pagination.find(attrs={'class': 'last'}) @@ -258,8 +267,14 @@ def legacy_search_parser(keyword, sorting, page, is_page_all=False): for p in pages: logger.info(f'Fetching page {p} ...') - response = request('get', url=constant.LEGACY_SEARCH_URL, - params={'q': keyword, 'page': p, 'sort': sorting}).content + if type_ == 'SEARCH': + response = request('get', url=constant.LEGACY_SEARCH_URL, + params={'q': keyword, 'page': p, 'sort': sorting}).content + else: + url = constant.ARTIST_URL + keyword + '/' + ('' if sorting == 'recent' else sorting) + print(url) + response = request('get', url=url, params={'page': p}).content + if response is None: logger.warning(f'No result in response in page {p}') continue