Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- persistent_user_manager.py +58 -34
persistent_user_manager.py
CHANGED
|
@@ -102,22 +102,32 @@ class PersistentUserManager:
|
|
| 102 |
password=cookie_password
|
| 103 |
)
|
| 104 |
|
| 105 |
-
# Cookie
|
| 106 |
-
max_attempts =
|
|
|
|
|
|
|
|
|
|
| 107 |
for attempt in range(max_attempts):
|
| 108 |
if cookies.ready():
|
|
|
|
| 109 |
break
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
|
|
|
| 115 |
if not cookies.ready():
|
| 116 |
-
logger.warning("Cookie
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
st.session_state[browser_session_key] = True
|
| 120 |
-
return
|
| 121 |
|
| 122 |
self.cookies = cookies
|
| 123 |
self._cookie_initialized = True
|
|
@@ -237,7 +247,7 @@ class PersistentUserManager:
|
|
| 237 |
return temp_id
|
| 238 |
|
| 239 |
def _get_user_id_from_cookie(self) -> Optional[str]:
|
| 240 |
-
"""CookieからユーザーIDを取得(永続Cookie
|
| 241 |
try:
|
| 242 |
# Cookie管理の初期化(初回のみ)
|
| 243 |
self._ensure_cookie_manager()
|
|
@@ -246,35 +256,43 @@ class PersistentUserManager:
|
|
| 246 |
logger.debug("Cookie管理システム無効 - None返却")
|
| 247 |
return None
|
| 248 |
|
| 249 |
-
# Cookie
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
all_cookies = dict(self.cookies)
|
| 256 |
-
logger.debug(f"利用可能なCookie: {list(all_cookies.keys())}")
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
except Exception as e:
|
| 273 |
logger.warning(f"Cookie取得エラー: {e}")
|
| 274 |
return None
|
| 275 |
|
| 276 |
def _set_user_id_cookie(self, user_id: str):
|
| 277 |
-
"""ユーザーID
|
| 278 |
try:
|
| 279 |
# Cookie管理システムが初期化されていない場合はスキップ
|
| 280 |
if not self._cookie_initialized:
|
|
@@ -285,16 +303,22 @@ class PersistentUserManager:
|
|
| 285 |
logger.debug("Cookie管理システム無効 - Cookie設定スキップ")
|
| 286 |
return
|
| 287 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
# 永続的なCookieを設定
|
| 289 |
self.cookies[self.cookie_name] = user_id
|
| 290 |
|
| 291 |
# Cookieを保存(streamlit-cookies-managerは自動的に永続化される)
|
| 292 |
self.cookies.save()
|
| 293 |
|
| 294 |
-
logger.info(f"HF Spaces永続クッキーに保存: {user_id[:8]}... (キー: {self.cookie_name})")
|
| 295 |
|
| 296 |
except Exception as e:
|
| 297 |
logger.warning(f"永続Cookie設定エラー: {e}")
|
|
|
|
| 298 |
|
| 299 |
def _is_valid_uuid(self, uuid_string: str) -> bool:
|
| 300 |
"""UUIDの形式チェック"""
|
|
|
|
| 102 |
password=cookie_password
|
| 103 |
)
|
| 104 |
|
| 105 |
+
# Cookieが準備できるまで待機(より長い待機時間で既存Cookie読み込み対応)
|
| 106 |
+
max_attempts = 10 # 試行回数を増加
|
| 107 |
+
total_wait_time = 0
|
| 108 |
+
max_wait_time = 3.0 # 最大3秒待機
|
| 109 |
+
|
| 110 |
for attempt in range(max_attempts):
|
| 111 |
if cookies.ready():
|
| 112 |
+
logger.info(f"Cookie準備完了 ({attempt + 1}回目の試行で成功)")
|
| 113 |
break
|
| 114 |
+
|
| 115 |
+
wait_time = 0.2 + (attempt * 0.1) # 段階的に待機時間を延長
|
| 116 |
+
total_wait_time += wait_time
|
| 117 |
+
|
| 118 |
+
if total_wait_time > max_wait_time:
|
| 119 |
+
logger.warning(f"Cookie準備タイムアウト ({total_wait_time:.1f}秒経過)")
|
| 120 |
+
break
|
| 121 |
+
|
| 122 |
+
logger.debug(f"Cookie準備待機中... ({attempt + 1}/{max_attempts}, {wait_time:.1f}秒待機)")
|
| 123 |
+
import time
|
| 124 |
+
time.sleep(wait_time)
|
| 125 |
|
| 126 |
+
# Cookie準備ができていない場合でも、一度試行してみる
|
| 127 |
if not cookies.ready():
|
| 128 |
+
logger.warning("Cookie準備未完了だが、既存Cookie読み込みを試行")
|
| 129 |
+
# フォールバックモードにせず、Cookieオブジェクトは保持
|
| 130 |
+
# 既存のCookieがある場合は読み込める可能性がある
|
|
|
|
|
|
|
| 131 |
|
| 132 |
self.cookies = cookies
|
| 133 |
self._cookie_initialized = True
|
|
|
|
| 247 |
return temp_id
|
| 248 |
|
| 249 |
def _get_user_id_from_cookie(self) -> Optional[str]:
|
| 250 |
+
"""CookieからユーザーIDを取得(永続Cookie対応・準備未完了でも試行)"""
|
| 251 |
try:
|
| 252 |
# Cookie管理の初期化(初回のみ)
|
| 253 |
self._ensure_cookie_manager()
|
|
|
|
| 256 |
logger.debug("Cookie管理システム無効 - None返却")
|
| 257 |
return None
|
| 258 |
|
| 259 |
+
# Cookie準備状態をチェックするが、準備未完了でも読み込みを試行
|
| 260 |
+
is_ready = self.cookies.ready()
|
| 261 |
+
if not is_ready:
|
| 262 |
+
logger.debug("Cookie準備未完了だが、既存Cookie読み込みを試行")
|
| 263 |
+
else:
|
| 264 |
+
logger.debug("Cookie準備完了 - 通常読み込み")
|
|
|
|
|
|
|
| 265 |
|
| 266 |
+
# 既存Cookieの読み込みを試行(準備状態に関係なく)
|
| 267 |
+
try:
|
| 268 |
+
# 全てのCookieを確認(デバッグ用)
|
| 269 |
+
all_cookies = dict(self.cookies)
|
| 270 |
+
logger.debug(f"利用可能なCookie: {list(all_cookies.keys())} (準備状態: {is_ready})")
|
| 271 |
|
| 272 |
+
user_id = self.cookies.get(self.cookie_name)
|
| 273 |
+
if user_id:
|
| 274 |
+
logger.debug(f"Cookie値取得: {user_id[:8] if len(user_id) >= 8 else user_id}...")
|
| 275 |
+
|
| 276 |
+
if self._is_valid_uuid(user_id):
|
| 277 |
+
logger.info(f"HF Spaces永続CookieからユーザーID取得成功: {user_id[:8]}... (キー: {self.cookie_name}, 準備状態: {is_ready})")
|
| 278 |
+
return user_id
|
| 279 |
+
else:
|
| 280 |
+
logger.warning(f"Cookie内のユーザーIDが無効な形式: {user_id}")
|
| 281 |
+
return None
|
| 282 |
+
|
| 283 |
+
logger.debug(f"Cookie内に有効なユーザーIDなし (準備状態: {is_ready})")
|
| 284 |
+
return None
|
| 285 |
+
|
| 286 |
+
except Exception as cookie_read_e:
|
| 287 |
+
logger.warning(f"Cookie読み込み試行エラー: {cookie_read_e}")
|
| 288 |
+
return None
|
| 289 |
|
| 290 |
except Exception as e:
|
| 291 |
logger.warning(f"Cookie取得エラー: {e}")
|
| 292 |
return None
|
| 293 |
|
| 294 |
def _set_user_id_cookie(self, user_id: str):
|
| 295 |
+
"""ユーザーIDを永続的なクッキーに設定(準備未完了でも試行)"""
|
| 296 |
try:
|
| 297 |
# Cookie管理システムが初期化されていない場合はスキップ
|
| 298 |
if not self._cookie_initialized:
|
|
|
|
| 303 |
logger.debug("Cookie管理システム無効 - Cookie設定スキップ")
|
| 304 |
return
|
| 305 |
|
| 306 |
+
# Cookie準備状態をチェックするが、準備未完了でも設定を試行
|
| 307 |
+
is_ready = self.cookies.ready()
|
| 308 |
+
if not is_ready:
|
| 309 |
+
logger.debug("Cookie準備未完了だが、Cookie設定を試行")
|
| 310 |
+
|
| 311 |
# 永続的なCookieを設定
|
| 312 |
self.cookies[self.cookie_name] = user_id
|
| 313 |
|
| 314 |
# Cookieを保存(streamlit-cookies-managerは自動的に永続化される)
|
| 315 |
self.cookies.save()
|
| 316 |
|
| 317 |
+
logger.info(f"HF Spaces永続クッキーに保存: {user_id[:8]}... (キー: {self.cookie_name}, 準備状態: {is_ready})")
|
| 318 |
|
| 319 |
except Exception as e:
|
| 320 |
logger.warning(f"永続Cookie設定エラー: {e}")
|
| 321 |
+
# エラーが発生してもアプリケーションは継続
|
| 322 |
|
| 323 |
def _is_valid_uuid(self, uuid_string: str) -> bool:
|
| 324 |
"""UUIDの形式チェック"""
|