Update .forgejo/scripts/radio_country_export.py

This commit is contained in:
Vlastimil Novotny / Ch-last / mxnticek 2025-06-15 02:15:17 +02:00
parent 447760c010
commit 3b911b9339

View file

@ -20,6 +20,39 @@ class RateLimitError(Exception):
"""Exception raised when rate limit is hit""" """Exception raised when rate limit is hit"""
pass pass
def get_workspace_safe_path(output_file):
"""
Ensure output path is safe for CI/CD environments.
If GITHUB_WORKSPACE is set and output_file is relative, use workspace as base.
"""
if not output_file:
return None
# If already absolute path, return as-is
if os.path.isabs(output_file):
return output_file
# Check if we're in a CI environment with GITHUB_WORKSPACE
workspace = os.environ.get('GITHUB_WORKSPACE')
if workspace and os.path.exists(workspace):
return os.path.join(workspace, output_file)
# Fallback to current working directory for relative paths
return os.path.abspath(output_file)
def generate_safe_output_path(output_file, country_codes):
"""Generate a safe output path for the playlist file"""
if not output_file:
country_string = "_".join(country_codes[:5]).upper()
if len(country_codes) > 5:
country_string += "_etc"
output_file = f"radio_playlist_{country_string}_{datetime.now().strftime('%Y%m%d')}.m3u"
# Make sure the path is workspace-safe
safe_path = get_workspace_safe_path(output_file)
print(f"Output file will be created at: {safe_path}")
return safe_path
def fetch_stations_with_retry(rb, country_code, max_retries=5, initial_backoff=10): def fetch_stations_with_retry(rb, country_code, max_retries=5, initial_backoff=10):
""" """
Fetch stations with retry logic for rate limiting Fetch stations with retry logic for rate limiting
@ -76,12 +109,8 @@ def create_multi_country_playlist(country_codes, output_file=None, group_title="
# Initialize RadioBrowser client # Initialize RadioBrowser client
rb = RadioBrowser() rb = RadioBrowser()
# Generate output filename if not provided # Generate safe output filename
if not output_file: output_file = generate_safe_output_path(output_file, country_codes)
country_string = "_".join(country_codes[:5]).upper() # Use first 5 countries for filename
if len(country_codes) > 5:
country_string += "_etc"
output_file = f"radio_playlist_{country_string}_{datetime.now().strftime('%Y%m%d')}.m3u"
# Get all country information for validation and display # Get all country information for validation and display
try: try:
@ -158,13 +187,17 @@ def create_multi_country_playlist(country_codes, output_file=None, group_title="
if failed_countries: if failed_countries:
print(f"Failed to process these countries: {', '.join(failed_countries)}") print(f"Failed to process these countries: {', '.join(failed_countries)}")
# In CI environment, don't ask for user input - just log and continue
if os.environ.get('GITHUB_WORKSPACE') or os.environ.get('CI'):
print("Running in CI environment - skipping retry prompt")
else:
retry_option = input("Would you like to retry failed countries? (y/n): ") retry_option = input("Would you like to retry failed countries? (y/n): ")
if retry_option.lower() == 'y': if retry_option.lower() == 'y':
print("Retrying failed countries...") print("Retrying failed countries...")
# Wait a bit longer before retrying failed countries # Wait a bit longer before retrying failed countries
time.sleep(30) time.sleep(30)
return create_multi_country_playlist(failed_countries, return create_multi_country_playlist(failed_countries,
f"retry_{output_file}", f"retry_{os.path.basename(output_file)}",
group_title, group_title,
default_logo_url, default_logo_url,
use_country_as_group) use_country_as_group)