Compare commits

...
Author SHA1 Message Date
Deniz Şafak cb6c8d5ffd Add files via upload 2024-07-22 11:23:48 +03:00
Deniz Şafak 638e888e83 Delete HotspotAutoLogin.pyw 2024-07-22 11:10:05 +03:00
Deniz Şafak ff2ac683bf Delete requirements.txt 2024-07-22 11:09:52 +03:00
Deniz Şafak 585d8d11d7 Delete log.txt 2024-07-22 11:09:45 +03:00
Deniz Şafak ed01a946b6 Delete icon.ico 2024-07-22 11:09:36 +03:00
Deniz Şafak 224e5625a7 Delete config.json 2024-07-22 11:09:27 +03:00
Deniz Şafak 37fc8a6a76 Delete main/last_known_directory.txt 2024-07-22 11:09:06 +03:00
Deniz Şafak 9d511650f6 v1.82 2024-07-22 11:08:46 +03:00
Deniz Şafak bad72f479a Add files via upload 2024-03-31 22:27:42 +03:00
Deniz Şafak 55bfb8c07d Add files via upload 2024-03-31 18:39:04 +03:00
Deniz Şafak dd966d353d Add files via upload 2024-03-31 18:15:28 +03:00
6 changed files with 167 additions and 69 deletions
View File
@@ -16,9 +16,13 @@ dns.resolver.default_resolver = dns.resolver.Resolver()
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
# Change the current working directory to the script's directory
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_dir)
# Program Information
program_name = "HotspotAutoLogin"
version = "v1.80"
version = "v1.82"
github_link = "https://github.com/denizsafak/HotspotAutoLogin"
# Function to center a window on the screen
@@ -259,7 +263,6 @@ def get_connected_network():
try:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
# Check if connected to Ethernet
ethernet_output = subprocess.check_output(["netsh", "interface", "show", "interface"], startupinfo=startupinfo).decode("utf-8")
ethernet_lines = ethernet_output.split("\n")
@@ -267,7 +270,6 @@ def get_connected_network():
if "Connected" in line:
if "Ethernet" in line:
return "Ethernet"
# If not connected to Ethernet, check Wi-Fi SSID
wifi_output = subprocess.check_output(["netsh", "wlan", "show", "interfaces"], startupinfo=startupinfo).decode("utf-8")
wifi_lines = wifi_output.split("\n")
@@ -283,6 +285,21 @@ def get_connected_network():
except subprocess.CalledProcessError:
return False
# Is connected to wifi
def is_connected_to_wifi():
try:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
wifi_output = subprocess.check_output(["netsh", "wlan", "show", "interfaces"], startupinfo=startupinfo).decode("utf-8")
wifi_lines = wifi_output.split("\n")
for line in wifi_lines:
if "SSID" in line:
return True
else:
return False
except subprocess.CalledProcessError:
return False
# Function to exit the application
def exit_application(icon, item):
icon.stop()
@@ -416,6 +433,13 @@ def check_network_status():
if (connected_ssid):
connected_ssid_lower = connected_ssid.lower()
if (connected_ssid) and ((connected_ssid_lower == expected_ssid_lower)):
if is_connected_to_wifi() and (connected_ssid == "Ethernet" and expected_ssid == "Ethernet"):
message = "You are both connected to Wi-Fi and Ethernet. Disconnecting from Wi-Fi and continuing with Ethernet..."
add_to_log(message)
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.check_output(['netsh', 'wlan', 'disconnect', 'interface=' + "Wi-Fi"], startupinfo=startupinfo).decode("utf-8")
time.sleep(5)
if (is_internet_available()):
sleepcount = check_every_second
request_success = False
@@ -450,7 +474,7 @@ def check_network_status():
save_to_file(message)
except requests.exceptions.RequestException as e:
errorcount += 1
sleepcount = 1
sleepcount = 3
request_success = False
if isinstance(e, requests.exceptions.HTTPError):
# Handle HTTP errors with different messages for different status codes
@@ -466,13 +490,11 @@ def check_network_status():
else:
message = "Request failed with HTTP error ({}). Trying to reconnect to the network... (Errors: {}/10)".format(status_code, errorcount)
else:
message = "Request failed with error:{}. If you are usşng a VPN, please disable it for now. Sometimes 'Kill Switch' feature cause this error. Trying to reconnect to the network... (Errors: {}/10)".format(e, errorcount)
message = "Request failed with error: {}. If you are usşng a VPN, please disable it for now. Trying to reconnect to the network... (Errors: {}/10)".format(e, errorcount)
add_to_log(message)
save_to_file(message)
time.sleep(sleepcount)
# If the payload URL is not reachable, try to reconnect to the network
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
if connected_ssid == "Ethernet":
try:
# Disable and enable the Ethernet adapter
@@ -494,6 +516,8 @@ def check_network_status():
# Disconnect and reconnect to the Wi-Fi network
message = "Disconnecting from {}...".format(ssid)
add_to_log(message)
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.check_output(['netsh', 'wlan', 'disconnect', 'interface=' + "Wi-Fi"], startupinfo=startupinfo).decode("utf-8")
time.sleep(5)
subprocess.check_output(['netsh', 'wlan', 'connect', 'name=' + ssid], startupinfo=startupinfo).decode("utf-8")
@@ -518,10 +542,15 @@ def check_network_status():
wifi_enabled = True
break
if connected_ssid == "Ethernet":
sleepcount = 60
sleepcount = 30
message = "If you want to connect to {}, please unplug your ethernet cable or disconnect from {}. Checking again in {} seconds...".format(expected_ssid, connected_ssid, str(sleepcount))
errorcount = 0
add_to_log(message)
elif expected_ssid == "Ethernet":
sleepcount = 30
message = "Looks like your ethernet cable is not plugged in. Please plug in your ethernet cable. Checking again in {} seconds...".format(str(sleepcount))
errorcount = 0
add_to_log(message)
elif wifi_enabled:
if (connected_ssid):
sleepcount = 10
@@ -535,12 +564,14 @@ def check_network_status():
result = subprocess.run(['netsh', 'wlan', 'connect', 'name=' + expected_ssid], capture_output=True, text=True, startupinfo=startupinfo)
if result.returncode != 0:
if result.returncode == 1:
sleepcount = 60
raise ValueError("Could not find a Wi-Fi network with SSID: {}. Checking again in {} seconds...".format(expected_ssid, str(sleepcount)))
sleepcount = 20
message = "Could not find a Wi-Fi network with SSID: {}. If your Wi-Fi is disabled, please enable it. Checking again in {} seconds...".format(expected_ssid, str(sleepcount))
add_to_log(message)
else:
raise subprocess.CalledProcessError(result.returncode, result.args, result.stderr)
message = "Connected to {}. Running the script again in {} seconds...".format(expected_ssid, str(sleepcount))
add_to_log(message)
else:
message = "Connected to {}. Running the script again in {} seconds...".format(expected_ssid, str(sleepcount))
add_to_log(message)
except (subprocess.CalledProcessError, ValueError) as e:
errorcount += 1
sleepcount = 15
+1 -1
View File
@@ -35,4 +35,4 @@
}
}
]
}
}
View File

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 3.7 KiB

+123 -56
View File
@@ -1,52 +1,118 @@
@echo off
setlocal enabledelayedexpansion
cd /d "%~dp0"
set NAME=HotspotAutoLogin
set RUN=HotspotAutoLogin.pyw
set "requirementsFile=requirements.txt"
set PROJECTFOLDER=main
set RUN=%PROJECTFOLDER%\HotspotAutoLogin.pyw
set requirementsFile=%PROJECTFOLDER%\requirements.txt
set VENV_PATH=.venv
set ACTIVATE_PATH=%VENV_PATH%\Scripts\activate
set CURRENT_DIR=%CD%
set LAST_DIR_FILE=%PROJECTFOLDER%\last_known_directory.txt
set refrenv=%PROJECTFOLDER%\refrenv.bat
set PYTHON_DOWNLOAD_URL=https://www.python.org/ftp/python/3.12.2/python-3.12.2-amd64.exe
:: Display provided argument if any
if not "%~1"=="" (
echo Open with: "%~1"
)
:: Check if Python is installed
pip --version >nul 2>&1
if errorlevel 1 (
echo Python is not installed.
set /p "userinp=Do you want to install Python (y/n)? "
if /i "!userinp!"=="y" (
echo Opening download link...
start https://www.python.org/downloads/
exit /b
pip --version >nul 2>&1 && (set python_installed=true) || (set python_installed=false)
if not "%python_installed%"=="true" (
echo Python is required to run this program.
set /p "userinp=Do you want to install Python (y/n)?: "
if /i "!userinp!"=="y" (
echo Downloading Python...
winget install python --accept-package-agreements
if errorlevel 1 (
echo Download failed using winget method. Trying to download manually...
:bothwingetfailed
if not exist "%PROJECTFOLDER%\python_installer.exe" (
echo Downloading Python from %PYTHON_DOWNLOAD_URL%
curl %PYTHON_DOWNLOAD_URL% -o %PROJECTFOLDER%\python_installer.exe
if not exist "%PROJECTFOLDER%\python_installer.exe" (
echo Download failed using curl. Trying with the powershell method
powershell -Command "& {Invoke-WebRequest -Uri '%PYTHON_DOWNLOAD_URL%' -OutFile '%PROJECTFOLDER%\python_installer.exe'}"
if errorlevel 1 (
echo Failed to install Python. Please install Python manually and try again.
pause
exit /b
)
)
)
echo Installing Python. Please wait...
start /wait %PROJECTFOLDER%\python_installer.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
if errorlevel 1 (
echo Failed to install Python. Please install Python manually and try again.
pause
exit /b
)
del %PROJECTFOLDER%\python_installer.exe
goto python_ok
)
) else (
echo Please install Python and try again.
pause
exit /b
echo Python is required to run this program. Please install Python and try again.
pause
exit /b
)
:python_ok
echo Python installed successfully.
:: Refresh the environment variables
call %refrenv%
set python_installed=true
)
:: Check if virtual environment exists
IF NOT EXIST %VENV_PATH% (
echo Creating virtual environment...
python -m venv %VENV_PATH%
echo Creating virtual environment...
echo %CURRENT_DIR% > "%LAST_DIR_FILE%"
python -m venv %VENV_PATH%
) ELSE (
goto check_python
goto check_python
)
:: Check if Python exists at the path stored in the virtual environment
set counut_python=0
set count_python=0
:check_python
%VENV_PATH%\Scripts\python.exe --version >nul 2>&1
if %errorlevel% neq 0 (
set /a count_python+=1
echo Python not found at the path stored in the virtual environment.
echo Recreating virtual environment...
rmdir /s /q %VENV_PATH%
python -m venv %VENV_PATH%
if !count_python! lss 3 (
goto check_python
) else (
echo Something went wrong. Please try again.
pause
exit /b
)
set /a count_python+=1
echo Python not found at the path stored in the virtual environment.
echo Recreating virtual environment...
rmdir /s /q %VENV_PATH%
python -m venv %VENV_PATH%
if !count_python! lss 3 (
goto check_python
) else (
echo Something went wrong. Please try again.
pause
exit /b
)
)
:: Check if the current directory has changed since the last run
if exist "%LAST_DIR_FILE%" (
set /p last_dir=<"%LAST_DIR_FILE%"
cd "%CURRENT_DIR%" && (
if not exist "!last_dir!" (
echo Current directory: %CURRENT_DIR%
echo Last known directory: !last_dir!
echo Looks like the directory of the program has been changed.
echo It is hihgly recommended to recreate the virtual environment.
set /p "recreate_venv=Recreate the virtual environment (y/n)?: "
if /i "!recreate_venv!"=="y" (
echo Recreating virtual environment...
rmdir /s /q %VENV_PATH%
python -m venv %VENV_PATH%
:: Update the last directory file
echo %CURRENT_DIR% > "%LAST_DIR_FILE%"
)
)
)
) else (
:: Create the last directory file if it doesn't exist
echo %CURRENT_DIR% > "%LAST_DIR_FILE%"
)
:: Activate the virtual environment
@@ -55,45 +121,46 @@ set count_activate=0
echo Activating virtual environment...
call %ACTIVATE_PATH%
if errorlevel 1 (
set /a count_activate+=1
echo Failed to activate virtual environment.
echo Recreating virtual environment...
rmdir /s /q %VENV_PATH%
python -m venv %VENV_PATH%
if !count_activate! lss 3 (
goto activate
) else (
echo Failed to activate virtual environment. Please try again.
pause
exit /b
)
set /a count_activate+=1
echo Failed to activate virtual environment.
echo Recreating virtual environment...
rmdir /s /q %VENV_PATH%
python -m venv %VENV_PATH%
if !count_activate! lss 3 (
goto activate
) else (
echo Failed to activate virtual environment. Please try again.
pause
exit /b
)
)
:: Get the list of installed packages using pip freeze
echo Checking the requirements...
for /f "tokens=1,* delims==" %%i in ('pip freeze') do (
set installed[%%i]=%%j
for /f "tokens=1,* delims==" %%i in ('pip freeze --all') do (
set installed[%%i]=%%j
)
:: Compare with the requirements from the requirements.txt file
for /f "tokens=1,* delims==" %%i in (%requirementsFile%) do (
if not "!installed[%%i]!"=="%%j" (
echo Installing package: %%i==%%j
pip install %%i==%%j --upgrade --quiet
if errorlevel 1 (
echo Failed to install %%i==%%j. Please check your internet connection and try again.
pause
exit /b
)
)
if not "!installed[%%i]!"=="%%j" (
echo Installing package: %%i==%%j
pip install %%i==%%j --upgrade --quiet
if errorlevel 1 (
echo Failed to install %%i==%%j. Please check your internet connection and try again.
pause
exit /b
)
)
)
:: Run the program
echo Starting %NAME%...
start /B "" "%VENV_PATH%/Scripts/pythonw.exe" %RUN%
start /B "" "%VENV_PATH%\Scripts\pythonw.exe" %RUN% %* > nul 2>&1
if errorlevel 1 (
echo Failed to start %NAME%. Please try again.
pause
exit /b
echo Failed to start %NAME%. Please try again.
pause
exit /b
)
exit /b