From 0583c7910cc04773d58e3c2466d6909fb2f8b625 Mon Sep 17 00:00:00 2001 From: ricterz Date: Sat, 9 May 2015 13:48:11 +0800 Subject: [PATCH] search for download --- hentai/cmdline.py | 4 ++-- hentai/constant.py | 3 ++- hentai/downloader.py | 1 - hentai/parser.py | 25 +++++++++++++++++++++---- nhentai.py | 11 +++++++++-- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/hentai/cmdline.py b/hentai/cmdline.py index 957e02c..2674b00 100644 --- a/hentai/cmdline.py +++ b/hentai/cmdline.py @@ -8,7 +8,7 @@ def banner(): print ''' _ _ _ _ _ __ | | | | ___ _ __ | |_ __ _(_) | '_ \| |_| |/ _ \ '_ \| __/ _` | | -| | | | _ | __/ | | | || (_| |w | +| | | | _ | __/ | | | || (_| | | |_| |_|_| |_|\___|_| |_|\__\__,_|_| ''' @@ -30,7 +30,7 @@ def cmd_parser(): _ = map(lambda id: id.strip(), args.ids.split(',')) args.ids = set(map(int, ifilter(lambda id: id.isdigit(), _))) - if args.is_download and not args.id and not args.ids: + if args.is_download and not args.id and not args.ids and not args.keyword: logger.critical('Dojinshi id/ids is required for downloading') parser.print_help() raise SystemExit diff --git a/hentai/constant.py b/hentai/constant.py index 8113a37..476978c 100644 --- a/hentai/constant.py +++ b/hentai/constant.py @@ -1,4 +1,5 @@ SCHEMA = 'http://' URL = '%snhentai.net' % SCHEMA DETAIL_URL = '%s/g' % URL -IMAGE_URL = '%si.nhentai.net/galleries' % SCHEMA \ No newline at end of file +IMAGE_URL = '%si.nhentai.net/galleries' % SCHEMA +SEARCH_URL = '%s/search/' % URL \ No newline at end of file diff --git a/hentai/downloader.py b/hentai/downloader.py index 3747e02..672b23f 100644 --- a/hentai/downloader.py +++ b/hentai/downloader.py @@ -87,4 +87,3 @@ class Downloader(object): # clean threads list self.threads = [] - logger.log(15, u'đŸș All done, saved to \'%s\'!' % folder) diff --git a/hentai/parser.py b/hentai/parser.py index 1224a70..b48fe71 100644 --- a/hentai/parser.py +++ b/hentai/parser.py @@ -2,15 +2,15 @@ import sys import re import requests from bs4 import BeautifulSoup -from constant import DETAIL_URL +from constant import DETAIL_URL, SEARCH_URL from hentai.logger import logger def dojinshi_parser(id): - logger.debug('Fetching dojinshi information') - if not isinstance(id, (int, )) or (isinstance(id, (str, )) and not id.isdigit()): + if not isinstance(id, (int, )) and (isinstance(id, (str, )) and not id.isdigit()): raise Exception('Dojinshi id(%s) is not valid' % str(id)) id = int(id) + logger.debug('Fetching dojinshi information of id %d' % id) dojinshi = dict() dojinshi['id'] = id url = '%s/%d/' % (DETAIL_URL, id) @@ -50,12 +50,29 @@ def dojinshi_parser(id): def search_parser(keyword): logger.debug('Searching dojinshis of keyword %s' % keyword) - return [] + result = [] + response = requests.get(SEARCH_URL, params={'q': keyword}).content + html = BeautifulSoup(response) + dojinshi_search_result = html.find_all('div', attrs={'class': 'preview-container'}) + for dojinshi in dojinshi_search_result: + dojinshi_container = dojinshi.find('div', attrs={'class': 'caption'}) + title = dojinshi_container.text.strip() + id_ = re.search('/g/(\d+)/', dojinshi.a['href']).group(1) + result.append({'id': id_, 'title': title}) + return result def tag_parser(tag): pass +def print_dojinshi(dojinshi_list): + logger.log(15, 'Print Dojinshi list') + print '-' * 60 + for dojinshi in dojinshi_list: + print dojinshi['id'], '-', dojinshi['title'] + print '-' * 60 + + if __name__ == '__main__': print dojinshi_parser(32271) \ No newline at end of file diff --git a/nhentai.py b/nhentai.py index 55d4bdb..37c8ffe 100644 --- a/nhentai.py +++ b/nhentai.py @@ -1,6 +1,6 @@ #coding: utf-8 from hentai.cmdline import cmd_parser, banner -from hentai.parser import dojinshi_parser, search_parser +from hentai.parser import dojinshi_parser, search_parser, print_dojinshi from hentai.dojinshi import Dojinshi from hentai.downloader import Downloader from hentai.logger import logger @@ -15,10 +15,15 @@ def main(): logger.log(15, 'nHentai: あăȘăŸă‚‚ć€‰æ…‹ă€‚ いいね?') + dojinshi_ids = [] dojinshi_list = [] if options.keyword: - dojinshi_ids = search_parser(options.keyword) + dojinshis = search_parser(options.keyword) + if options.is_download: + dojinshi_ids = map(lambda d: d['id'], dojinshis) + else: + print_dojinshi(dojinshis) else: dojinshi_ids = options.ids @@ -38,6 +43,8 @@ def main(): else: map(lambda dojinshi: dojinshi.show(), dojinshi_list) + logger.log(15, u'đŸș All done.') + if __name__ == '__main__': main() \ No newline at end of file