Created
October 30, 2025 09:16
-
-
Save megadix/e573ec121b53f5d21590464f0411fc4d to your computer and use it in GitHub Desktop.
Searches for web radio stations by genre using the Radio Browser API and plays them on VLC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .SYNOPSIS | |
| Searches for and plays web radio stations by genre using the Radio Browser API. | |
| .DESCRIPTION | |
| The playWebRadio function searches the Radio Browser API (radio-browser.info) for radio stations | |
| matching a specified genre/style and plays a random working station using VLC media player. | |
| The function performs the following operations: | |
| - Searches the Radio Browser API for stations matching the specified genre tag | |
| - Filters results to only include working stations (passed last health check) | |
| - Orders results by popularity (vote count) | |
| - Randomly selects one station from the available options | |
| - Registers a click with the API (good practice for the service) | |
| - Plays the selected station using VLC media player | |
| .PARAMETER Style | |
| The music genre or style to search for. This parameter is used as a tag in the Radio Browser API. | |
| Common genres include: ambient, jazz, rock, classical, electronic, pop, blues, country, etc. | |
| Default value is "ambient". | |
| .EXAMPLE | |
| playWebRadio | |
| Plays a random ambient radio station (default genre). | |
| .EXAMPLE | |
| playWebRadio -Style "jazz" | |
| Searches for and plays a random jazz radio station. | |
| .EXAMPLE | |
| playWebRadio "classical" | |
| Searches for and plays a random classical music radio station. | |
| .NOTES | |
| Prerequisites: | |
| - VLC media player must be installed at 'C:\Program Files\VideoLAN\VLC\vlc.exe' | |
| - Internet connection required to access Radio Browser API | |
| - PowerShell with Invoke-RestMethod support | |
| API Information: | |
| - Uses Radio Browser API (https://de1.api.radio-browser.info) | |
| - Searches by tag/genre with a limit of 50 stations | |
| - Only returns stations that passed the last health check | |
| - Results are ordered by popularity (vote count) | |
| Error Handling: | |
| - Displays helpful message if no stations are found for the specified genre | |
| - Suggests alternative genres if search fails | |
| - Gracefully handles API timeouts and connection errors | |
| - Click registration errors are silently ignored | |
| .LINK | |
| Radio Browser API: https://radio-browser.info/ | |
| .OUTPUTS | |
| Console output displaying: | |
| - Search progress and results count | |
| - Selected station information (name, country, tags, bitrate, codec, URL) | |
| - Error messages if applicable | |
| #> | |
| function playWebRadio { param( | |
| [string]$Style = "ambient" | |
| ) | |
| Write-Output "Searching Radio Browser for '$Style' stations..." | |
| try { | |
| # Use one of the Radio Browser API servers | |
| $apiBase = "https://de1.api.radio-browser.info" | |
| # Search by tag (genre) | |
| $searchUrl = "$apiBase/json/stations/search" | |
| $body = @{ | |
| tag = $Style | |
| hidebroken = "true" # Only return working stations | |
| order = "votes" # Order by popularity | |
| reverse = "true" # Most popular first | |
| limit = 50 # Get 50 stations to choose from | |
| } | ConvertTo-Json | |
| $headers = @{ | |
| "User-Agent" = "RandomRadioPlayer/1.0" | |
| "Content-Type" = "application/json" | |
| } | |
| $response = Invoke-RestMethod -Uri $searchUrl -Method Post -Body $body -Headers $headers -TimeoutSec 10 | |
| if ($response.Count -eq 0) { | |
| Write-Output "No stations found for: $Style" | |
| Write-Output "Try different genres like: jazz, rock, classical, electronic, pop, etc." | |
| return | |
| } | |
| # Filter only stations that passed last check | |
| $workingStations = $response | Where-Object { | |
| $_.lastcheckok -eq $true | |
| } | |
| if ($workingStations.Count -eq 0) { | |
| Write-Output "No working stations found matching your criteria" | |
| return | |
| } | |
| # Pick a random station | |
| $station = $workingStations | Get-Random | |
| Write-Output "`nFound $($workingStations.Count) working stations" | |
| Write-Output "Playing: $($station.name)" | |
| Write-Output "Country: $($station.country)" | |
| Write-Output "Tags: $($station.tags -replace ',', ', ')" | |
| Write-Output "Bitrate: $($station.bitrate) kbps" | |
| Write-Output "Codec: $($station.codec)" | |
| Write-Output "URL: $($station.url_resolved)" | |
| # Register the click with the API (good practice) | |
| try { | |
| $clickUrl = "$apiBase/json/url/$($station.stationuuid)" | |
| Invoke-RestMethod -Uri $clickUrl -Headers $headers -TimeoutSec 5 | Out-Null | |
| } catch { | |
| # Ignore click registration errors | |
| } | |
| # Play with VLC | |
| $vlcPath = 'C:\Program Files\VideoLAN\VLC\vlc.exe' | |
| & $vlcPath '--one-instance' '--playlist-enqueue' $station.url_resolved | |
| } catch { | |
| Write-Output "Error: $_" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment