mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-04-20 11:01:17 +02:00
update search_parse and use tabulate
This commit is contained in:
parent
b85af08ca0
commit
3da4418f22
@ -27,7 +27,7 @@ def cmd_parser():
|
|||||||
help='page number of search result')
|
help='page number of search result')
|
||||||
parser.add_option('--path', type='string', dest='saved_path', action='store', default='',
|
parser.add_option('--path', type='string', dest='saved_path', action='store', default='',
|
||||||
help='path which save the dojinshi')
|
help='path which save the dojinshi')
|
||||||
parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=1,
|
parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
|
||||||
help='thread count of download dojinshi')
|
help='thread count of download dojinshi')
|
||||||
parser.add_option('--timeout', type='int', dest='timeout', action='store', default=30,
|
parser.add_option('--timeout', type='int', dest='timeout', action='store', default=30,
|
||||||
help='timeout of download dojinshi')
|
help='timeout of download dojinshi')
|
||||||
|
@ -19,10 +19,9 @@ def main():
|
|||||||
|
|
||||||
if options.keyword:
|
if options.keyword:
|
||||||
dojinshis = search_parser(options.keyword, options.page)
|
dojinshis = search_parser(options.keyword, options.page)
|
||||||
|
print_dojinshi(dojinshis)
|
||||||
if options.is_download:
|
if options.is_download:
|
||||||
dojinshi_ids = map(lambda d: d['id'], dojinshis)
|
dojinshi_ids = map(lambda d: d['id'], dojinshis)
|
||||||
else:
|
|
||||||
print_dojinshi(dojinshis)
|
|
||||||
else:
|
else:
|
||||||
dojinshi_ids = options.ids
|
dojinshi_ids = options.ids
|
||||||
|
|
||||||
@ -31,7 +30,6 @@ def main():
|
|||||||
dojinshi_info = dojinshi_parser(id)
|
dojinshi_info = dojinshi_parser(id)
|
||||||
dojinshi_list.append(Dojinshi(**dojinshi_info))
|
dojinshi_list.append(Dojinshi(**dojinshi_info))
|
||||||
else:
|
else:
|
||||||
logger.log(15, 'Nothing has been done.')
|
|
||||||
raise SystemExit
|
raise SystemExit
|
||||||
|
|
||||||
if options.is_download:
|
if options.is_download:
|
||||||
|
@ -61,7 +61,7 @@ class Downloader(object):
|
|||||||
if not os.path.exists(folder):
|
if not os.path.exists(folder):
|
||||||
logger.warn('Path \'%s\' not exist.' % folder)
|
logger.warn('Path \'%s\' not exist.' % folder)
|
||||||
try:
|
try:
|
||||||
os.mkdir(folder)
|
os.makedirs(folder)
|
||||||
except EnvironmentError as e:
|
except EnvironmentError as e:
|
||||||
logger.critical('Error: %s' % str(e))
|
logger.critical('Error: %s' % str(e))
|
||||||
raise SystemExit
|
raise SystemExit
|
||||||
|
@ -5,6 +5,7 @@ import requests
|
|||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from constant import DETAIL_URL, SEARCH_URL
|
from constant import DETAIL_URL, SEARCH_URL
|
||||||
from logger import logger
|
from logger import logger
|
||||||
|
from tabulate import tabulate
|
||||||
|
|
||||||
|
|
||||||
def dojinshi_parser(id):
|
def dojinshi_parser(id):
|
||||||
@ -54,10 +55,11 @@ def search_parser(keyword, page):
|
|||||||
result = []
|
result = []
|
||||||
response = requests.get(SEARCH_URL, params={'q': keyword, 'page': page}).content
|
response = requests.get(SEARCH_URL, params={'q': keyword, 'page': page}).content
|
||||||
html = BeautifulSoup(response)
|
html = BeautifulSoup(response)
|
||||||
dojinshi_search_result = html.find_all('div', attrs={'class': 'preview-container'})
|
dojinshi_search_result = html.find_all('div', attrs={'class': 'gallery'})
|
||||||
for dojinshi in dojinshi_search_result:
|
for dojinshi in dojinshi_search_result:
|
||||||
dojinshi_container = dojinshi.find('div', attrs={'class': 'caption'})
|
dojinshi_container = dojinshi.find('div', attrs={'class': 'caption'})
|
||||||
title = dojinshi_container.text.strip()
|
title = dojinshi_container.text.strip()
|
||||||
|
title = (title[:85] + '..') if len(title) > 85 else title
|
||||||
id_ = re.search('/g/(\d+)/', dojinshi.a['href']).group(1)
|
id_ = re.search('/g/(\d+)/', dojinshi.a['href']).group(1)
|
||||||
result.append({'id': id_, 'title': title})
|
result.append({'id': id_, 'title': title})
|
||||||
return result
|
return result
|
||||||
@ -66,12 +68,10 @@ def search_parser(keyword, page):
|
|||||||
def print_dojinshi(dojinshi_list):
|
def print_dojinshi(dojinshi_list):
|
||||||
if not dojinshi_list:
|
if not dojinshi_list:
|
||||||
return
|
return
|
||||||
logger.log(15, 'Print dojinshi list')
|
dojinshi_list = [i.values() for i in dojinshi_list]
|
||||||
print('-' * 60)
|
headers = ['id', 'dojinshi']
|
||||||
for dojinshi in dojinshi_list:
|
logger.info('Search Result\n' +
|
||||||
print(dojinshi['id'], '-', dojinshi['title'])
|
tabulate(tabular_data=dojinshi_list, headers=headers, tablefmt='rst'))
|
||||||
print('-' * 60)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print(dojinshi_parser(32271))
|
print(dojinshi_parser("32271"))
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
requests>=2.5.0
|
requests>=2.5.0
|
||||||
BeautifulSoup4>=4.0.0
|
BeautifulSoup4>=4.0.0
|
||||||
threadpool>=1.2.7
|
threadpool>=1.2.7
|
||||||
|
tabulate>=0.7.5
|
Loading…
x
Reference in New Issue
Block a user