diff --git a/Src/Lib/Request/user_agent.py b/Src/Lib/Request/user_agent.py index a103663..44f0504 100644 --- a/Src/Lib/Request/user_agent.py +++ b/Src/Lib/Request/user_agent.py @@ -6,7 +6,6 @@ import os import random import threading import json -import tempfile from typing import Dict, List # Internal utilities @@ -48,6 +47,28 @@ def update_user_agents(browser_name: str, browser_user_agents: Dict[str, List[st browser_user_agents[browser_name] = get_browser_user_agents_online(browser_name) +def create_temp_directory(): + """ + Create the TEMP directory if it doesn't exist. + + If the TEMP environment variable doesn't exist, this function creates + the TEMP directory in the user's local AppData folder. + + Returns: + str: The path to the TEMP directory. + """ + + # Get the TEMP directory from the environment variables + temp_dir = os.environ.get('TEMP') + + # If TEMP directory is not set, create it in the local AppData folder + if temp_dir is None: + temp_dir = os.path.join(os.path.expanduser("~"), "AppData", "Local", "Temp") + os.makedirs(temp_dir, exist_ok=True) + + return temp_dir + + def create_or_update_user_agent_file() -> None: """ Create or update the user agent file with browser user agents. @@ -80,8 +101,11 @@ class UserAgentManager: """ def __init__(self): + # Create temp directory if not exist + temp_dir = create_temp_directory() + # Get path to temp file where save all user agents - self.user_agent_file = os.path.join(tempfile.gettempdir(), 'fake_user_agent.json') + self.user_agent_file = os.path.join(temp_dir, 'fake_user_agent.json') # If file dont exist, creaet it if not os.path.exists(self.user_agent_file): diff --git a/Src/Util/_win32.py b/Src/Util/_win32.py index 1fac9f5..af6ba64 100644 --- a/Src/Util/_win32.py +++ b/Src/Util/_win32.py @@ -4,13 +4,19 @@ # run somehwere backup # add config to trace if ffmpeg is install, using config in local or temp -import winreg +import platform import os import logging +if platform.system() == "Windows": -# Define Windows registry key for user environment variables -env_keys = winreg.HKEY_CURRENT_USER, "Environment" + import winreg + + # Define Windows registry key for user environment variables + env_keys = winreg.HKEY_CURRENT_USER, "Environment" + +else: + env_keys = None def get_env(name: str) -> str: