From 18b22de0fa0a454cafe9e6d9f3b2cbe8cd7ad559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=A4=E4=B8=81=E4=B8=81?= Date: Wed, 28 Aug 2019 02:38:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20ZhConvert=5FBata=20?= =?UTF-8?q?=E7=95=B0=E6=AD=A5=E8=99=95=E7=90=86=E6=A8=A1=E7=B5=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/zhconvert.py | 95 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/modules/zhconvert.py b/modules/zhconvert.py index cf2cad6..60c3699 100644 --- a/modules/zhconvert.py +++ b/modules/zhconvert.py @@ -1,8 +1,12 @@ # 繁化姬模組 +import asyncio import json +import aiohttp import requests -from modules.utils.error import (RequestError, ZhconvertKeyNotFound, + +from modules.utils.error import (AsyncRequestError, RequestError, + ZhconvertKeyNotFound, ZhConvertMissNecessarykey) @@ -16,7 +20,8 @@ def __init__(self): def __request(self, endpoint: str, playload): with requests.get(f'{self.api}{endpoint}', data=playload) as req: if req.status_code != 200: - raise RequestError(f'zhconvert Request error. status code: {req.status_code}') + raise RequestError( + f'zhconvert Request error. status code: {req.status_code}') req.encoding = 'utf-8' return json.loads(req.text) @@ -94,3 +99,89 @@ def text(self): if self.convert_obj['code'] != 0: return None return self.convert_obj['data']['text'] + + +class ZhConvert_Bata: + """繁化姬異步處理模組 + + """ + + def __init__(self): + self.api = 'https://api.zhconvert.org' + + async def fetch(self, session, endpoint: str, chapter: dict): + ''' + coroutine function to fetch data from the API. + + Arguments: + session {aiohttp object} -- aiohttp client session. + endpoint {str} -- api endpoint. + ''' + response_content = [] + if chapter.get('filename', None) is None or chapter.get('content', None) is None: + raise AsyncRequestError('Miss kwargs') + paragraph_content = self.paragraph(chapter.get('content', None)) + for content in paragraph_content: + playload = { + 'text': content, + 'converter': chapter.get('converter', None) + } + async with session.get(f'{self.api}{endpoint}', data=playload) as response: + if response.status != 200: + raise AsyncRequestError( + f'zhconvert Request error. status code: {response.status}') + resp = await response.json() + if resp['code'] == 0: + response_content.append(resp['data']['text']) + else: + raise AsyncRequestError( + f'zhconvert convert error. return code: {resp["code"]}') + return {'filename': chapter['filename'], 'content': response_content} + + + def paragraph(self, content): + ''' + ''' + paragraph_content:str = [] + if len(content) > 50_000: + paragraph_count = len(content)/50_000 + for i in range(0, int(paragraph_count)+1): + paragraph_content.append(content[50_000*(i):50_000*(i+1)]) + else: + paragraph_content.append(content) + return paragraph_content + + async def async_convert(self, **args): + ''' + coroutine function to convert content from the epub file. + + Necessary Arguments: + book {json} - chapters of book. + converter {str} - zhconvert convert mode. + ''' + allow_keys = [ + 'book', + 'converter', + 'ignoreTextStyles', + 'jpTextStyles', + 'jpStyleConversionStrategy', + 'jpTextConversionStrategy', + 'modules', + 'userPostReplace', + 'userPreReplace', + 'userProtectReplace', + ] + tasks = [] + error_key = [key for key in args.keys() if key not in allow_keys] + if error_key: + raise ZhconvertKeyNotFound(', '.join(error_key)) + if args.get('book', None) is None or args.get('converter', None) is None: + raise ZhConvertMissNecessarykey() + async with aiohttp.ClientSession() as session: + for chapter in args.get('book'): + chapter['converter'] = args.get('converter') + task = asyncio.create_task( + self.fetch(session, '/convert', chapter)) + tasks.append(task) + async_response = await asyncio.gather(*tasks) + return async_response