Spaces:
Paused
Paused
File size: 1,167 Bytes
e26cd2e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import json
from pathlib import Path
from fake_useragent import UserAgent
def read_session_profiles(sessions: list[str]) -> dict | None:
file_path = Path("session_profile.json")
if not file_path.exists():
return None
try:
with file_path.open(encoding="utf-8") as file:
data = json.load(file)
return data if all(session in data and len(data[session]) >= 2 for session in sessions) else None
except (OSError, json.JSONDecodeError):
return None
def get_session_profiles(sessions: list[str]) -> dict:
session_profiles = read_session_profiles(sessions)
if session_profiles is None:
session_profiles = {}
ua_generator = UserAgent(browsers=["safari"], os=["ios"], platforms=["mobile", "tablet"])
for session in sessions:
inner = session_profiles.setdefault(session, [])
inner.append({"User-Agent": ua_generator.random})
inner.append({"proxy": None})
with Path("session_profile.json").open("w", encoding="utf-8") as file:
json.dump(session_profiles, file, ensure_ascii=False, indent=4)
return session_profiles
|