Skip to content

Commit

Permalink
Added Option to Auto Start
Browse files Browse the repository at this point in the history
Via Environmental Variable
  • Loading branch information
TheWicklowWolf authored Jun 6, 2024
1 parent 6d4cf2b commit 1404c76
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Certain values can be set via environment variables:
* __last_fm_api_key__: The API key for LastFM. Defaults to ``.
* __last_fm_api_secret__: The API secret for LastFM. Defaults to ``.
* __mode__: Mode for discovery (Spotify or LastFM). Defaults to `Spotify`.
* __auto_start__: Whether to run automatically at startup. Defaults to `False`.
* __auto_start_delay__: Delay duration fo Auto Start in Seconds (if enabled). Defaults to `60`.

---

Expand Down
30 changes: 26 additions & 4 deletions src/Lidify.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ def load_environ_or_config_settings(self):
"search_for_missing_albums": False,
"dry_run_adding_to_lidarr": False,
"app_name": "Lidify",
"app_rev": "0.04",
"app_url": "http://" + "".join(random.choices(string.ascii_lowercase, k=10)) + ".com",
"app_rev": "0.10",
"app_url": "http://Lidify.com",
"last_fm_api_key": "",
"last_fm_api_secret": "",
"mode": "Spotify",
"auto_start": False,
"auto_start_delay": 60,
}

# Load settings from environmental variables (which take precedence) over the configuration file.
Expand All @@ -84,6 +86,10 @@ def load_environ_or_config_settings(self):
self.last_fm_api_key = os.environ.get("last_fm_api_key", "")
self.last_fm_api_secret = os.environ.get("last_fm_api_secret", "")
self.mode = os.environ.get("mode", "")
auto_start = os.environ.get("auto_start", "")
self.auto_start = auto_start.lower() == "true" if auto_start != "" else ""
auto_start_delay = os.environ.get("auto_start_delay", "")
self.auto_start_delay = float(auto_start_delay) if auto_start_delay else ""

# Load variables from the configuration file if not set by environmental variables.
try:
Expand All @@ -106,6 +112,20 @@ def load_environ_or_config_settings(self):
# Save config.
self.save_config_to_file()

if self.auto_start:
try:
auto_start_thread = threading.Timer(self.auto_start_delay, self.automated_startup)
auto_start_thread.daemon = True
auto_start_thread.start()

except Exception as e:
self.lidify_logger.error(f"Auto Start Error: {str(e)}")

def automated_startup(self):
self.get_artists_from_lidarr(checked=True)
artists = [x["name"] for x in self.lidarr_items]
self.start(artists)

def connection(self):
if self.similar_artists:
if self.clients_connected_counter == 0:
Expand Down Expand Up @@ -153,7 +173,7 @@ def start(self, data):
else:
self.find_similar_artists()

def get_artists_from_lidarr(self):
def get_artists_from_lidarr(self, checked=False):
try:
self.lidify_logger.info(f"Getting Artists from Lidarr")
self.lidarr_items = []
Expand All @@ -163,7 +183,7 @@ def get_artists_from_lidarr(self):

if response.status_code == 200:
self.full_lidarr_artist_list = response.json()
self.lidarr_items = [{"name": unidecode(artist["artistName"], replace_str=" "), "checked": False} for artist in self.full_lidarr_artist_list]
self.lidarr_items = [{"name": unidecode(artist["artistName"], replace_str=" "), "checked": checked} for artist in self.full_lidarr_artist_list]
self.lidarr_items.sort(key=lambda x: x["name"].lower())
self.cleaned_lidarr_items = [item["name"].lower() for item in self.lidarr_items]
status = "Success"
Expand Down Expand Up @@ -443,6 +463,8 @@ def save_config_to_file(self):
"last_fm_api_key": self.last_fm_api_key,
"last_fm_api_secret": self.last_fm_api_secret,
"mode": self.mode,
"auto_start": self.auto_start,
"auto_start_delay": self.auto_start_delay,
},
json_file,
indent=4,
Expand Down

0 comments on commit 1404c76

Please sign in to comment.