Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加域名fallback,以应对蓝奏云某些域名有时候会短暂挂掉的情况 #60

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 52 additions & 12 deletions lanzou/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,60 @@ def __init__(self):
disable_warnings(InsecureRequestWarning) # 全局禁用 SSL 警告

def _get(self, url, **kwargs):
try:
kwargs.setdefault('timeout', self._timeout)
kwargs.setdefault('headers', self._headers)
return self._session.get(url, verify=False, **kwargs)
except (ConnectionError, requests.RequestException):
return None
for possiable_url in self.all_possiable_urls(url):
try:
kwargs.setdefault('timeout', self._timeout)
kwargs.setdefault('headers', self._headers)
return self._session.get(possiable_url, verify=False, **kwargs)
except (ConnectionError, requests.RequestException):
logger.debug(f"_get({possiable_url}) failed, try another domain")

return None

def _post(self, url, data, **kwargs):
try:
kwargs.setdefault('timeout', self._timeout)
kwargs.setdefault('headers', self._headers)
return self._session.post(url, data, verify=False, **kwargs)
except (ConnectionError, requests.RequestException):
return None
for possiable_url in self.all_possiable_urls(url):
try:
kwargs.setdefault('timeout', self._timeout)
kwargs.setdefault('headers', self._headers)
return self._session.post(possiable_url, data, verify=False, **kwargs)
except (ConnectionError, requests.RequestException):
logger.debug(f"_post({possiable_url}, {data}) failed, try another domain")

return None

def all_possiable_urls(self, lanzouyun_url:str)-> List[str]:
if self._host_url not in lanzouyun_url:
return [lanzouyun_url]

old_domain = 'pan.lanzous'

return [
# 首先尝试传入的url
lanzouyun_url,

# 目前网盘默认分享链接是这个,后面可以根据经验,哪个最靠谱,调整先后顺序
lanzouyun_url.replace(old_domain, 'wwx.lanzoui'),

# 本地测试当前可用的域名,后续可以调整
lanzouyun_url.replace(old_domain, 'pan.lanzoui'),
lanzouyun_url.replace(old_domain, 'up.lanzoui'),
lanzouyun_url.replace(old_domain, 'wws.lanzoui'),
lanzouyun_url.replace(old_domain, 'www.lanzoui'),

lanzouyun_url.replace(old_domain, 'pan.lanzoux'),
lanzouyun_url.replace(old_domain, 'up.lanzoux'),
lanzouyun_url.replace(old_domain, 'wws.lanzoux'),
lanzouyun_url.replace(old_domain, 'www.lanzoux'),
lanzouyun_url.replace(old_domain, 'wwx.lanzoux'),

lanzouyun_url.replace(old_domain, 'wwx.lanzous'),

# 其余备用的域名,测试时暂时不可用
lanzouyun_url.replace(old_domain, 'pan.lanzous'),
lanzouyun_url.replace(old_domain, 'up.lanzous'),
lanzouyun_url.replace(old_domain, 'wws.lanzous'),
lanzouyun_url.replace(old_domain, 'www.lanzous'),
]

def ignore_limits(self):
"""解除官方限制"""
Expand Down