Merge pull request #82 from felinae98/bug/catch-json-err

捕获 JSONDecodeError
This commit is contained in:
felinae98 2022-05-29 01:11:08 +08:00 committed by GitHub
commit 9091157d44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 75 deletions

View File

@ -1,3 +1,4 @@
import json
import ssl import ssl
import time import time
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
@ -60,6 +61,25 @@ class Platform(metaclass=RegistryABCMeta, base=True):
) -> list[tuple[User, list[Post]]]: ) -> list[tuple[User, list[Post]]]:
... ...
async def do_fetch_new_post(
self, target: Target, users: list[UserSubInfo]
) -> list[tuple[User, list[Post]]]:
try:
return await self.fetch_new_post(target, users)
except httpx.RequestError as err:
logger.warning(
"network connection error: {}, url: {}".format(
type(err), err.request.url
)
)
return []
except ssl.SSLError as err:
logger.warning(f"ssl error: {err}")
return []
except json.JSONDecodeError as err:
logger.warning(f"json error, parsing: {err.doc}")
return []
@abstractmethod @abstractmethod
async def parse(self, raw_post: RawPost) -> Post: async def parse(self, raw_post: RawPost) -> Post:
... ...
@ -227,33 +247,22 @@ class NewMessage(MessageProcess, abstract=True):
async def fetch_new_post( async def fetch_new_post(
self, target: Target, users: list[UserSubInfo] self, target: Target, users: list[UserSubInfo]
) -> list[tuple[User, list[Post]]]: ) -> list[tuple[User, list[Post]]]:
try: post_list = await self.get_sub_list(target)
post_list = await self.get_sub_list(target) new_posts = await self.filter_common_with_diff(target, post_list)
new_posts = await self.filter_common_with_diff(target, post_list) if not new_posts:
if not new_posts: return []
return [] else:
else: for post in new_posts:
for post in new_posts: logger.info(
logger.info( "fetch new post from {} {}: {}".format(
"fetch new post from {} {}: {}".format( self.platform_name,
self.platform_name, target if self.has_target else "-",
target if self.has_target else "-", self.get_id(post),
self.get_id(post),
)
) )
res = await self.dispatch_user_post(target, new_posts, users)
self.parse_cache = {}
return res
except httpx.RequestError as err:
logger.warning(
"network connection error: {}, url: {}".format(
type(err), err.request.url
) )
) res = await self.dispatch_user_post(target, new_posts, users)
return [] self.parse_cache = {}
except ssl.SSLError as err: return res
logger.warning(f"ssl error: {err}")
return []
class StatusChange(Platform, abstract=True): class StatusChange(Platform, abstract=True):
@ -274,33 +283,22 @@ class StatusChange(Platform, abstract=True):
async def fetch_new_post( async def fetch_new_post(
self, target: Target, users: list[UserSubInfo] self, target: Target, users: list[UserSubInfo]
) -> list[tuple[User, list[Post]]]: ) -> list[tuple[User, list[Post]]]:
try: new_status = await self.get_status(target)
new_status = await self.get_status(target) res = []
res = [] if old_status := self.get_stored_data(target):
if old_status := self.get_stored_data(target): diff = self.compare_status(target, old_status, new_status)
diff = self.compare_status(target, old_status, new_status) if diff:
if diff: logger.info(
logger.info( "status changes {} {}: {} -> {}".format(
"status changes {} {}: {} -> {}".format( self.platform_name,
self.platform_name, target if self.has_target else "-",
target if self.has_target else "-", old_status,
old_status, new_status,
new_status,
)
) )
res = await self.dispatch_user_post(target, diff, users)
self.set_stored_data(target, new_status)
return res
except httpx.RequestError as err:
logger.warning(
"network connection error: {}, url: {}".format(
type(err), err.request.url
) )
) res = await self.dispatch_user_post(target, diff, users)
return [] self.set_stored_data(target, new_status)
except ssl.SSLError as err: return res
logger.warning(f"ssl error: {err}")
return []
class SimplePost(MessageProcess, abstract=True): class SimplePost(MessageProcess, abstract=True):
@ -309,32 +307,21 @@ class SimplePost(MessageProcess, abstract=True):
async def fetch_new_post( async def fetch_new_post(
self, target: Target, users: list[UserSubInfo] self, target: Target, users: list[UserSubInfo]
) -> list[tuple[User, list[Post]]]: ) -> list[tuple[User, list[Post]]]:
try: new_posts = await self.get_sub_list(target)
new_posts = await self.get_sub_list(target) if not new_posts:
if not new_posts: return []
return [] else:
else: for post in new_posts:
for post in new_posts: logger.info(
logger.info( "fetch new post from {} {}: {}".format(
"fetch new post from {} {}: {}".format( self.platform_name,
self.platform_name, target if self.has_target else "-",
target if self.has_target else "-", self.get_id(post),
self.get_id(post),
)
) )
res = await self.dispatch_user_post(target, new_posts, users)
self.parse_cache = {}
return res
except httpx.RequestError as err:
logger.warning(
"network connection error: {}, url: {}".format(
type(err), err.request.url
) )
) res = await self.dispatch_user_post(target, new_posts, users)
return [] self.parse_cache = {}
except ssl.SSLError as err: return res
logger.warning(f"ssl error: {err}")
return []
class NoTargetGroup(Platform, abstract=True): class NoTargetGroup(Platform, abstract=True):

View File

@ -59,7 +59,7 @@ async def fetch_and_send(target_type: str):
send_user_list, send_user_list,
) )
) )
to_send = await platform_manager[target_type].fetch_new_post( to_send = await platform_manager[target_type].do_fetch_new_post(
target, send_userinfo_list target, send_userinfo_list
) )
if not to_send: if not to_send: