Skip to content

Commit

Permalink
改进Cookies提取功能: 即使遇到异常也尝试完所有的浏览器
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuukiy committed Nov 19, 2023
1 parent ae59dfe commit a0d874c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
29 changes: 22 additions & 7 deletions core/chromium.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
"""解析Chromium系浏览器Cookies的相关函数"""
import os
import sys
import json
import base64
import sqlite3
import logging
from glob import glob
from shutil import copyfile
from datetime import datetime

__all__ = ['get_browsers_cookies']


import win32crypt
from Crypto.Cipher import AES

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import core.config # to init the logging module

__all__ = ['get_browsers_cookies']
logger = logging.getLogger(__name__)


class Decrypter():
Expand Down Expand Up @@ -39,6 +46,7 @@ def get_browsers_cookies():
}
LocalAppDataDir = os.getenv('LOCALAPPDATA')
all_browser_cookies = []
exceptions = []
for brw, path in user_data_dirs.items():
user_dir = LocalAppDataDir + path
cookies_files = glob(user_dir+'/*/Cookies') + glob(user_dir+'/*/Network/Cookies')
Expand All @@ -48,12 +56,19 @@ def get_browsers_cookies():
decrypter = Decrypter(key)
for file in cookies_files:
profile = brw + ": " + file.split('User Data')[1].split(os.sep)[1]
records = get_cookies(file, decrypter)
if records:
# 将records转换为便于使用的格式
for site, cookies in records.items():
entry = {'profile': profile, 'site': site, 'cookies': cookies}
all_browser_cookies.append(entry)
file = os.path.normpath(file)
try:
records = get_cookies(file, decrypter)
if records:
# 将records转换为便于使用的格式
for site, cookies in records.items():
entry = {'profile': profile, 'site': site, 'cookies': cookies}
all_browser_cookies.append(entry)
except Exception as e:
exceptions.append(e)
logger.debug(f"无法解析Cookies文件({e}): {file}", exc_info=True)
if len(all_browser_cookies) == 0 and len(exceptions) > 0:
raise exceptions[0]
return all_browser_cookies


Expand Down
2 changes: 1 addition & 1 deletion core/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ retry = 3
timeout = 10

# 要使用的爬虫列表(汇总数据时从前到后进行)
# airav avsox fanza fc2 fc2fan javbus javdb javlib javmenu jav321 mgstage prestige
# airav avsox avwiki fanza fc2 fc2fan javbus javdb javlib javmenu jav321 msin mgstage prestige
[CrawlerSelect]
normal = airav,avsox,javbus,javdb,javlib,jav321,mgstage,prestige
fc2 = fc2,fc2fan,javdb,msin,javmenu
Expand Down
2 changes: 1 addition & 1 deletion core/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def get_remaining_path_len(path):
# Windows: If the length exceeds ~256 characters, you will be able to see the path/files via Windows/File Explorer, but may not be able to delete/move/rename these paths/files
if cfg.NamingRule.calc_path_len_by_byte == 'auto':
is_remote = is_remote_drive(path)
logger.debug(f"目标路径{['', '不是'][is_remote]}远程文件系统")
logger.debug(f"目标路径{['不是', ''][is_remote]}远程文件系统")
cfg.NamingRule.calc_path_len_by_byte = is_remote
length = len(fullpath.encode('utf-8')) if cfg.NamingRule.calc_path_len_by_byte else len(fullpath)
remaining = cfg.NamingRule.max_path_len - length
Expand Down
6 changes: 2 additions & 4 deletions web/javdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ def get_html_wrapper(url):
try:
cookies_pool = get_browsers_cookies()
except (PermissionError, OSError) as e:
logger.warning('无法从浏览器Cookies文件获取JavDB的登录凭据(可能是安全软件在保护浏览器Cookies文件)')
logger.debug(e, exc_info=True)
logger.warning(f"无法从浏览器Cookies文件获取JavDB的登录凭据({e}),可能是安全软件在保护浏览器Cookies文件", exc_info=True)
cookies_pool = []
except Exception as e:
logger.warning('获取JavDB的登录凭据时出错,你可能使用的是国内定制版等非官方Chrome系浏览器')
logger.debug(e, exc_info=True)
logger.warning(f"获取JavDB的登录凭据时出错({e}),你可能使用的是国内定制版等非官方Chrome系浏览器", exc_info=True)
cookies_pool = []
if len(cookies_pool) > 0:
item = cookies_pool.pop()
Expand Down

0 comments on commit a0d874c

Please sign in to comment.