Skip to content

Commit

Permalink
Check if Sunshine is running before trying anything
Browse files Browse the repository at this point in the history
  • Loading branch information
Arbitrate3280 committed Aug 4, 2024
1 parent 1fd1ad7 commit f41d57f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
13 changes: 9 additions & 4 deletions lutristosunshine.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from config.constants import COVERS_PATH, DEFAULT_IMAGE, SOURCE_COLORS, RESET_COLOR
from utils.utils import handle_interrupt, run_command, get_games_found_message, parse_json_output
from utils.input import get_yes_no_input, get_user_selection
from sunshine.sunshine import detect_sunshine_installation, add_game_to_sunshine, get_existing_apps, get_auth_token
from sunshine.sunshine import detect_sunshine_installation, add_game_to_sunshine, get_existing_apps, get_auth_token, is_sunshine_running
from utils.steamgriddb import manage_api_key, download_image_from_steamgriddb
from launchers.heroic import list_heroic_games, get_heroic_command, HEROIC_PATHS
from launchers.lutris import list_lutris_games, get_lutris_command, is_lutris_running
Expand All @@ -24,9 +24,14 @@ def main():
print("Error: Sunshine Flatpak is not supported. Please use the native installation of Sunshine.")
return

token = None
while token is None:
token = get_auth_token()
if not is_sunshine_running():
print("Error: Sunshine is not running. Please start Sunshine and try again.")
return

token = get_auth_token()
if not token:
print("Error: Could not obtain a valid authentication token. Exiting.")
return

lutris_command = get_lutris_command()
heroic_command, _ = get_heroic_command()
Expand Down
19 changes: 17 additions & 2 deletions sunshine/sunshine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import requests
import getpass
import urllib3
import subprocess
from typing import Tuple, Optional, Dict, List
from config.constants import DEFAULT_IMAGE, CREDENTIALS_PATH, SUNSHINE_API_URL
from utils.utils import run_command
Expand Down Expand Up @@ -53,11 +54,25 @@ def get_sunshine_credentials() -> Tuple[str, str]:
password = getpass.getpass("Enter your Sunshine password: ")
return username, password

def is_sunshine_running() -> bool:
"""Checks if Sunshine is currently running."""
try:
# Run the ps command to check for the Sunshine process
output = subprocess.check_output(["ps", "-A"], stderr=subprocess.STDOUT).decode()
return "sunshine" in output.lower() # Check if "sunshine" is present in the process list
except subprocess.CalledProcessError:
return False

def get_auth_token() -> Optional[str]:
"""Retrieves or generates an authentication token."""
token_path = os.path.join(CREDENTIALS_PATH, "auth_token.txt")

# Check for an existing token
# Check if Sunshine is running BEFORE attempting any authentication
if not is_sunshine_running():
print("Error: Sunshine is not running. Please start Sunshine and try again.")
return None

# Check for an existing token (only if Sunshine is running)
if os.path.exists(token_path):
with open(token_path, 'r') as f:
token = f.read().strip()
Expand All @@ -70,7 +85,7 @@ def get_auth_token() -> Optional[str]:
else:
return token

# If no valid token exists, prompt for credentials
# If no valid token exists, prompt for credentials (only if Sunshine is running)
username, password = get_sunshine_credentials()
if not username or not password:
return None
Expand Down

0 comments on commit f41d57f

Please sign in to comment.