Skip to content

Commit

Permalink
改进获取JavDB Cookies的逻辑,避免无法获取Cookies时影响正常功能的运行
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuukiy committed Dec 9, 2021
1 parent dc19850 commit 6d7dcb9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
7 changes: 3 additions & 4 deletions core/chromium.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ def decrypt_key(local_state):

def get_cookies(cookies_file, decrypter, host_pattern='javdb%.com'):
"""从cookies_file文件中查找指定站点的所有Cookies"""
# 复制Cookies文件到当前目录,避免直接操作原始的Cookies文件
temp_cookie = './Cookies'
# 复制Cookies文件到临时目录,避免直接操作原始的Cookies文件
temp_dir = os.getenv('TMPDIR', os.getenv('TEMP', os.getenv('TMP', '.')))
temp_cookie = os.path.join(temp_dir, 'Cookies')
copyfile(cookies_file, temp_cookie)
# 连接数据库进行查询
conn = sqlite3.connect(temp_cookie)
Expand All @@ -103,8 +104,6 @@ def get_cookies(cookies_file, decrypter, host_pattern='javdb%.com'):

if __name__ == "__main__":
all_cookies = get_browsers_cookies()
# with open('cookies.json', 'wt', encoding='utf-8') as f:
# json.dump(all_cookies, f, indent=2)
for d in all_cookies:
print('{:<20}{}'.format(d['profile'], d['site']))

15 changes: 10 additions & 5 deletions web/javdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,26 @@

def get_html_wrapper(url):
"""包装外发的request请求并负责转换为可xpath的html,同时处理Cookies无效等问题"""
global request
global request, cookies_pool
r = request.get(url, delay_raise=True)
if r.status_code == 200:
# 发生重定向可能仅仅是域名重定向,因此还要检查url以判断是否被跳转到了登录页
if r.history and '/login' in r.url:
# 仅在需要时去读取Cookies
if 'cookies_pool' not in globals():
try:
cookies_pool = get_browsers_cookies()
except Exception as e:
logger.warning('获取JavDB的登录凭据时出错,你可能使用的是绿色版、修改版等非官方Chrome系浏览器')
logger.debug(e, exc_info=True)
cookies_pool = []
if len(cookies_pool) > 0:
item = cookies_pool.pop()
# 更换Cookies时需要创建新的request实例,否则cloudscraper会保留它内部第一次发起网络访问时获得的Cookies
request = Request(use_scraper=True)
request.cookies = item['cookies']
cookies_source = (item['profile'], item['site'])
logger.debug(f'检测到重定向,尝试更换Cookies为: {cookies_source}')
logger.debug(f'未携带有效Cookies而发生重定向,尝试更换Cookies为: {cookies_source}')
return get_html_wrapper(url)
else:
raise Exception(f'JavDB: 所有浏览器Cookies均已过期')
Expand Down Expand Up @@ -164,9 +172,6 @@ def parse_clean_data(movie: MovieInfo):
movie.title = new_title


cookies_pool = get_browsers_cookies()


if __name__ == "__main__":
import pretty_errors
pretty_errors.configure(display_link=True)
Expand Down

0 comments on commit 6d7dcb9

Please sign in to comment.