|
@@ -3,6 +3,7 @@
|
|
|
# File : vod_tool.py
|
|
|
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
|
|
|
# Date : 2024/2/5
|
|
|
+import re
|
|
|
|
|
|
import ujson
|
|
|
from time import time
|
|
@@ -55,11 +56,19 @@ def base_request(_url, _object, _js_type=0, cloudfare=False):
|
|
|
timeout = round(timeout / 1000, 2)
|
|
|
# print(f'timeout:{timeout}')
|
|
|
body = _object.get('body') or ''
|
|
|
+ encoding = _object.get('encoding') or 'utf-8'
|
|
|
data = _object.get('data') or {}
|
|
|
- headers = _object.get('headers') or {}
|
|
|
- headers = dict(headers)
|
|
|
- for key, value in headers.items():
|
|
|
- headers[key] = str(value)
|
|
|
+ _headers = _object.get('headers') or {}
|
|
|
+ _headers = dict(_headers)
|
|
|
+ headers = {}
|
|
|
+ for key, value in _headers.items():
|
|
|
+ headers[str(key).lower()] = str(value)
|
|
|
+
|
|
|
+ if headers.get('content-type') and re.search('charset=(.*)', headers['content-type'], re.I):
|
|
|
+ try:
|
|
|
+ encoding = re.search('charset=(.*)', headers['content-type'], re.I).groups()[0]
|
|
|
+ except:
|
|
|
+ pass
|
|
|
|
|
|
if body and not data:
|
|
|
if '&' in body:
|
|
@@ -74,21 +83,22 @@ def base_request(_url, _object, _js_type=0, cloudfare=False):
|
|
|
|
|
|
elif not body and data and method != 'get':
|
|
|
content_type_keys = [key for key in headers if key.lower() == 'content-type']
|
|
|
- content_type = 'application/json'
|
|
|
+ default_type = 'application/json'
|
|
|
+ content_type = default_type
|
|
|
if content_type_keys:
|
|
|
content_type_key = content_type_keys[-1]
|
|
|
old_content_type = headers[content_type_key]
|
|
|
- if content_type not in old_content_type:
|
|
|
- headers[content_type_key] = content_type
|
|
|
+ # if content_type not in old_content_type:
|
|
|
+ # headers[content_type_key] = content_type
|
|
|
+ content_type = old_content_type
|
|
|
else:
|
|
|
- headers['Content-Type'] = content_type
|
|
|
+ headers['Content-Type'] = default_type
|
|
|
|
|
|
- if isinstance(data, dict):
|
|
|
+ if isinstance(data, dict) and default_type in content_type:
|
|
|
data = ujson.dumps(data, ensure_ascii=False)
|
|
|
- encoding = _object.get('encoding') or 'utf-8'
|
|
|
+
|
|
|
buffer = _object.get('buffer') or 1
|
|
|
redirect = False if _object.get('redirect') == 0 or _object.get('redirect') == False else True
|
|
|
-
|
|
|
withHeaders = bool(_object.get('withHeaders') or False)
|
|
|
r = None
|
|
|
r_text = ''
|