feat: added some invalid json responses, which are ok

This commit is contained in:
Hellow 2024-04-25 22:42:40 +02:00
parent 9018e80dc3
commit 2080c21898
2 changed files with 18 additions and 4 deletions

View File

@ -2,11 +2,11 @@ import requests
import logging import logging
from urllib.parse import urlparse, urlunparse, parse_qs, urlencode from urllib.parse import urlparse, urlunparse, parse_qs, urlencode
import re import re
from typing import Optional, List, Dict, Union, Any, Callable from typing import Optional, List, Dict, Union, Any, Callable, Set
import json import json
from functools import wraps from functools import wraps
from .exceptions import SponsorBlockError, SponsorBlockIdNotFoundError from .exceptions import SponsorBlockError, SponsorBlockIdNotFoundError, ReturnDefault
from .constants import Segment from .constants import Segment
@ -19,6 +19,9 @@ def error_handling(default: Any) -> Callable:
try: try:
return func(self, *args, **kwargs) return func(self, *args, **kwargs)
except SponsorBlockError as e: except SponsorBlockError as e:
if isinstance(e, ReturnDefault):
return default
if not self.silent: if not self.silent:
raise e raise e
@ -61,7 +64,12 @@ class SponsorBlock:
else: else:
return query_stuff["v"][0] return query_stuff["v"][0]
def _request(self, method: str, endpoint: str) -> Union[List, Dict]: def _request(self, method: str, endpoint: str, return_default_at_response: List[str] = None) -> Union[List, Dict]:
valid_responses: Set[str] = set([
"Not Found",
])
valid_responses.update(return_default_at_response or [])
error_message = "" error_message = ""
url = self.base_url + endpoint url = self.base_url + endpoint
@ -79,11 +87,14 @@ class SponsorBlock:
if r.status_code == 400: if r.status_code == 400:
self.logger.warning(f"{url} returned 400, meaning I did something wrong.") self.logger.warning(f"{url} returned 400, meaning I did something wrong.")
if r.text in valid_responses:
raise exceptions.ReturnDefault()
data = {} data = {}
try: try:
data = r.json() data = r.json()
except json.JSONDecodeError: except json.JSONDecodeError:
raise exceptions.SponsorBlockConnectionError(f"{r.content} is invalid json.") raise exceptions.SponsorBlockConnectionError(f"{r.text} is invalid json.")
return data return data

View File

@ -6,3 +6,6 @@ class SponsorBlockIdNotFoundError(SponsorBlockError):
class SponsorBlockConnectionError(SponsorBlockError): class SponsorBlockConnectionError(SponsorBlockError):
pass pass
class ReturnDefault(SponsorBlockError):
pass