77import logging
88import os
99import requests
10+ import http .cookies
1011from bs4 import BeautifulSoup
1112import urllib3
1213import notify # 来自青龙面板默认的通知模块,不需要安装依赖
@@ -47,12 +48,30 @@ def init_proxy():
4748 logging .error (f'初始化代理失败: { e } ' )
4849 send_notify ('初始化失败' , f'初始化代理失败: { e } ' )
4950
51+ def mask_cookie (cookie_str ):
52+ masked_parts = []
53+ for item in cookie_str .split (';' ):
54+ item = item .strip ()
55+ if '=' in item :
56+ key , value = item .split ('=' , 1 )
57+ if len (value ) > 8 :
58+ masked_value = value [:3 ] + '*' * (len (value ) - 6 ) + value [- 3 :]
59+ elif len (value ) > 0 :
60+ masked_value = value [0 ] + '*' * (len (value ) - 1 )
61+ else :
62+ masked_value = ''
63+ masked_parts .append (f"{ key } ={ masked_value } " )
64+ else :
65+ masked_parts .append (item )
66+ return '; ' .join (masked_parts )
67+
5068def init_cookie ():
5169 try :
5270 cookie = os .getenv ('E_COOKIE' )
5371 if cookie :
5472 headers ['Cookie' ] = cookie
55- logging .info (f'使用Cookie: { cookie } ' )
73+ masked_cookie = mask_cookie (cookie )
74+ logging .info (f'使用Cookie: { masked_cookie } ' )
5675 else :
5776 logging .info ('未设置Cookie,将不发送Cookie。' )
5877 except Exception as e :
@@ -71,6 +90,56 @@ def init_UserAgent():
7190 logging .error (f'初始化User-Agent失败: { e } ' )
7291 send_notify ('初始化失败' , f'初始化User-Agent失败: { e } ' )
7392
93+ def parse_set_cookie (set_cookie_headers ):
94+ cookies = {}
95+ expires_info = []
96+
97+ if not isinstance (set_cookie_headers , list ):
98+ set_cookie_headers = [set_cookie_headers ]
99+
100+ for raw_cookie in set_cookie_headers :
101+ morsel = http .cookies .SimpleCookie ()
102+ morsel .load (raw_cookie )
103+ for key , value in morsel .items ():
104+ cookies [key ] = value .value
105+ if 'expires' in value :
106+ expires_info .append ((key , value ['expires' ]))
107+
108+ return cookies , expires_info
109+
110+ def compare_and_update_cookie_env (new_cookies , expires_info ):
111+ cookie_fields = ['ipb_member_id' , 'ipb_pass_hash' , 'sk' , 'hath_perks' , 'nw' , 'event' ]
112+ old_cookie = os .getenv ('E_COOKIE' , '' )
113+
114+ # 解析旧cookie为字典
115+ old_cookie_dict = {}
116+ for item in old_cookie .split (';' ):
117+ if '=' in item :
118+ k , v = item .strip ().split ('=' , 1 )
119+ old_cookie_dict [k ] = v
120+
121+ updated = False
122+ updated_cookie_dict = old_cookie_dict .copy ()
123+
124+ for field in cookie_fields :
125+ if field in new_cookies :
126+ if old_cookie_dict .get (field ) != new_cookies [field ]:
127+ updated_cookie_dict [field ] = new_cookies [field ]
128+ updated = True
129+
130+ if updated :
131+ # 组装新cookie并写入 os.environ
132+ new_cookie_str = '; ' .join ([f'{ k } ={ v } ' for k , v in updated_cookie_dict .items ()])
133+ os .environ ['E_COOKIE' ] = new_cookie_str
134+ logging .info (f'已更新 os.environ 中的 E_COOKIE:{ new_cookie_str } ' )
135+
136+ # 发送过期时间通知
137+ notify_lines = [f"{ k } 过期时间: { v } " for k , v in expires_info if k in updated_cookie_dict ]
138+ if notify_lines :
139+ send_notify ("Cookie 更新提醒" , '\n ' .join (notify_lines ))
140+ else :
141+ logging .info ('本地 Cookie 是最新的,无需更新。' )
142+
74143def scrape ():
75144 init_proxy ()
76145 init_cookie ()
@@ -79,21 +148,25 @@ def scrape():
79148 response = requests .get ('https://e-hentai.org/news.php' , headers = headers , proxies = proxies , verify = False )
80149 response .raise_for_status ()
81150
151+ # 检查 Set-Cookie 并处理
152+ set_cookie_headers = response .headers .get_all ('Set-Cookie' ) if hasattr (response .headers , 'get_all' ) else response .headers .get ('Set-Cookie' )
153+ if set_cookie_headers :
154+ new_cookies , expires_info = parse_set_cookie (set_cookie_headers )
155+ compare_and_update_cookie_env (new_cookies , expires_info )
156+ else :
157+ logging .info ("未检测到 Set-Cookie,说明当前 Cookie 有效,无需更新。" )
158+
82159 soup = BeautifulSoup (response .text , 'html.parser' )
83160 event_pane = soup .find ('div' , id = 'eventpane' )
84161
85162 if event_pane :
86163 text_lines = [p .get_text () for p in event_pane .find_all ('p' )]
87-
88- # Check for "encounter" (case-insensitive)
89164 for line in text_lines :
90- if 'encounter' in line .lower (): # Case-insensitive check
165+ if 'encounter' in line .lower ():
91166 logging .info ('出现 Random Encounter!' )
92167 send_notify ('签到结果' , '出现 Random Encounter!' )
93168
94169 logging .info ('签到成功!' )
95- for line in text_lines :
96- logging .info (line )
97170 send_notify ('签到结果' , '签到成功!\n ' + '\n ' .join (text_lines ))
98171 return text_lines
99172 else :
0 commit comments